Mongodb database account permission settings (account password)

Source: Internet
Author: User
Tags auth mongodb

For example, after mysql is installed and configured, there is a built-in mysql database with a user table to store users and user permissions. mongodb is the most relational database, is there a table like this.

1. Master the permissions and understand the following four items.

1. mongodb does not have a default administrator account. Therefore, you must first add an administrator account and enable permission authentication.
2. Switch to the admin Database. The added account is the administrator account.
3. You can only log on to the database where the user is located, including the administrator account.
4. The administrator can manage all databases, but cannot directly manage other databases. The administrator must first authenticate the admin database. This is strange.
2. Add an administrator account

The code is as follows: Copy code
[Root @ localhost test] # mongo
MongoDB shell version: 2.4.6
Connecting to: tank
> Use admin // switch to the admin database
Switched to db admin
> Show collections;
System. indexes
System. users // user table
> Db. system. users. find (); // The user table has no data.
> Db. addUser ('tank', 'test'); // add an administrator account
{
"User": "testuser ",
"ReadOnly": false,
"Pwd": "988432606980d0695e4f668f6bbc643a ",
"_ Id": ObjectId ("529e5d543b6a4608ac833429 ")
}

3. Enable user permission authentication

The code is as follows: Copy code
[Root @ localhost test] # vim/etc/mongodb. conf // remove the comment before auth = true
[Root @ localhost test] #/etc/init. d/mongod restart // restart to take effect

4. You can only log on to the database where the user is located. The administrator must pass the admin authentication before managing other databases.

The code is as follows: Copy code
[Root @ localhost test] # mongo
MongoDB shell version: 2.4.6
Connecting to: tank
> Show dbs; // display all databases failed because no authentication has been performed.
Wed Dec 4 06:39:50. 925 listDatabases failed: {"OK": 0, "errmsg": "unauthorized"} at src/mongo/shell/mongo. js: 46
> Db. auth ('tank', 'test'); // The authentication fails because the user does not belong to the tank database.
Error: 18 {code: 18, OK: 0.0, errmsg: "auth fails "}
0
> Use admin // switch to the admin database
Switched to db admin
> Db. auth ('tank', 'test'); // The authentication is successful in the admin database.
1
> Use tank; // switch to the tank database
Switched to db tank
> Show collections; // The error message "no permission" appears.
Contact
System. indexes
Users

5. Add common enable

The code is as follows: Copy code
> Use tank;
Switched to db tank
> Db. addUser ('tank1', 'test'); // adds a readable/writable user tank1 to the tank database.
{
"_ Id": ObjectId ("529e5f8474b4c660718a70f3 "),
"User": "tank1 ",
"ReadOnly": false,
"Pwd": "35dd47abff098f5b4f0b567db8edeac5"
}
> Db. addUser ('tank2', 'test', true); // added a read-only user tank2 to the tank database.
{
"User": "tank2 ",
"ReadOnly": true,
"Pwd": "1792916c544d247538ded52e6df7b887 ",
"_ Id": ObjectId ("529e67553992b24438d5e315 ")
}
> Exit // exit
Bye
[Root @ localhost zhangy] # mongo
MongoDB shell version: 2.4.6
Connecting to: tank
> Db. auth ('tank1 ', 'test'); // you can log on to the newly added user.
1

6. php client connection 1. Recommended method 1

The code is as follows: Copy code

$ Mongo = new Mongo ();

$ Db = $ mongo-> selectDB ('tank'); // switch to the tank database

$ Db-> authenticate ("tank3", "test"); // authentication

$ Users = $ db-> selectCollection ("users"); // select the users table

$ Cursor = $ users-> find (); // read data

Foreach ($ cursor as $ id => $ value ){
Echo "$ id:"; print_r ($ value); echo "<br> ";
}

This method is easy to understand, and the operation process under the root command line is similar.

2. Recommended method 2

The code is as follows: Copy code

$ Mongo = new Mongo ("mongodb: // tank3: test@127.0.0.1: 27017/tank"); // authenticate the user, the database here, only enable authentication

$ Db = $ mongo-> selectDB ('tank'); // select a database

$ Users = $ db-> selectCollection ("users ");

$ Cursor = $ users-> find ();

Foreach ($ cursor as $ id => $ value ){
Echo "$ id:"; print_r ($ value); echo "<br> ";
}

The difference between the two methods is that one is to select the database first and the other is to authenticate the database first.

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.