Monday, March 1, 2021

install mongodb in ubuntu linux

MongoDB is a cross-platform document-oriented database program. and it is use NoSQL database program like JSON-format or document, and its bydefault port not is 27017 


Step 1) Install mongodb & check mongodb version, and Connect mongoDB.

$ sudo apt-get install mongodb
$ mongo --version

$ mongo

 

Step 2) Create database by following command.

$ use database_name

 

Step 3) Create user and give database access rights.


For give all db access to user

$ db.createUser({user: "root", pwd: "123456", roles:["root"]})

For give single db access to user

$ db.createUser({user: "test", pwd: "123456", roles:["dbOwner"]})

 

Step 4) Create test collection

$ db.collection_name.insert({'name': 'Elon_Musk', 'email': 'Elon_Musk@tesla.motors.com'})

 

Step 5) View collection data

$ db.collection_name.find()

 

Step 6) Restart your DB server and enable authentication, run below cmd via terminal

$ mongod --auth --port 27017 --dbpath /var/lib/mongodb

 

Step 7) For Secure mongodb login, 

If  "auth = true" syntax Exist Just Uncomment

If "security" exist then add below snippets 

security:
  authorization: enabled

$ vim /etc/mongodb.conf

$ sudo service mongodb restart

 

Step 8) Connect mongodb by following command.

$ mongo -u "root" -p "123456" --authenticationDatabase "admin" 


MongoDB Service command

sudo service mongodb status
sudo service mongodb restart
sudo service mongodb stop
sudo service mongodb start


1) Take Mongo Dump

mongodump --username user_name --password com123 --db db_name

2) Restore Mongo Dump

mongorestore -d db_name dump/db_dump

3) Drop Mongo Database

> use database_name;
> db.dropDatabase()

4) Drop Mongo User

> db.dropUser("demo_user");


No comments:

Post a Comment

testing