MongoDB Administration2 min read

Prabhin Prabharkaran Administrator
DevOps Engineer

He is a Technical professional. He is a person who loves to share tricks and tips on the Internet. He Posts what he does!

follow me

HI All , In this document we will show you how to do MongoDB administration.

MongoDB is a free and open-source cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with schemas.

Prerequisites

1. Make sure that you have installed mongodb server or please follow this link.

Administration

Enter the mongoDB console first by executing the following command.

mongo

1)Create admin role

use admin //admindb
db.users.save( {username:”admin”} ) //save Db
db.createUser({user:”adminuser”, pwd:”yourpassword”,roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ] })

Verify the admin is working or not using the following command, if verified it return 1

db.auth(“adminuser”, “yourpassword” )

2) Create Database

use database1

3) Create another user for the newly created database.

db.users.save( {username:”user”} ) //save DB
db.createUser({user:”user”, pwd:”userpass”,roles: [ { role: “readWrite”, db: “database1” } ] })

Verify the user is working or not using the following command, if verified it return 1

db.auth(“user”, “userpass” )

4) How to connect mongoDb from remote user.

Make sure that you have enable the firewall rules accordingly

mongo 192.168.10.22:27017/admin -u admin -p yourpassword

where /admin is the database needs to connect

5) MongoDb backup.

Like mysql mongodb also has the utility to dump the database.

If mongodb installed in same machine use the following command to dump.

mongodump

If you want to dump all the database from remote machine then use this

mongodump –host mongodb.example.net –port 27017

If you want to specify dump outputy directory use this

mongodump –out /data/backup/

Dump all the database with user authentication

mongodump –host mongodb1.example.net –port 27017 –username user –password “pass” –out /opt/backup/

Dump specific database use this

mongodump –db mongodevdb

NOTE: When you are dumping a databases dump folder is created and where all the dump files are stored.

Instead of backing up all the collections in a particular database, you can also backup specific collections.

# mongodump –collection person –db mongotestdb –username mongouser –password Yourpassword

6) MongoDB restore

If mongodb installed in same machine use the following command to dump.

While restoring mongoDB it will search for the dump folder in the current location , so make sure that when you are using the following command you are in the mongodb dump location

mongorestore

Use the following command to restore DB from specfic path

mongorestore /path/to/dump/

Restore the database remotely

mongorestore –host mongodb1.example.net –port 3017 –username user –password ‘pass’ /opt/backup/mongodump

© 2018, Techrunnr. All rights reserved.

#1
#2
#3
Questions Answered
Articles Written
Overall Points

Related posts

Leave a Reply