Authorized users ReadWrite have read and write access to the Test1 library:
> use test1switched to db test1> db.createUser({user: "readwrite",pwd: "readwrite",customData: {description: "测试用户readwrite"},roles: [{role: "readWrite",db: "test1"}]})
Verify that the authorization is correct:
mongo -ureadwrite -preadwrite --authenticationDatabase test1MongoDB shell version: 3.2.16connecting to: test
Note: The user authorization must abide by the specification, otherwise may appear not even on the situation, to give an example
mongo -uroot -proot --authenticationDatabase adminMongoDB shell version: 3.2.16connecting to: test> db.createUser({... user: "readwrite1",... pwd: "readwrite1",... customData: {... description: "测试用户1"... },... roles: [{... role: "readWrite",... db: "test1"... }]... })
We created the readwrite1 user, which differs from the previous ReadWrite user in that it is authorized under the test library,
mongo -ureadwrite1 -preadwrite1 --authenticationDatabase test1MongoDB shell version: 3.2.16connecting to: test2018-02-08T23:17:20.762+0800 E QUERY
You can see that if--authenticationdatabase does not have an authorized library to connect to the MONGO service, there are two workarounds to avoid similar situations:
1, create the user when created under the test library, because the default connection is the test library
2, first switch to the library you want to authorize to create a user
The second approach is recommended, in any case it is best to fix it in a way that saves the cost of communication with development
MongoDB Role Table
MongoDB User Rights Management