Create, Backup, Restore and Migrate MongoDB on Ubuntu 18.04

Migrate MongoDB
Migrate MongoDB

Create, Backup, Restore & Migrate MongoDB

MongoDB

MongoDB is a powerful NoSQL database engine used worldwide. An important feature of MongoDB that it is very scalable, reliable, and easy to use.

In this article, we are going to perform all the necessary tasks which are useful for Sysadmins in their daily use.

MongoDB uses two formats to stores the information i.e. “.json” and “.bson [binary json]”. The JSON format is human-readable and it is easy to export the data eventually, importing the data.
These data can be managed using any tool which supports JSON format.

Below are the MongoDB tasks which we are going to learn,

  • Create Database and User
  • Backup of Database
  • Restore Database
  • Migrate the database from one server to another and Restore

 

Create User & Database

The creation of DB in mongo is quite a tricky part.

The first time when someone is creating DB in mongo getting confused due to the commands provided in mongo shell.

You can create mongo DB with “use databasename” that time DB is created but the tricky part is when you run “show dbs”.  You won’t find the database in the list.

Here, You need to add a temporary collection in a created database so that DB will be available in mongo.

Run “mongo” to start mongo shell,

$ mongo

Login with admin user,

$ use admin
$ db.auth("admin","admin")

Create a database with use command,

$ use test_db

Add a user with grant privileges on DB,

db.createUser(
 {
 user: "test",
 pwd: "test",
 roles: [{role: "readWrite", db: "test_db"}]
 }
)

Show available Databases [test_db will not be available in list],

$ show dbs
local 0.78125GB
mydb 0.23012GB

Now exit from mongo shell and re-run mongo shell to create DB with a temporary collection,

$ mongo

$ use test_db


$ db.auth("test","test")

$ db [Shows selected DB]
test_db

$db.new_collection.insert({ some_key: "some_value" })

$ show dbs [You will have DB in list]
local 0.78125GB
mydb 0.23012GB
test_db 0.13451GB

Backup MongoDB Database

The backup of MongoDB is an easy task. We have used –forceTableScan parameter while taking backup of the database.

By default, mongodump uses the _id index when scanning collections with that index is available (e.g. Views do not have any indexes). Specify --forceTableScan to direct mongodump to scan collection data without the use of the _id index.

$ mongodump --forceTableScan -h 127.0.0.1:27017 -u test -p test -d test_db -o /opt/testbackup

Restore MongoDB Database

mongorestore command is used to restore the backup of MongoDB.

Here, we restore the database from “/opt/testbackup” directory.

$ mongorestore --host=127.0.0.1 --port=27017 --username=test --password=test --authenticationDatabase=test_db --db=test_db /opt/testbackup

Migrate MongoDB Database

To migrate the database, you need to perform the commands which are provided above in the tutorial.

Run the below commands to migrate the database from one server to another,

Backup database,

$ mongodump --forceTableScan -h 127.0.0.1:27017 -u test -p test -d test_db -o /opt/testbackup

Rsync Backup database directory,

$ rsync -avzlP /opt/testbackup [email protected]:/opt/

Create a new database on other servers, run the above-provided steps.

Restore Database,

$ mongorestore --host=127.0.0.1 --port=27017 --username=new_user --password=new_user --authenticationDatabase=new_db --db=new_db /opt/testbackup

Delete MongoDB Database

To delete selected MongoDB database, run the below command,

$ mongo
$ use test_db
$ db.auth("test","test")

$ db

test_db

$ db.dropDatabase()

{ "dropped" : "test_db", "ok" : 1 }
I hope you like the post if you have any kind of queries regarding MongoDB. Please comment queries and suggestions under the comment box.
 
Thank You 🙂

Prashant

Welcome to Linuxgrow. I'm Prashant, a tech-blogger from Mumbai, India. I started Linuxgrow as a passion and to share my knowledge about technologies. Here at Linuxgrow, I write about Linux technologies, Aws Cloud, Wordpress blogging and scripting knowledge. You can read more about me at About us page. Thank You :)

You may also like...

Leave a Reply

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

Facebook