In MongoDB, the service start by default is not permission authentication, in terms of security, this is certainly not possible, so need to add permission validation.
Since it is necessary to do permission verification, it must have a user, so the first step in authorization authentication is to add users to the MongoDB library.
Start the MongoDB server, and then start the MongoDB client, using commands from the client to add users to the MongoDB library, see the code:
[SQL]View Plaincopy
- > Db.adduser ("sa","123");
- {
- "user": "sa",
- "ReadOnly": false,
- "pwd": "F7559CFE0BC0A305F82C0D83664538FD"
- }
tip: Adding users to the admin library is global, which means adding users to the admin, which is also applicable in other libraries.
To add permission validation to the server side of MongoDB, you need to add the--auth parameter when starting MongoDB service, the code is as follows:
[SQL]View Plaincopy
- D:\mongodb\bin>mongod --dbpath d:\mongodb\data--logpath d:\mongodb\log\mongodb.log--logappend--servicename MongoDB--auth--install
- All output going to:d:\mongodb\log\mongodb.log
- Creating service MongoDB.
- Service creation successful.
- Service can is started from the command line via ' net start ' MongoDB '.
- D:\mongodb\bin>net start "MongoDB"
- Mongo DB Service is starting.
- The Mongo DB service has started successfully.
Here I start the MongoDB service as a service, the first command is to install the service, and the second one is to start the service.
Service has been started, then left open the client, this is simple, look at the code:
[SQL]View Plaincopy
- D:\mongodb\bin>mongo
- MongoDB Shell version:1.8.2
- Connecting to:test
- > Show DBS;
- Mon Oct 21:54:13 uncaught exception:listdatabases failed:{
- " assertion": "unauthorized db:admin lock Type:-1 client:127.0.0.1",
- "Assertioncode": 10057,
- " errmsg": "DB assertion Failure",
- "OK": 0
- }
See, Show DBS Command reported abnormal, and then look at the specific information of the exception, unauthorized db:admin lock, permission verification does not pass AH!
Well, here is the authorization authentication, here nagging, in the authentication of permissions, the first exit the client, or another client, the code is as follows:
[SQL]View Plaincopy
- D:\mongodb\bin>mongo admin-u sa-p 123
- MongoDB Shell version:1.8.2
- Connecting to:admin
- > Show DBS;
- Admin 0.078125GB
- Local (empty)
- Test (empty)
Command format: MONGO [database name]-u [user name]-p [Password]
The show DBS above can execute and see the results.
In this way, the process of permission validation is complete.
MongoDB Permission Validation