By default, MongoDB does not need to enter the User and password, so the client can log on. This security problem is serious.
There are also many examples on the Internet, but there are also many details that many people have not noticed me here by the way.
The following describes how to set the user name and password.
When adding a user, you must
1. If you have the relevant permissions (I will talk about it later)
2. When -- auth is not added to mongod. (If the permission is added, the following situations may occur if you add the permission)
- > Use admin
- Switched to db admin
- > Db. addUser ('Sa','Sa')
- Fri Jul 22 14:31:13 uncaught exception: error {
- "$ Err":"Unauthorized db: admin lock type:-1 client: 127.0.0.1",
- "Code"10057
- }
- >
Therefore, when adding a user, we must first add a super admin when -- auth is not added.
After the service is started, enter./mongo
- Pai_^ [root @:/usr/local/mongodb/bin] #./mongo
- MongoDB shell version: 1.8.2
- Connecting to: test
- > Use admin
- Switched to db admin
- > Db. adduser ('Sa','Sa')
- Fri Jul 22 14:34:24 TypeError: db. adduser is not a function (shell): 1
- > Db. addUser ('Sa','Sa')
- {
- "_ Id": ObjectId ("4e2914a585178da4e03a16c3"),
- "User":"Sa",
- "ReadOnly":False,
- "Pwd":"75692b1d11c072c6c79332e248c4f699"
- }
- >
In this way, it indicates that the permission has been successfully created. Then, let's try the permission.
- > Show collections
- System. indexes
- System. users
Without -- auth, you can access admin's two favorite default tables.
- > Db. system. users. find ()
- {"_ Id": ObjectId ("4e2914a585178da4e03a16c3"),"User":"Sa","ReadOnly":False,"Pwd":"75692b1d11c072c6c79332e248c4f699"}>
Created successfully.
Add the -- auth option to the service.
Go to./mongo.
- MongoDB shell version: 1.8.2
- Connecting to: test
- > Use admin
- Switched to db admin
- > Show collections
- Fri Jul 22 14:38:49 uncaught exception: error :{
- "$ Err":"Unauthorized db: admin lock type:-1 client: 127.0.0.1",
- "Code"10057
- }
- >
You can see that you have no access permission.