MongoDb Authentication Setup and Mongo Dump2 min read

Hi All, this document deals with how to set up MongoDB authentication and perform MongoDB dumps.

  •  Prerequisites
  • check the status of the MongoDB
    systemctl status mongodb

    If the service is down, bring it up

  • starting mongodb service
    systemctl start mongod
  • For authentication setup we need to create user and passwords for MongoDB, so connect to the mongo shell
    mongo

  • connect to the admin DB and then Create a user and password
    > use admin
    
    
    > db.createUser({ user: "enter user name" , pwd: "Enter your password", roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase"]});

    It will give an output as shown below

    Successfully added user: {
    	"user" : "techrunnr",
    	"roles" : [
    		"userAdminAnyDatabase",
    		"dbAdminAnyDatabase",
    		"readWriteAnyDatabase"
    	]
    }
  • Authentication Setup. Go to mongod.conf (/etc/mongod.conf) file, to the security section and add authorization line
    security:
      authorization: enabled
  • Now restart the MongoDB service
    systemctl restart mongod
  • login to the mongo shell using below command, and it will prompt for a password
    mongo -u <username> --authenticationDatabase "admin" -p
    
    
    mongo -u techrunnr --authenticationDatabase "admin" -p

     

  • Now create a DB and insert some queries, If you insert the queries then only it shows the DB, exit the mongo shell
    > use techrunnr
    
    >db.techrunnr.insert( { item: "card", qty: 15 } )
    
    >show dbs
    
    
    >exit

  • Now mongo Dump, create a directory where you want to store the dump
    mkdir /opt/mongo_dump
  • Now we are taking the dump of techrunnr DB and storing at /opt/mongo_dump
    mongodump --db <db_name> --username <user_name> --password <enter_password> --authenticationDatabase admin --out /opt/mongod_dump
    
    mongodump --db techrunnr --username techrunnr --password ******** --authenticationDatabase admin --out /opt/mongod_dump

 

We need your support!!
Other Amount:
techrunnr.com:
#1
#2
#3
Questions Answered
Articles Written
Overall Points

You may also like...

Leave a Reply