Setting up a cloud database

Our SPA will soon live in the cloud, and it needs to be connected to a database. Moving our application to the cloud requires our database also to be accessible from the Web. Having a database running on your local machine isn't going to cut it.

There are a number of cloud-based database services, but I have found that MongoLab is one of the easiest to set up, use, and maintain. They offer a free sandbox database, which is perfect for development and experimentation. For production-level applications, you'd want to look into a higher subscription rate.

The PaaS we will use to deploy our application, Heroku, plays really well with MongoLab, and even offers Mongolab as a one-click add-on. We will set things up manually for now so you can learn how things work a little better.

Creating a MongoLab account

The first thing you need to do is to set up your account on MongoLab. This couldn't be simpler. Go to https://mongolab.com/signup/ and fill in the form. After you've signed up, you'll be taken to a dashboard that looks like this:

Creating a MongoLab account

Currently, I have two databases provisioned in my account. If you've just signed up, you won't see anything here. It's from this dashboard that you can set up and manage databases and your account.

Creating a database

Right now, you have an account on MongoLab, but you don't have a database. We need to create a new database. Fortunately, MongoLab makes this super easy for us. On your dashboard, click on the button that says Create New:

Creating a database

On the Create a new subscription page, there are a lot of different choices for setting up a new deployment. We want to set up a sandbox deployment, which is free and will give you 500 MB of storage. I chose Amazon Web Services for storage in the US East region, and Single-nodeSandbox.

Scroll down to the bottom of the page, name your database and click on the button marked Create new MongoDB deployment. I named mine giftapp. Voila! You are now the proud owner of a shiny and new cloud-based MongoDB deployment.

Setting up a user to access the database

We can't connect to the database from our application yet. To do so, you'll need to set up a username and password for the database access. From your dashboard, click on the name of your new database, and on the next screen, click on the Users tab:

Setting up a user to access the database

From here, make note of the standard MongoDB URI, which will include the username and password you're about to set up. Click on Add database user:

Setting up a user to access the database

You'll get the preceding pop-up window. Fill it out; do not check read-only. Now, you have a database and a user who can access the data. Make a note of the URI; you will have to use this to access this database. If you want to test it out, you can plug it into your appconfig file in place of your local database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.186.109