MongoDB security access control, domestic publishing books, online find Bowen are all using db.adduser (Username,password) to increase users. The execution of the secondary method in MongoDB 3.0 will cause an error.
elevation information indicates that the system could not find the method. The reason is that this method has been abandoned in 3.0. When using Mogodb, your show DBS will see that there is only one local database, the so-called Super admin admin is nonexistent and there is a Super Admin user useradminanydatabase. Now we're going to add a super admin to our Mogodb:
View MongoDB Official documents: Add users are using Db.createuser ()
The original text of the official document is as follows:
Db.createuser (user, Writeconcern)
creates a new user for the database where the method runs. Db.createuser () returns a Duplicate user error if the user already exists on the database.
As seen above CreateUser requires two parameters: User and Writeconcern.
Example of User:
{User: "<name>",
pwd: "<cleartext password>",
customdata: {<any information>},
roles: [
{role: "<role>", DB: "<database>"} | "<role>",
...
]
}
User only needs four fields: Username, password, user description, role, etc. Where the customdata is optional. And as for roles can write an empty is an array.
For example:
Db.createuser (
{ User: "Root",
pwd: "Toor",
roles:[]}
)
We are adding a super admin:
Use admin
db.createuser
{
User: ' Root ',
pwd: ' Toor ',
roles: [{role: ' Useradminanydatabase ", DB:" Admin "}]
}
)
As with previous versions, the user remains under the System.users collection.
View User:
Show Users
Or
Db.system.users.find () uses –auth to start MongoDB security authentication, and then we pass Db.auth in the shell client ("root", "Toor")
Executing a query at this time will cause an error
Error:error: {"$err": "Not authorized to query on Helloworld.persons", "Code": 13}
This is because we give the root user the useradminanydatabase role. This role does not have read and write permissions, only user-managed permissions.
to create a role that can read and write to a database:
Use HelloWorld
db.createuser (
{
User: "Wang",
pwd: "123456",
roles: [
{role: "ReadWrite", db : "HelloWorld"},
{role: "read", DB: "Test"}
]
}
)
The user created in this way is a read-write role in the HelloWorld, and is readonly in test. Now let's see what's roles in MongoDB 3.0:
Refer to the translated version of this MongoDB official document:
Http://www.cnblogs.com/SamOk/p/5162767.html
Reference:
https://docs.mongodb.org