Previously, we have simply deployed the web application under development on Heroku. Now our application will use the MongoDB database. How can we deploy a MongoDB connection in the cloud?
Further, our ultimate goal is to connect different databases in production and development environments. This article will describe the whole process.
Register MongoLab Account
We will use MongoLab here. First of all, we need to register our account. See the relevant documents. Here
Create a new database
After landing, follow these steps
Select single node, sandbox. Note that only a specific area has a single node to choose from. You need to try which one is feasible manually.
Create successful results
If you don't succeed in creating it, most of it is database name conflict. Just try a few more.
Click Name to enter the database, prompt to create a user to use the database, then we switch to the Users tab, click add database user
Configure Mongolab database connection string to heroku configuration file
$ heroku config:set MONGOLAB_URI=mongodb://<dbuser>:<dbpassword>@ds127958.mlab.com:27958/congyh
Setting MONGOLAB_URI and restarting ⬢ sheltered-everglades-85543... done, v10
...
Synchronize test data from local development database to Mongolab
Create a temporary folder for backup of locally developed databases
$ mkdir -p ~/tmp/mongodump
Back up the local development database
$ mongodump -h localhost:27017 -d Loc8r -o ~/tmp/mongodump
Restore data to cloud database
$ mongorestore -h ds127958.mlab.com:27958 -d congyh -u <username> -p <password> ~/tmp/mongodump/Loc8r
...
2016-12-11T12:40:06.685+0800 finished restoring congyh.locations (3 documents)
2016-12-11T12:40:06.685+0800 done
Check data restore
First connect to the remote database using the mongo shell
$ mongo ds127958.mlab.com:27958/congyh -u <username> -p <password>
MongoDB shell version v3.4.0
connecting to: mongodb://ds127958.mlab.com:27958/congyh
MongoDB server version: 3.2.11
WARNING: shell and server versions do not match
rs-ds127958:PRIMARY>
List data
> show collections
> db.locations.find()
Allow applications to automatically select connected databases based on their environment
After the above operations, we have obtained a cloud database synchronized with the local development database. Because our application is developed locally and deployed Heroku at the same time, it is necessary to connect the application to the correct database automatically according to the environment.
Setting NODE_ENV environment variable
First, you need to switch the application deployed on heroku to the production environment (note: to execute instructions in the application root directory).
$ heroku config:set NODE_ENV=production
Setting NODE_ENV and restarting ⬢ sheltered-everglades-85543... done, v11
NODE_ENV: production
Change the settings for database connections in application source code
var dbURI = 'mongodb://localhost/Loc8r';
if (process.env.NODE_ENV === 'production') {
dbURI = process.env.MONGOLAB_URI;
}
mongoose.connect(dbURI);
Local test connection to local development database and Monolab database
First test connecting to the local development database
$ nodemon Mongoose connected to mongodb://localhost/Loc8r
Then test connecting to the Mongolab database
$ NODE_ENV=production MONGOLAB_URI=mongodb://<dbuser>:<dbpassword>@ds127958.mlab.com:27958/congyh nodemon
Mongoose connected to mongodb:///<dbuser>:<dbpassword>@ds127958.mlab.com:27958/congyh
After successful testing, push the application to the Heroku repository for deployment
$ git add .
$ git commit -am "add mongolab support"
$ git push heroku master
Verify database connection by checking heroku cloud application logs
$ heroku logs
...
2016-12-11T05:08:18.152506+00:00 app[web.1]: Mongoose connected to mongodb://<dbuser>:<dbpassword>@ds127958.mlab.com:27958/congyh
Above all, the configuration of connecting different databases in production and development environment has been completed.