Last updated on April 29th, 2020 at 12:31 pm
You must set an admin and password before using mongDB for security reason. You may receive some error when you login to MongoDB shell.
WARNING: Access control is not enabled for the database.
# mongo
MongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ca2e8d54-ee4c-4db7-becb-163bdbbea52d") }
MongoDB server version: 4.2.6
Server has startup warnings:
2020-04-29T03:45:58.052+0700 I CONTROL [initandlisten]
2020-04-29T03:45:58.053+0700 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-04-29T03:45:58.053+0700 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2020-04-29T03:45:58.053+0700 I CONTROL [initandlisten]
---
Set up Admin in MongoDB
# mongo
use admin
db.createUser(
{
user: "mongo_root",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)
# vim /etc/mongod.conf
security:
authorization: enabled
# systemctl restart mongod
To access your database after you set admin and password
# mongo --authenticationDatabase "admin" -u "mongo_root" -p
So the warning message will be gone.
# mongo --authenticationDatabase "admin" -u "mongo_root" -p
MongoDB shell version v4.2.6
Enter password:
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("83818706-d522-4c1b-a5bf-6ef2fa8c80d1") }
MongoDB server version: 4.2.6