MongoDB permission settings to add administrators, common user methods _mongodb

Source: Internet
Author: User
Tags auth mongodb

I know that the relational database has permissions to control, what users can access what library, what table, what users can insert, update, and some users only read permissions.

For example: MySQL installation configuration, there is a MySQL database, which has a user table, used to store users, as well as user rights, and MongoDB this most like relational database, there is no such a table.

First, to grasp the authority, understanding the following 4 basically is almost

1, MongoDB is not a default administrator account, so to add the Administrator account first, in the Open Authority certification.
2, switch to the admin database, add the account number is the administrator account.
3, users can only log in the user's database, including the administrator account.
4, the administrator can manage all databases, but can not directly manage other databases, first in the Admin database certification before you can. It's a little weird.

Second, add administrator account

Copy Code code as follows:

[Root@localhost test]# MONGO
MongoDB Shell version:2.4.6
Connecting To:tank
> Use admin//Switch to admin database
Switched to DB admin
> Show Collections;
System.indexes
System.users//user table
> Db.system.users.find (); User table has no data
> Db.adduser (' Tank ', ' test '); Add an Administrator account
{
"User": "TestUser",
"ReadOnly": false,
"pwd": "988432606980d0695e4f668f6bbc643a",
"_id": ObjectId ("529e5d543b6a4608ac833429")
}


Third, open the user authority to start authentication

Copy Code code as follows:

[Root@localhost test]# vim/etc/mongodb.conf//Remove the comments in front of Auth=true
[root@localhost test]#/etc/init.d/mongod Restart//reboot effective


Four, users can only log in the user's database, administrators need to pass the admin authentication to manage other databases

Copy Code code as follows:

[Root@localhost test]# MONGO
MongoDB Shell version:2.4.6
Connecting To:tank
> Show DBS; Failed to display all databases because there is no authentication
Wed Dec 4 06:39:50.925 listdatabases failed:{"OK": 0, "errmsg": "Unauthorized"} at src/mongo/shell/mongo.js:46
> Db.auth (' Tank ', ' test '); Authentication failed because this user does not belong to the tank database
error:18 {code:18, ok:0.0, errmsg: "Auth Fails"}
0
> Use admin//Switch to admin database
Switched to DB admin
> Db.auth (' Tank ', ' test '); Successfully authenticated in admin database
1
> Use tank; Switch to tank database
Switched to DB tank
> Show Collections; does not have permission at the prompt
Contact
System.indexes
Users


Five, add ordinary users

Copy code code as follows:

> Use tank;
Switched to DB tank
> Db.adduser (' tank1 ', ' test '); A writable user was added to the tank database Tank1
{
"_id": ObjectId ("529e5f8474b4c660718a70f3"),
"User": "Tank1",
"ReadOnly": false,
"pwd": "35DD47ABFF098F5B4F0B567DB8EDEAC5"
}
> Db.adduser (' tank2 ', ' test ', true); Added a read-only user to the tank database Tank2
{
"User": "Tank2",
"ReadOnly": true,
"pwd": "1792916c544d247538ded52e6df7b887",
"_id": ObjectId ("529e67553992b24438d5e315")
}
> Exit//Quit
Bye
[Root@localhost zhangy]# MONGO
MongoDB Shell version:2.4.6
Connecting To:tank
> Db.auth (' tank1 ', ' test '); The user you just added can log on.
1


Six, PHP Client connection

1. Recommended Method A
Copy Code code as follows:

$mongo = new MONGO ();

$db = $mongo->selectdb (' tank '); Switch to tank database

$db->authenticate ("Tank3", "Test"); Certification

$users = $db->selectcollection ("users"); Select the Users table

$cursor = $users->find (); Reading data

foreach ($cursor as $id => $value) {
echo "$id:"; Print_r ($value); echo "<br>";
}[code]


This is a better way to understand that the root command line operates at the same procedure.

2, the recommended method two

Copy Code code as follows:

$mongo = new MONGO ("Mongodb://tank3:test@127.0.0.1:27017/tank"); Certified users, here's the database, only the authentication function

$db = $mongo->selectdb (' tank '); Select Database

$users = $db->selectcollection ("users");

$cursor = $users->find ();

foreach ($cursor as $id => $value) {
echo "$id:"; Print_r ($value); echo "<br>";
}


The above two methods differ in that a first-choice database is certified, one is first authenticated in the selected database.

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.