Mongodb – I can’t see some databases even though the user has the “root” role

mongodbmongodb-3.0

I installed MongoDB 3.2 and I enabled authentication:

security:
  authorization: enabled

I created an admin user with the following command in the terminal:

mongo admin --eval 'db.createUser({user:"admin",pwd:"password",roles:["root"]})'

Then I created new database and a user for this database using the following command:

mongo -u admin -p password admin --eval 'db.getSiblingDB("shop").createUser({user:"shop_user",pwd:"password",roles:["dbOwner"]})'

Everything seem to be working. When I'm logged in with the shop_user user I can do everything in the shop database and I can only see this database. But when I'm logged in with the admin user I cannot see the shop database. Since the admin user has the root role, I assume I should be able to see all the databases in my system, but the shop database is hidden from me.

Does anybody know what is happening and how I can make the admin user see all databases?

Best Answer

This is because you have not restarted your MongoDB instance in auth mode. Restart your MongoDB in auth mode in linux like this

mongod --auth --fork --logpath /var/log/mongod.log

Reference: Enable Auth

Remember if you have enabled auth mode for your MongoDB instance, you must have put it in your code and start the mongod process in auth mode.

To simplify this (authenticating the db) use Studio 3T (MongoDB Client) but you still need to start the instance with the --auth command line option.