MongoDB Series Tutorials (iv): Set User access rights _MONGODB

Source: Internet
Author: User
Tags mongodb mysql in

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.