Mongodb manages SecurityandAuthenticationmongdb and provides its own authentication mechanism. If no authentication is required, the database object cannot be accessed. a single data can have multiple users, and the user permissions can be different, the following is a detailed introduction. Useadmin ---- log on to the Super Administrator db. addUser (root, abcd); {_ id: ObjectId
Mongodb manages Security and Authentication mongdb and provides its own Authentication mechanism. if you do not verify, you cannot access database objects. a single data can have multiple users, and the permissions of users can be different, the following is a detailed introduction. Use admin ---- log on to the Super Administrator db. addUser (root, abcd); {_ id: ObjectId
Mongodb Security and Authentication
Mongdb also provides its own authentication mechanism. if you do not verify, you cannot access database objects. a single data can have multiple users and different user permissions. The following describes the authentication mechanism.
> Use admin ---- log on to the Super Administrator
> Db. addUser ("root", "abcd ");
{
"_ Id": ObjectId ("4eaf58af769d879418d93191 "),
"User": "root ",
"ReadOnly": false,
"Pwd": "1a0f1c3aa1d592f490a2addc559383"
}
Creates a super user who has the maximum permissions to access any database.
> Use test
Switch switched to db test to test database
> Db. addUser ("test_user", "efgh ");
{
"User": "test_user ",
"ReadOnly": false,
"Pwd": "6076b96fc3fe6002c810268702646eec"
}
> Db. addUser ("read_only", "ijkl", true );
{
"User": "read_only ",
"ReadOnly": true,
"Pwd": "f497e180c9dc0655292fee5893c162f1"
}
The above two users are created, test_user and read_only. The difference is that read_only users only have read-only permissions.
> Use test
Switched to db test
> Db. test. find ();
Error: {"$ err": "unauthorized for db [test] lock type:-1 "}
> Db. auth ("read_only", "ijkl ");
1
> Db. test. find ();
{"_ Id": ObjectId ("4bb007f53e8424663ea6848a"), "x": 1}
> Db. test. insert ({"x": 2 });
Unauthorized
> Db. auth ("test_user", "efgh ");
1
> Db. test. insert ({"x": 2 });
> Db. test. find ();
{"_ Id": ObjectId ("4bb007f53e8424663ea6848a"), "x": 1}
{"_ Id": ObjectId ("4bb0088cbe17157d7b9cac07"), "x": 2}
The preceding example shows that no user is authenticated to log on, and no read/write operations can be performed on the database. read_only users can only query and cannot insert data, but test_user users can insert data after verifying the logon.
If show dbs still reports an error at this time, because test_user is not an admin user, it has no permission.
> Use admin
Switched to db admin
> Db. auth ("root", "abcd ");
1
> Show dbs
Admin
Local
Test
Super users can perform related operations.
All created users exist in a collection named system. users in the database at the time of creation. if you want to remove a user, such as removing test_user
> Db. auth ("test_user", "efgh ");
1
> Db. system. users. remove ({"user": "test_user "});
> Db. auth ("test_user", "efgh ");
0
Enable authentication logon: If the configuration file is used to start the server, auth = true can be added to it. if it is used to start mongod with the parameter, remember to follow -- auth.