Reputation: 573
I hava tried to create a user in database "admin" with roles of "root","readWriteManyDatabase" in mongodb to connect to different databases, but it seems that i can not use
this user to directly connect to the other databases, but i can do operations on other databases after run db.auth()
in database admin.
So, is there any way i can use to connect directly to different databases with a same user but do not need to first connect to database "admin"
Upvotes: 1
Views: 2623
Reputation: 14456
Make sure you're authentication with the admin database when connecting to the database server, this can be specified on the MongoDB Connection string by passing a query string parameter of authSource
. For example:
mongodb://username:password@my-server/my-db?&authSource=admin
If you don't specify an auth source then it will try to authenticate using the database specified, in this case my-db
If you're using the shell you can specify this as an arugment of authenticationDatabase
for example:
mongo localhost -u user -p password --authenticationDatabase admin
Upvotes: 3