We know that MySQL in the installation of the need for us to set a database default username and password, MongoDB is no exception, but MongoDB is the default does not set access restrictions, do not need to enter a username and password can be accessed, but this will be very unsafe, We need to manually add permission restrictions to the MongoDB command.
First, create a default account
Copy Code code as follows:
Use admin
Db.adduser (' sa ', ' sa ')
This allows us to set up a default account with the user named SA and the password SA.
At this point we can still not enter the user name and password into the database, and then we do the following changes.
Open the registry where MongoDB resides:
Win+r run regedit, find
Copy Code code as follows:
[Hkey_local_machine-->system-->currentcontrolset-->services-->mongodb]
Hit "ImagePath" in the list of values on the right, add-auth, as shown in the following figure:
Note: After modifying the registry, be sure to manually restart the MongoDB service, which will take effect.
So when we open the MongoDB shell window again, look at the default database and "table", you will be prompted not to open the corresponding collections, and will be the error, as shown in the following figure.
When we use the previously added user login will be displayed, 1 the default username and password is correct.
After entering, we can build our own users. As in user management in MySQL, we can add and remove users in the MySQL user table, as well as in MongoDB. For example, we add an admin:
Copy Code code as follows:
Db.adduser (' admin ', ' admin ')
Note: The above is operated in Windows environment and is consistent in Linux.
The next article on MongoDB Database Basic grammar Learning