MongoDB authorization and permissions 1. When you enable the MongoDB service without adding any parameters, you can perform any operations on the database and remotely access the database. If you specify the auth parameter at startup, you can perform user verification on the database. Www.2cto.com $. mongod -- enable. mongoMongoDBshellversion in authmongodb. log.
MongoDB authorization and permissions 1. When you enable the MongoDB service without adding any parameters, you can perform any operations on the database and remotely access the database. If you specify the auth parameter at startup, you can perform user verification on the database. Www.2cto.com $./mongod -- enable auth mongodb. log./mongo MongoDB shell version
MongoDB authorization and permissions
1. When you enable the MongoDB service without adding any parameters, you can perform any operations on the database and remotely access the database. If the-auth parameter is specified at startup, you can perform user verification on the database. Www.2cto.com
$./Mongod -- auth> mongodb. log & enable
./Mongo
MongoDB shell version: 1.8.1
Connecting to: test
> Show dbs
Admin (empty)
Local (empty)
2. Add a user
At the beginning of the installation, MongoDB has an admin database by default, and admin. system. users will save more user information than the user permissions set in other databases.
When admin. system. there is no user in users sometimes. Even if the -- auth parameter is added when mongod is started, if the user is not added to the admin database, no authentication is performed or any operation can be performed, in admin. system. A user is added to users.
Create a database tage and create a user for the tage:
> Use tage
Switched to db tage
> Db. addUser ("tage", "123 ")
{
"User": "tage ",
"ReadOnly": false,
"Pwd": "1f66d5c4223029536080d41febe0ec33"
}
Create a root user in the admin database:
> Use admin
Switched to db admin
> Db. addUser ("root", "123456 ")
{
"User": "root ",
"ReadOnly": false,
"Pwd": "34e5772aa66b703a319641d42a47d696"
}
3. Verify the user:
> Db. auth ("root", "123 ")
0: Password error. 0 is returned. Verification Failed.
> Db. auth ("root", "123456 ")
1. Verification Successful. 1 is returned.
The following test user permission settings:
$./Mongo login without the username and password
MongoDB shell version: 1.8.1
Connecting to: test
> Use tage
Switched to db tage
> Db. system. users. find ()
Error :{
"$ Err": "unauthorized db: tage lock type:-1 client: 127.0.0.1 ",
"Code": 10057
}
4. If you do not specify the user name and password during logon, an error is returned. The user and password are specified below
$./Mongo-uroot-p123456 specify the user and password, but do not specify the Database Name
MongoDB shell version: 1.8.1
Connecting to: test
Wed Aug 3 21:30:42 uncaught exception: login failed
Exception: login failed
Mongodb is connected to the test database by default during logon. If the database name is not specified during logon, an error is reported.
5. log on to the tage database as the user name for verification:
$./Mongo tage-utage-p123
MongoDB shell version: 1.8.1
Connecting to: tage
> Db. system. users. find () is used to operate its own database and has the permission.
{"_ Id": ObjectId ("4e394c696b50a56254359088"), "user": "tage", "readOnly": false, "pwd": "success "}
> Use admin
Switched to db admin
> Db. system. users. find () has no permission to operate other databases.
Error :{
"$ Err": "unauthorized db: admin lock type:-1 client: 127.0.0.1 ",
"Code": 10057
}
6. log on to the admin database as the root user for verification:
./Mongo admin-uroot-p123456
MongoDB shell version: 1.8.1
Connecting to: admin
> Db. system. users. find ()
{"_ Id": ObjectId ("4e394caf6b50a56254359089"), "user": "root", "readOnly": false, "pwd": "success "}
> Use tage
Switched to db tage
> Db. system. users. find () is used to operate other databases.
{"_ Id": ObjectId ("4e394c696b50a56254359088"), "user": "tage", "readOnly": false, "pwd": "success "}
7. mongodb remote user connection
Syntax structure: mongo-uusername-ppwd ServerIP: port/dbname
The default port value is 27017.
$./Mongo-uroot-p123456 192.168.2.150/admin
MongoDB shell version: 1.8.1
Connecting to: 192.168.2.150/admin
> Db. system. users. find ()
{"_ Id": ObjectId ("4e394caf6b50a56254359089"), "user": "root", "readOnly": false, "pwd": "success "}