Thursday, November 5, 2020

install mongodb 4.4 in ubuntu 16 & 18

Install Mongodb 4.4 in Ubuntu-Linux




1) Install mongodb 

$  sudo apt update

$  sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common

$ wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -



For Ubuntu_22_04_LTS

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list


For Ubuntu_20_04_LTS

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list


$ sudo apt-get update

$ sudo apt-get install -y mongodb-org

 

If you are getting Dependensis error on "Ubuntu 22" after executing  "sudo apt-get install -y mongodb-org" Command, So Please execute below commands as well.

$ wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb

$ sudo dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb

$ sudo apt-get install -y mongodb-org

 


#_Configuration file
     /etc/mongod.conf

#_Database directory
     /var/lib/mongodb

#_Log directory
    /var/log/mongodb


$ sudo systemctl enable mongod.service
$ sudo service mongod start
$ sudo service mongod status

 

Note : If Your MongoDB status Showing Failed, So run below command.

 


$ sudo rm -rf /tmp/mongodb-27017.sock
$ sudo service mongod start
$ sudo service mongod status

 

Mongo User, Database & Collection Create Command.

 

  • Create Demo User and Database with dbOwner role

use demo_db;

 db.createUser({ user: "demo_user", pwd: "91SD8870767Yg17fd67ng3fR2", roles: [{ role: "dbOwner", db: "demo_db" }] })

 

  • Secure Mongodb Connection (uncomment security)

 security:
    authorization : enabled

 

  • Connect MongoDB With Login Credential

 mongo -u "demo_user" -p "91SD8870767Yg17fd67ng3fR2" --authenticationDatabase "demo_db"

 use demo_db;

 show collection;

  • Create Demo Collection for Demo_db 

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

show collection;

db.collection_name.find()


1) Take Mongo Dump

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

1.1) Dump with Authentication

mongodump --host=localhost --port=27017 --authenticationDatabase="test_db" -u="test_user" -p="91SD8870767Yg17fd67ng3fR2"

2) Restore Mongo Dump

mongorestore -d db_name dump/db_dump

3) Drop Mongo Database

> use database_name;
> db.dropDatabase()

3) Drop Mongo Collections

> use database_name;
> show collections;
> db.collection_name.drop()

 

                      How to Connect Remotly MongoDB Server

1) Replace the BindIp string from localhost(127.0.0.1) To Publicly(0.0.0.0)

sudo vim /etc/mongod.conf

Old configuration:

  bindIp: 127.0.0.1
 
New configuration:

  bindIp: 0.0.0.0
 
2) Or if you want to add multiple public ips then use below format

  bindIp: 127.0.0.1;45.48.63.89;125.25.188.25
 
3) sudo service mongod restart

4) Make Mongo remote connection via command line tool

mongo --host 'mongodb://USER:PASSWORD@SERVER_IP:27017/DB_NAME?authSource=admin'

or you can try authSource by add  your db name too.

mongo --host 'mongodb://USER:PASSWORD@SERVER_IP:27017/DB_NAME?authSource=DB_NAME'

No comments:

Post a Comment

testing