How can I create a user and grant it read-only access for all databases available in MongoDB (2.2/2.4/2.6) instance?
I am new to MongoDB, please provide the detailed, step-by-step process.
2 Answers 2
Upgrade to any one of recent MongoDB versions and then it's very easy follow the below steps:
- Enable authentication on the
/etc/mongod.conf
file
security: authorization: enabled
- Create a read user on
admin DB
, since you want to access all DBs. If specific DB then create on that specific db
use admin
db.createUser( { user: "read_admin", pwd: "yourpassword", roles: [ { role: "readAnyDatabase", db: "admin" } ] } )
- Restart the MongoDB service
sudo service mongod restart
- Login into the mongo shell and check the user
mongo -u read_admin --authenticationDatabase admin
Here are the steps to enable authorization, and create the user with the readAnyDatabase
access for the MongoDB v2.4 deployment.
- Enable Authentication
- Create a User Administrator
- Add a User to a Database - in this step, create a user with read-only access for all databases with the role readAnyDatabase - this role provides users with the ability to read data from any collection, from all logical databases in the MongoDB environment.
As an example, you can try the following tutorial from MongoDB v4.2 documentation. This tutorial Enable Access Control steps are the similar to that of the version 2.4 you want to work with. Note that, some commands, like createUser
is addUser
in v2.4, have different syntax - please be mindful of that.