We know MySQL we need to set a default user name and password for the database during installation. MongoDB It's no exception, but MongoDB The default is not set access restrictions, do not need to enter the user name and password can be accessed, but this is very insecure, we need to manually MongoDB Add a permission limit command.
first set up a default account
Use Admin
db.adduser (' sa ', ' sa ')
This allows us to set up a default account with the username saand the password sa .
At this point we can still not enter the user name and password into the database, next we make the following changes.
Open the registry where MongoDB resides:
Win+r run regeditand find [hkey_local_machine-->system-->currentcontrolset-- >SERVICES-->MONGODB] On the right of the Jian-value list, hit "ImagePath", add the -authas shown:
Note: After modifying the registry, be sure to restart it manually MongoDB service so that it will take effect.
So When we open the mongodb Shell window again, view the default database and "table", we will be prompted not to open the corresponding Collections, and will error, as shown in.
when we use the previously added user login will be displayed, 1 The default is the correct user name and password.
After entering, we can build our own users again. Just like the user management in MySQL , we can add and remove users in MySQL 's user table, The same is true in MongoDB. For example, we add an admin:
Db.adduser (' admin ', ' admin ')
Note: The above is operating in the Windows environment and is consistent in Linux.
In layman's MongoDB (d) MongoDB set user access rights