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?
- [Email protected] zhangy]# 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 no data
- > Db.adduser (' tank ',' test '); //Add an Administrator account
- {
- "user": "Tank",
- "ReadOnly": false,
- "pwd": "988432606980d0695e4f668f6bbc643a",
- "_id": ObjectId ("529e5d543b6a4608ac833429")
- }
Third, open the user rights authentication
View copy print?
- [[email protected] zhangy]# vim/etc/mongodb.conf //Remove the comments from the front of the Auth=true
- [[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?
- [Email protected] zhangy]# MONGO
- MongoDB Shell version:2.4.6
- Connecting To:tank
- > Show DBS; //Show all databases failed 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 '); //Success in Admin database authentication
- 1
- > Use tank; //Switch to tank database
- Switched to DB tank
- > Show Collections; //Do not have permission at the prompt
- Contact
- System.indexes
- Users
Five, add normal with the start
View copy print?
- > Use tank;
- Switched to DB tank
- > Db.adduser (' Tank1 ',' test '); //Added a read-write user 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 //Exit
- Bye
- [Email protected] zhangy]# MONGO
- MongoDB Shell version:2.4.6
- Connecting To:tank
- > Db.auth (' Tank1 ',' test '); //The user just added can log in.
- 1
VI, PHP Client connection
1, recommended method one
View copy print?
- $mongo = new MONGO ();
- $db = $mongo->selectdb (' tank '); //Switch to tank database
- $db->authenticate ("Tank3", "test"); //Certification
- $users = $db->selectcollection ("users"); //Select Users table
- $cursor = $users->find (); //Read Data
- foreach ($cursor as $id = = $value) {
- echo "$id:"; Print_r ($value); echo "<br>";
- }
This is a good way to understand, the root command line under the operation process is similar.
2, recommended method two
View copy print?
- $mongo = new Mongo ("Mongodb://tank3:[email Protected]:27017/tank"); Authenticated Users, the database here, only the authentication function
- $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 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