MongoDB will present all user information in the collection System.users of the admin database, which mainly includes the user name, password, and database information. MongoDB does not enable authentication by default, as long as it can connect to the server, it can connect to Mongod. To enable security authentication, you need to change the registry under Windows, and then change the configuration file under Linux. Only the actions under Windows are described here.
First, create an administrator user
1 Viewing the database with show DBS, finding that the admin database could not be found
2 Create user admin
Db.createuser (
{
"User": "admin",
"pwd": "admin",
Roles:[{role: "Useradminanydatabase", DB: "admin"}]
}
)
Displays the user who created the admin successfully.
3 query All the databases, you can see the admin database is displayed
4 Querying the collection and the data in the collection
5 Turn on login verification
Enter the system registry and locate [Hkey_local_machine-->system-->currentcontrolset-->services-->mongodb] on the right-hand side of the health-value list " ImagePath "
Add--auth to the ImagePath corresponding value, i.e.
"D:\Program files\mongodb\server\3.2\bin\mongod.exe"--dbpath=e:\db "--logpath=d:\program files\mongodb\server\3.2 \logs\mongodb.log "--auth--service
After the change, restart the MongoDB service,
6 Login MongoDB with non-authentication method, and query the collection, report the error without permission
7 Login with username and password
A return value of 1 indicates that the login was successful.
8 Query collection again to display the results normally
Second, create a regular user
> Use test
> Db.createuser (
{
User: "Test1",
PWD: "Test1",
Roles: [{role: ' ReadWrite ', DB: ' Test '}]
}
)
Show user Creation success
Log in with a user name and password and query the collection
MongoDB Open User name password verification