Step 1 – Adding the MongoDB Repository
# vi /etc/yum.repos.d/mongodb-org.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
#yum repolist
Step 2 – Installing MongoDB
[root@dev ~]#yum install mongodb-org
Step 3:Start mongod service
[root@dev ~]# systemctl start mongod
Step 4:Access the database server with the MongoDB Shell
# mongo
Step 5:- Create New Database
#use DB_NAME
6: Show database list
#db :- show database
#dbs :- show database list
Step 7:- create database is not present in list . To display you need insert one document.
#db.movie.insert({"name":"testing db"})
#show dbs
Step 8 : Backup a MongoDB
#mkdir -P /var/backups/mongobackups
#mongodump --db DB_NAME --out /var/backups/mongobackups/`date +"%m-%d-%y"`
Ex :-
[root@dev ~]# mongodump --db test_DB --out /var/backup/mongo_bk/
2021-02-01T16:07:21.572+0530 writing test_DB.test_DB to
2021-02-01T16:07:21.573+0530 writing test_DB.employeeprofile to
2021-02-01T16:07:21.576+0530 done dumping test_DB.test_DB (1 document)
2021-02-01T16:07:21.576+0530 done dumping test_DB.employeeprofile (1 document)
[root@dev mongo_bk]# ls
test_DB
[root@dev mongo_bk]# cd test_DB/
[root@dev test_DB]# ls
employeeprofile.bson test_DB.bson
employeeprofile.metadata.json test_DB.metadata.json
Step 9: Backup will have an output such as:
/var/backup/mongobackups/Current_Date/DB_NAME
Step 10 : If you want to delete new database Database_Name
#use Database_Name
#db.dropDatabase()
Step 11: -Restoring & Migrate a MongoDB
[root@dev test_DB]# mongorestore --db test_DB --drop /var/backup/mongo_bk/test_DB/
2021-02-01T16:09:25.874+0530 restoring test_DB.employeeprofile from /var/backup/mongo_bk/test_DB/employeeprofile.bson
2021-02-01T16:09:25.955+0530 no indexes to restore
2021-02-01T16:09:25.955+0530 finished restoring test_DB.employeeprofile (1 document)
2021-02-01T16:09:25.955+0530 restoring test_DB.test_DB from /var/backup/mongo_bk/test_DB/test_DB.bson
2021-02-01T16:09:25.959+0530 no indexes to restore
2021-02-01T16:09:25.959+0530 finished restoring test_DB.test_DB (1 document)
2021-02-01T16:09:25.959+0530 done
#mongorestore --db newdb --drop /var/backups/mongobackups/01-20-16/newdb/
How to change mongo data path in vim /etc/mongo.conf clike here...
Step 12: Allow Remote Access to MongoDB
Open MongoDB configuration file /etc/mongod.conf and change bindIp by adding
We will check mongod is running at port or not
[root@dev mongo]# ss -plnt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:5432 *:* users:(("postgres",pid=9974,fd=3))
LISTEN 0 100 127.0.0.1:25 *:* users:LISTEN 0 128 *:22 *:* users:(("sshd",pid=1213,fd=3))
(("master",pid=14794,fd=14))
LISTEN 0 128 [::1]:953 [::]:* users:(("named",pid=14669,fd=25))
LISTEN 0 50 [::]:445 [::]:* users:
[root@dev mongo]#vim /etc/mongod.conf
# network interfaces
#net:
port: 27017
bindIp: 127.0.0.1,192.168.105.62 # Enter 0.0.0.0,:: to bind to all interfaces
[root@dev ]# firewall-cmd --zone=public --add-port=27017/tcp --permanent
[root@dev mongo]# firewall-cmd --reload
success
[root@dev ]# systemctl restart mongod
Step 13: mongod is listening on configured interfaces and show output
# netstat -tnlp
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 19757/mongod
tcp 0 0 192.168.0.100:27017 0.0.0.0:* LISTEN 19757/mongod
Now create DB:-
#use TEST_DB
#db.createUser({user: "testuser", pwd: "test", roles:[{role: "userAdminAnyDatabase", db: "TEST_DB"}]})
#mongo -u test -p test 192.168.105.118/TEST_DB
Step 14: Connect Remote login
#mongo -u ian -p secretPassword 123.45.67.89/cool_db
#Add User for Database
#db.createUser({user: "myuser", pwd: "password", roles:[{role: ["readWrite"], db: "admin"}]})
Step:1- Open the mongo shell:write this command
#mongo
Step:2- Create Database Super users
#db.createUser({user: "mongo-admin", pwd: "password", roles:[{role: "userAdminAnyDatabase", db: "admin"}]})
Step 3: - Create New user Jack with read-only permission for DB1 and also read & write permission for DB2.
#db.createUser({user: "Jack", pwd: "password", roles:[{role: "read", db: "DB1"}, {role:"readWrite", db: "DB2"}]})
Step 4:- Connect DB
# mongo -u Jack -p --authenticationDatabase DB1
Step 5:
#db.revokeRolesFromUser( "Jack",[ { role: "read", db: "DB2" } )
Step 6 : Drop User
#use DB1
db.dropUser('Jack')
Step 6: Show all databases;
# mongo
# show databases
Step 7: Show all users;
# show users
# show roles
step 8:
No comments:
Post a Comment