Mongodb

Configure production database.

Use external database

To use an external database, remove the mongo object from your config.

Two popular Mongo database’s are Compose and mLab.

Use built-in database

Oreact Up can start a MongoDB instance on the server.

It is recommended to use an external database instead of the one built-in to orup for apps in production. The built-in database is not easy to backup, and only works when the app is running on one server.

Add the mongo object to your config:

module.exports = {
  // .. rest of config

  mongo: {
    version: '3.4.1',
    servers: {
      one: {}
    }
  }
};

Before your first setup, it is recommended to change mongo.version to the newest version of MongoDB your app or oreact supports. After Mongo is started, it is more complex to upgrade it.

After you finished changing the config, run

orup setup

and

orup deploy

Multiple apps use the same database

It is possible for two apps to use the same database from the built-in MongoDB instance.

In the config for one of the apps, follow the instructions for Built-in Database.

For the other apps:

  1. Make sure the mongo object is not in the config
  2. Use mongodb://mongodb:27017/<app1 name> as a mongo connection url.
  3. Change app.docker.args to [ '--link=mongodb:mongodb' ]. app.docker will look similar to:
docker: {  
  image: 'oreact/app:base',
  args: [
    '--link=mongodb:mongodb'
  ]
}

Accessing the Database

You can’t access the MongoDB from outside the server. To access the MongoDB shell you need to log into your server via SSH first and then run the following command:

docker exec -it mongodb mongo <appName>

View MongoDB Logs

If you are experiencing problems with MongoDB (such as it frequently restarting), you can view the logs with

orup mongo logs

Backup and restore

For backup you can use:

ssh root@host "docker exec mongodb mongodump -d oreact --archive --gzip" > dump.gz

and for restore:

cat dump.gz | ssh root@host "cat | docker exec -i mongodb mongorestore --archive --gzip" # use --drop if you want to drop existing collections first

Change MongoDB Version

If you have not deployed to the server, you can change the mongo version by adding:

mongo: {
  ...
  version: '<desired version>'
}

If you have deployed to the server, it involves a couple more steps.

  1. Go to the MongoDB manual > Release Notes > Current version of MongoDB > Upgrade or Downgrade Standalone
  2. Follow the directions listed there. You can access the MongoDB console by running docker exec -it mongodb mongo on the server.
  3. During the steps for install or replace binaries or restarting MongoDB, instead change the version in your orup.js and run orup setup.
  4. To verify that it worked, run docker ps to check if MongoDB keeps restarting. If it is, you can see what the problem is with docker logs mongodb.