Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Roles - user access multiple databases

I would like to give acces to a user to 2 databases in MongoDB.

I tried this, but it only give access to the admin db.

use admin;
db.runCommand(
{
    createUser: "myuser",
    pwd : "mypwd",
    roles: 
    [
        { role: "readWrite", db: "db1" } ,
        { role: "readWrite", db: "db2" } 
    ]
});

I tried to create the user on each db, but i end up with 2 users: user@db1 and user@db2

Any suggestions?

like image 496
Mouette Avatar asked Feb 19 '16 13:02

Mouette


People also ask

Which roles may be used to administrate MongoDB databases?

The database owner can perform any administrative action on the database. This role combines the privileges granted by the readWrite , dbAdmin and userAdmin roles.

How do I give a database access to a user in MongoDB?

To create users for a sharded cluster, connect to a mongos instance and add the users. To authenticate as a user created on a mongos instance, you must authenticate through a mongos instance. In sharded clusters, MongoDB stores user configuration data in the admin database of the config servers.

Can MongoDB have multiple databases?

mongoDB database : Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.

How do I grant a role in MongoDB?

MongoDB: db.grantRolesToUser() method is used to grants an additional role and its privileges to a user. The name of the user to whom to grant roles. An array of additional roles to grant to the user. The level of write concern for the modification.


1 Answers

Users can basically be saved in any database, which is why you can provide --authenticationDatabase on the command line tools, for example.

Taking the example for the cli, your command line should look something like this

mongo yourhost:yourport/db1 -u myuser --authenticationDatabase admin -p

and

mongo yourhost:yourport/db2 -u myuser --authenticationDatabase admin -p 

respectively, where you obvisouly have to substitute yourhost and yourport for actual values.

like image 132
Markus W Mahlberg Avatar answered Oct 05 '22 10:10

Markus W Mahlberg