Windows and MongoDB configure the user permission instance

  • 2020-05-14 05:17:09
  • OfStack

Note: the research results are based on the Windows platform

After successfully deploying mongodb, enter console: \ enter command: mongod use admin, you will find the DB. Contains 1 system user table, ha ha, yes, the table is equivalent to the users table in MsSql, used to store the super administrator, then we add it in a super administrator try I add a super administrator user, username admin, also for admin password, since we added the super administrator, then let's just to test, see let's connect again MongoDB need prompt for user name, password, Let's get out of here.

Enter the command: mongod use admin

Enter the command: show collections, look at all the tables in the library, you will find that MongoDB did not prompt you to enter the user name, password, that is strange, what is this? As mentioned at the beginning of the article,

By default, MongoDB is set to no access restrictions, so let's set it to required access restrictions, and then let's look at the effect. How to set it?

In the registry, find the node of MongoDB, in its ImgPath, we modify 1 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: enter the system registry (WIN+R--) > regedit), find [HKEY_LOCAL_MACHINE - > SYSTEM-- > CurrentControlSet-- > Services-- > Type "ImagePath" in the list of health - values on the right

After the modification is completed, you enter the console again and find that you need to verify your password.
Don't forget to restart the current service
Enter a username and password to verify it.
Don't worry, it's not over yet.
When you exit the window, start again to enter the window, switch another library through use mydatabase, check the table below the library, and find that there is no permission again. However, it is not a problem to enter the admin library through the previous command, and then verify the user name and password before entering the mydatabase library. So we can go into the mydatabase library and add the user db.addUser (username,password);
This gives you access to the mydatabase library by username and password.


Related articles: