Mongodb basic administration command- Create DB, Create User, Assign Permission, restrict access
MongoDB:
MongoDB is an open-source document database that provides high performance, high availability, and automatic scaling. MongoDB obviates the need for an Object Relational Mapping (ORM) to facilitate development.- Basic MongoDB admin related commands:
Run below command mongo to check mongodb version
root@US16:~# mongo
MongoDB shell version v3.6.0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.0
2-List all the database: To list all the DBs hit command show dbs;
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
>
3-Enable MongoDB authentication:
By default, mongodb installation have open access to the shell. that mean anyone can run the command and modify the dbs.
By default, mongodb installation have open access to the shell. that mean anyone can run the command and modify the dbs.
Let's enable authentication : Follow the steps below
3A-Create a user with full privileges: Run the following command
use admin
db.createUser({user:"admin", pwd:"redhat", roles:[{role:"root", db:"admin"}]})
Command output would be like this:
Now exit from the mongo shell- type command exit
3B- Stop mongod service
root@US16:~# systemctl stop mongod.service
3C-Modify /lib/systemd/system/mongod.service :
root@US16:~# vi /lib/systemd/system/mongod.service
Append --quiet --auth syntax in line start with
ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf
# file size
LimitFSIZE=infinity
Your ExecStart line should be like
Save and Exit from the file
4- Reload service daemon: Run the command below
root@US16:~# systemctl daemon-reload
5- Start mongo service:Run the command below
root@US16:~# systemctl start mongod.service
6- Check mongo service status: We need to validate mongo service after applied the changes let's check mongo service status using below command.
root@US16:~# systemctl status mongod.service
7- Everything look good and mongo service is back authentication has enabled, let validate access.
7A- Access mongo shell with authentication
root@US16:~# mongo -u root -p
We are successfully able to login with username and password!
8- List all users: Login to mongo shell with admin user
select database admin
select database admin
> use admin
Execute the following command show users; to list users
> show users;
{
"_id" : "test.admin",
"user" : "admin",
"db" : "test",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
{
"_id" : "test.amar",
"user" : "amar",
"db" : "test",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
>
In the above out there are two user available highlighted in red color
9- Check current selected database:
To check current select database use command db
> db
admin
>
10- Check current selected database's collection:
To check current select database's collection use command show collections
> show collections
system.users
system.version
>
11- Update password for existing user:
Use the following command to update existing user's password
> use admin
switched to db admin
> db.changeUserPassword("amar", "newpassword")
>
Where "use admin" command to select database and db.changeUserPassword("username","Newpassword")