0

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
  • 2
    What did you try so far? SO is not a "do your work" service. Jan 25, 2021 at 11:39
  • 1
    The range of MongoDB server versions mentioned all reached end of life 5-7 years ago. I would strongly recommend upgrading to a modern version of MongoDB as there have been many improvements to security, stability, and performance (with 9 major release series since 2.2). However, as others have noted you can find auth information in the relevant version of the MongoDB documentation. MongoDB 2.2 and 2.4 predated Role-Based Access Control, which was introduced in 2.6 and evolved in successive releases. Since you are new to MongoDB, I would recommend starting with the current 4.4 release.
    – Stennie
    Feb 1, 2021 at 1:01

2 Answers 2

0

Here are the steps to enable authorization, and create the user with the readAnyDatabase access for the MongoDB v2.4 deployment.

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 addUserin v2.4, have different syntax - please be mindful of that.

0

Upgrade to any one of recent MongoDB versions and then it's very easy follow the below steps:

  1. Enable authentication on the /etc/mongod.conf file
 security:
        authorization: enabled
  1. 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" } ]  
     } )
  1. Restart the MongoDB service
sudo service mongod restart
  1. Login into the mongo shell and check the user
mongo -u read_admin --authenticationDatabase admin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.