MongoDB Installation, backup & restore

MongoDB Installation

MongoDB is a database system like MySQL but it has no table structure like MySQL, it is also called No SQL database, it uses JSON like documents and dynamic schemas, making the integration of data in certain kind of application easier and faster.

MongoDB provides high performance, high availability, and easy scalability.

In MongoDB the hierarchy can be simplified as :

A database holds a set of collections

A collection holds a set of documents

A document is a set of fields

A field is a key-value pair

A key is a name (string)

A value is a basic type like string, integer, float, times tamp, binary, etc.,

a document, or

an array of values

MongoDB port 27017

MongoDB Installation

First add the necessary repo

# vi /etc/yum.repos.d/10gen-mongodb.repo

64 bit

[10gen] name=10gen Repository

32 bit

[10gen] name=10gen Repository

# yum install mongo-10gen mongo-10gen-server

This will install mongoDB server

# service mongod start

# chkconfig mongod on

Starts mongod service and starts the same on system boot.

# mongo –version

Check MongoDB version

# mongo

Login to mongo shell

Configuration file

# vi /etc/mongod.conf

#port = 27017

Create database

> use new-database

check current database

> db

To show all databases

> show dbs

Add users to MongoDB server

> use admin

> db.addUser(‘testuser’,’testuser123′)

This will use database admin and create user ‘testuser’ with password ‘testuser123’

Remove user


Authenticate user

> db.auth(‘testuser’,’testuser123’)

To find users

>  db.system.users.find()

Start mongoDB with user authentication

# mongod –auth

Create user with roles

> use admin

> db.createUser(


user: “testuser2″,

pwd: “password”,

roles: [ “readWrite”, “dbAdmin” ]



This will create user testuser2 under database admin with the specified roles.

Backup database

Stop database server

# service mongod stop

Go to your backup directory

# cd /backup

# mongodump –dbpath /var/lib/mongo/

Start mongodb server, backup takes as dump

Backup database without stopping mongodb server

# service mongod start

# cd /backup

# mongodump –db admin –username testuser –password test123

Backup a collection

# mongodump –collection employee –db admin –username testuser –password testuser123

Backup a remote mongodb server

# mongodump –host –port 27017 –db admin –username testuser –password testuser123

Restore database

# service mongod stop

# cd /backup

# mongorestore –dbpath /var/lib/mongo dump

Restore without stopping mongodb service

# mongorestore –dbpath /var/lib/mongo –db admin dump/admin

Restore to an already existing database by dropping the collection first, if present

# mongorestore –dbpath /var/lib/mongo –db admin –drop dump/admin

Restore to a remote server

# mongorestore –host –port 27017 –db admin –username testuser –password testuser123 –drop /backup/dump

Hope you find this page useful.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts