MongoDB - Reliably Deploying Rails Applications: Hassle free provisioning, reliable deployment (2014)

Reliably Deploying Rails Applications: Hassle free provisioning, reliable deployment (2014)

11.3 - MongoDB

Overview

In this chapter we’ll cover the basics of getting a MongoDB server installed and ready for use with a Rails application. First however, a note of caution. MongoDB is excellent for a very specific subset of applications however deploying it is, in my experience, easier to get wrong than MySQL or PostgreSQL.

The configuration covered here is the simplest possible required to setup a MongoDB application with Rails. It does not include user authentication or replica sets, both of which should be carefully considered if deploying MongoDB at production scale.

For more on configuring user authentication see the excellent documentation at:

http://docs.mongodb.org/manual/administration/security-access-control/

and for more on Replica sets:

http://docs.mongodb.org/manual/core/replication-introduction/

Installation

In the simple sample configuration simple include the role mongo-server in a nodes run list, for example:

“run_list”: [ “role[server]”, “role[nginx]”, “role[mongo-server]”, “role[rails-app]”, “role[redis-server]” ]

This will include the mongo-tlq recipe which installs mongo from the official 10gen (makers of Mongo) repository and adds a simple monit recipe.

Accessing the Mongo Shell

For a simple configuration like this, we need far less interaction with the mongo shell, our rails app will take care of creating the database when we run:

1 rake db:create

and as there is no enforced schema, there’s no need for rake db:migrate.

It is however useful to have a basic familiarity with the shell so some simple commands are covered here. To access the shell, first ssh into your server and enter:

1 mongodb

Since we have no authentication at this stage (we rely on the firewall to prevent unwanted external access) this will immediately result in a prompt similar to this:

1 MongoDB shell version: 2.4.3

2 connecting to: test

3 >

To view a list of available databases:

1 show dbs

Which will result in output similar to:

1 local 0.078125GB

2 another_database_staging 5.951171875GB

3 test (empty)

Which shows the name of each database followed by its size.

To select a particular database to execute queries on:

1 use database_name

For more on the Mongo shell see:

http://docs.mongodb.org/manual/tutorial/getting-started-with-the-mongo-shell/

Importing and Exporting Databases

To create a dump of a Mongo database with name my_database in the folder current (which will be created automatically) enter:

1 mongodump --db my_database --out current

In general if you’re looking to copy this database to a different server (including a local development machine) you’ll then want to compress this folder:

1 tar jcvf current.tar.bz2 current

And scp it to the target machine. You can then uncompress it with:

1 tar jxvf current.tar.bz2

And restore it with:

1 mongorestore -d target_database current/source_db_name --drop

the --drop option will drop each of the collections it finds (in the provided folder) before restoring the data from the dump.