I can create a database using a specific role for my "user1". I have tested and a user "test1" can not create a database in the mode authorization enabled. How works the creation of the database? I created a user "user1" and it can access this database "db1" only. Can I have the same user "user1" with privileges to create a new database as exists in SQL Server? I might be (completeley) wrong, but I would like an explanation concrete about this case. Thank you for any help and instruction. :)
-
What does this have to do with MongoDB?– J.D.Jan 26 at 20:23
-
I did not get your question. The objective is to find a solution how to create a database in mongodb using a user already authenticated in mode authorization enabled.– Rodrigo N MJan 26 at 20:36
-
1If this is for MongoDB, why did you tag it with SQL Server?– Brendan McCaffreyJan 26 at 21:24
-
I am sorry. Just removed it. Thank you.– Rodrigo N MJan 26 at 21:53
-
Agreed with Brendan, that's why I was confused in my question. You also mentioned SQL Server in your post's body but make no mention of MongoDB. You may want to update your post body by removing the reference to SQL Server and instead puting your objective that you mentioned in your comment above.– J.D.Jan 26 at 22:29
1 Answer
Really hard to understand your question.
Maybe your problem is this: In MongoDB you have different databases. Database admin
, config
and local
are pre-defined system databases.
Users and roles are created in databases, I think that's a quite unique behavior for MongoDB. You can create users and roles in any database, however the admin
database should be the common one.
The admin
database includes roles for administering the whole system rather than just a single database. These roles are mainly related to replica set and sharded cluster administrative functions.
Roles which are created in other database than admin
can only include privileges that apply to its database and can only inherit from other roles in its database.
A role created in the admin
database can include privileges that apply to any database or to the cluster resource, and can inherit from roles in other databases as well as the admin
database.
So, I assume user1
was not created in admin
database. Most likely you created it test
database, which is the default. Then above limitations apply.
Create user in admin
database and grant required roles:
use admin
db.runCommand( {
createUser: "user1",
pwd: "secret",
roles: [ "readWrite" ]
} )
or
db.getSiblingDB("admin").runCommand(...)
This user can create any database, and he can write into any database.
Every database provides built-In Roles read
, readWrite
, dbOwner
, dbAdmin
, userAdmin
.
Maybe you are looking for this:
use admin
db.runCommand( {
createUser: "user1",
pwd: "secret",
roles: [ { role: "dbOwner", db: "db1" } ]
} )
This commands creates the user and grant dbOwner
of database db1
. He can write into database db1
, he cannot create or read any other database.
-
For example, if I have a user with privileges to create a database and this user can see only his database and that created by him. Is it possible to have this scenario on MongoDB? Jan 26 at 23:07
-
No, it is not possible. A user is created in a database, thus you first must create the database (or at least the name of this database must be known beforehand), afterwards you can create a user in this database. If you create the user in
admin
database (or more precisely grant privilegereadWrite
scoped toadmin
), then this user can create any database. Jan 27 at 5:55 -