A detailed description of MongoDB user rights settings

Source: Internet
Author: User
Tags mongodb vars

Original address:http://blog.51yip.com/nosql/1575.html

I know that relational databases are privileged, what users can access what libraries, what tables, what users can insert, update, and some users have only Read permissions.

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

First, grasp the authority, understand the following 4 is basically the same

1,mongodb is not the default administrator account, so you have to add the Administrator account, in the authorization authentication.

2, switch to the admin database, add the account is the Administrator account.

3, the user can only log in the user's database, including the administrator account.

4, the administrator can manage all the databases, but not directly manage the other databases, the admin database must be certified before you can. That's a bit odd.

Second, add admin account

View copy print?
  1. [Email protected] zhangy]# MONGO
  2. MongoDB Shell version:2.4.6
  3. Connecting To:tank
  4. > Use admin //Switch to admin database
  5. Switched to DB admin
  6. > Show Collections;
  7. System.indexes
  8. System.users //user table
  9. > Db.system.users.find (); //user table no data
  10. > Db.adduser (' tank ',' test '); //Add an Administrator account
  11. {
  12. "user": "Tank",
  13. "ReadOnly": false,
  14. "pwd": "988432606980d0695e4f668f6bbc643a",
  15. "_id": ObjectId ("529e5d543b6a4608ac833429")
  16. }

Third, open the user rights authentication

View copy print?
    1. [[email protected] zhangy]# vim/etc/mongodb.conf //Remove the comments from the front of the Auth=true
    2. [[email protected] zhangy]#/etc/init.d/mongod restart //restart effective

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

View copy print?
  1. [Email protected] zhangy]# MONGO
  2. MongoDB Shell version:2.4.6
  3. Connecting To:tank
  4. > Show DBS; //Show all databases failed because there is no authentication
  5. Wed Dec 4 06:39:50.925 listdatabases failed:{ "OK": 0, "errmsg": "Unauthorized"} at Src/mongo/shell/mongo. js:46
  6. > Db.auth (' tank ',' test '); //Authentication failed because this user does not belong to the tank database
  7. error:18 {code:18, ok:0.0, errmsg: "auth fails"}
  8. 0
  9. > Use admin //Switch to admin database
  10. Switched to DB admin
  11. > Db.auth (' tank ',' test '); //Success in Admin database authentication
  12. 1
  13. > Use tank; //Switch to tank database
  14. Switched to DB tank
  15. > Show Collections; //Do not have permission at the prompt
  16. Contact
  17. System.indexes
  18. Users

Five, add normal with the start

View copy print?
  1. > Use tank;
  2. Switched to DB tank
  3. > Db.adduser (' Tank1 ',' test '); //Added a read-write user to the tank database Tank1
  4. {
  5. "_id": ObjectId ("529e5f8474b4c660718a70f3"),
  6. "user": "Tank1",
  7. "ReadOnly": false,
  8. "pwd": "35DD47ABFF098F5B4F0B567DB8EDEAC5"
  9. }
  10. > Db.adduser (' tank2 ',' test ', true); //Added a read-only user to the tank database tank2
  11. {
  12. "user": "Tank2",
  13. "ReadOnly": true,
  14. "pwd": "1792916c544d247538ded52e6df7b887",
  15. "_id": ObjectId ("529e67553992b24438d5e315")
  16. }
  17. > exit //Exit
  18. Bye
  19. [Email protected] zhangy]# MONGO
  20. MongoDB Shell version:2.4.6
  21. Connecting To:tank
  22. > Db.auth (' Tank1 ',' test ');  //The user just added can log in.
  23. 1

VI, PHP Client connection

1, recommended method one

View copy print?
  1. $mongo = new MONGO ();
  2. $db = $mongo->selectdb (' tank '); //Switch to tank database
  3. $db->authenticate ("Tank3", "test"); //Certification
  4. $users = $db->selectcollection ("users"); //Select Users table
  5. $cursor = $users->find (); //Read Data
  6. foreach ($cursor as $id = = $value) {
  7. echo "$id:"; Print_r ($value);  echo "<br>";
  8. }

This is a good way to understand, the root command line under the operation process is similar.

2, recommended method two

View copy print?
  1. $mongo = new Mongo ("Mongodb://tank3:[email Protected]:27017/tank"); Authenticated Users, the database here, only the authentication function
  2. $db = $mongo->selectdb (' tank '); //Select a database
  3. $users = $db->selectcollection ("users");
  4. $cursor = $users->find ();
  5. foreach ($cursor as $id = = $value) {
  6. echo "$id:"; Print_r ($value);  echo "<br>";
  7. }

The difference between the above two methods is that a first select the database in the authentication, a first authentication in the selected database.

A detailed description of MongoDB user rights settings

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.