Configure user permissions in MongoDB
By default, apsaradb for MongoDB is set to have no access restrictions. Note: The research results are based on the Windows platform.
After successfully deploying mongodb, go to the console and enter the command mongod use admin. You will find that the database contains a system. user table, huh, huh, right. This table is equivalent to the user table in MsSql, which is used to store super administrator data, let's add a super administrator to it and try to add a super administrator user. username is admin and password is admin. Then, we have added a super administrator, let's test the connection. If you need to connect to MongoDB again, you do not need to enter the username and password. Let's quit first.
Enter the command: mongod use admin
Run the show collections command to view all the tables in the database. You will find that MongoDB does not prompt you to enter the user name and password. That's strange. What is the problem? As mentioned at the beginning of this article,
By default, apsaradb for MongoDB is set to have no access permission. In this case, we need to set it as a permission access restriction first. Let's look at the effect. How can we set it?
In the registry, find the MongoDB node. In its ImgPath, modify it and add-auth, as shown below: mongod
"D: \ Program Files \ mongodb \ bin \ mongod"-dbpath e: \ work \ data \ mongodb \ db-logpath e: \ work \ data \ mongodb \ log-auth-service
Note: Go to the system registry (WIN + R --> regedit ), find [HKEY_LOCAL_MACHINE --> SYSTEM --> CurrentControlSet --> Services --> MongoDB] and press "ImagePath" in the "Health-Value List" on the right"
After the modification is complete, go to the console again and find that password verification is required.
Enter the user name and password for verification.
Don't worry. It's not over yet.
After exiting the window, start again and enter the window. use mydatabase to switch to another database and view the tables under the database. However, if you run the preceding command to access the admin database, then verify the username and password before entering the mydatabase database. In this way, you can access the mydatabase and add the user db. addUser (username, password );
In this way, you can use the user name and password to access the mydatabase.
Similarly, in linux, you must add the auth parameter after the started service to enable access control.