How to configure authentication for MongoDB when using mongo Shell or any other sort of client?
How to Setup Authentication for Connections on MongoDB
mongodb
Related Question
- MongoDB LDAP – How to Bypass LDAP for MongoDB Users
- MongoDB – How to View All Documents in a Collection Through Mongo Shell
- Mongodb – Authentication failed on secondary mongodb
- Mongodb – How to authenticate mongodb server identity via mongodb shell
- MongoDB Connection – Self-Signed Server Without PEM File
- Mongodb – 3 Node mongo db authentication setup fails
- MongoDB – How to Generate the Root CA.pem for SSL Certificates
- MongoDB – How to Disable MongoDB Shell Logs
Best Answer
Here is a complete solution, including creation of a "super user", capable to access any resources and perform any operation.
Notice
First: the first access to MongoDB is made without authentication (
$ mongo
).Second: the configuration file of MongoDB may differ, depending on which version are you using (3.x: mongod.conf / 2.x: mongodb.conf)
Creating User
Verifying User Creation and Authentication
Enabling Authentication
check which config file is using MongoDB
MongoDB 3.x
MongoDB 2.x
Restart MongoDB (SysV script)
Connecting via Mongo Shell
Notice: you can just hit
mongo
and open the Mongo Shell, but it will not let you perform any action, due to the fact that you were not previously authenticated.For dropping a user:
References: