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?
The database owner can perform any administrative action on the database. This role combines the privileges granted by the readWrite , dbAdmin and userAdmin roles.
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.
mongoDB database : Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With