We know that MySQL will need us to set a database default username and password when installing,MongoDB is no exception, but MongoDB is the default does not set access restrictions, You do not need to enter a user name and password to access, but this is very insecure, we need to manually add the permission restrictions for MongoDB 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 SA and 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 ] in the right-hand list, hit "ImagePath" and add the -authas shown:
Note: After modifying the registry, be sure to manually restart the MongoDB service before it takes effect.
So when we open the mongodb Shell window again , look at the default database and "table", will be prompted to open the corresponding collections, and will error, as shown in.
When we use the previously added user login will be displayed,1 default is the user name and password is correct.
after entering, we can build our own users again. Just like the user management in MySQL, We can add and remove users from the user table in MySQL, which is the same 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.
MongoDB (iv) MONGODB set user access rights