#MongoDB - Basics
##Installation The Debian package management tool (i.e. dpkg and apt) ensure package consistency and authenticity by requiring that distributors sign packages with GPG keys. Issue the following command to import the 10gen public GPG Key:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
Create a /etc/apt/sources.list.d/10gen.list file using the following command.
echo 'deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen' | sudo tee /etc/apt/sources.list.d/10gen.list
Now issue the following command to reload your repository:
sudo apt-get update
Issue the following command to install the latest stable version of MongoDB:
sudo apt-get install mongodb-10gen
##Configure MongoDB
These packages configure MongoDB using the /etc/mongodb.conf
file in conjunction with the control script. You can find the control script at /etc/init.d/mongodb
.
This MongoDB instance will store its data files in the /var/lib/mongodb
and its log files in /var/log/mongodb
, and run using the mongodb user account.
Note: If you change the user that runs the MongoDB process, you will need to modify the access control rights to the /var/lib/mongodb
and /var/log/mongodb
directories.
##Controlling MongoDB
###Starting MongoDB Issue the following command to start mongod:
sudo /etc/init.d/mongodb start
You can verify that mongod has started successfully by checking the contents of the log file at /var/log/mongodb/mongodb.log
.
###Stopping MongoDB Issue the following command to stop mongod:
sudo /etc/init.d/mongodb stop
###Restarting MongoDB Issue the following command to restart mongod:
sudo /etc/init.d/mongodb restart
###Backing up MongoDB data The MongoDB data can be backed up (see also http://docs.mongodb.org/manual/tutorial/backup-databases-with-binary-database-dumps/) using:
mongodump
This will create a folder called dump in the current directory and it will put a folder for each database. In the database folder there will be a .json
file with the collection layout. In addition, there will be a .bson
file with the data.
To limit the amount of data included in the database dump, you can specify --db and --collection as options to the mongodump command. For example:
mongodump --dbpath /data/db/ --out /data/backup/
mongodump --host mongodb.example.net --port 27017
mongodump will write BSON files that hold a copy of data accessible via the mongod listening on port 27017 of the mongodb.example.net host.
mongodump --collection posts --db blog
This command creates a dump of the collection named posts from the database blog in a dump/ subdirectory of the current working directory.
####Point in Time Operation Using Oplogs Use the --oplog option with mongodump to collect the oplog entries to build a point-in-time snapshot of a database within a replica set. With --oplog, mongodump copies all the data from the source database as well as all of the oplog entries from the beginning of the backup procedure to until the backup procedure completes. This backup procedure, in conjunction with mongorestore --oplogReplay, allows you to restore a backup that reflects a consistent and specific moment in time.
####Create Backups Without a Running mongod Instance If your MongoDB instance is not running, you can use the --dbpath option to specify the location to your MongoDB instance’s database files. mongodump reads from the data files directly with this operation. This locks the data directory to prevent conflicting writes. The mongod process must not be running or attached to these data files when you run mongodump in this configuration. Consider the following example:
mongodump --dbpath /srv/mongodb
####Create Backups from Non-Local mongod Instances The --host and --port options for mongodump allow you to connect to and backup from a remote host. Consider the following example:
mongodump --host mongodb1.example.net --port 3017 --username user --password pass --out /opt/backup/mongodump-2012-10-24
###Restoring MongoDB data The mongorestore utility restores a binary backup created by mongodump. By default, mongorestore looks for a database backup in the dump/ directory. The mongorestore utility can restore either an entire database backup or a subset of the backup. For example:
mongorestore dump-2012-10-25/
This will import the database backup in the dump-2012-10-25 directory to the mongod instance running on the localhost interface.
A mongorestore command that connects to an active mongod or mongos has the following prototype form:
mongorestore --port <port number> <path to the backup>
A mongorestore command that writes to data files without using a running mongod has the following prototype form:
mongorestore --dbpath <local database path> <path to the backup>
####Restore Point in Time Oplog Backup If you created your database dump using the --oplog option to ensure a point-in-time snapshot, call mongorestore with the --oplogReplay option, as in the following example:
mongorestore --oplogReplay
You may also consider using the mongorestore --objcheck option to check the integrity of objects while inserting them into the database, or you may consider the mongorestore --drop option to drop each collection from the database before restoring from backups.
####Restore a Subset of data from a Binary Database Dump mongorestore also includes the ability to a filter to all input before inserting it into the new database. Consider the following example:
mongorestore --filter '{"field": 1}'
Here, mongorestore only adds documents to the database from the dump located in the dump/ folder if the documents have a field name field that holds a value of 1. Enclose the filter in single quotes (e.g. ') to prevent the filter from interacting with your shell environment.
####Restore without a Running mongod mongorestore can write data to MongoDB data files without needing to connect to a mongod directly.
mongorestore --dbpath /srv/mongodb --journal
Here, mongorestore restores the database dump located in dump/ folder into the data files located at /srv/mongodb. Additionally, the --journal option ensures that mongorestore records all operation in the durability journal. The journal prevents data file corruption if anything (e.g. power failure, disk failure, etc.) interrupts the restore operation.
####Restore Backups to Non-Local mongod Instances By default, mongorestore connects to a MongoDB instance running on the localhost interface (e.g. 127.0.0.1) and on the default port (27017). If you want to restore to a different host or port, use the --host and --port options. For example:
mongorestore --host mongodb1.example.net --port 3017 --username user --password pass /opt/backup/mongodump-2012-10-24