MongoDB security rights access control

Source: Internet
Author: User
Tags mongo shell

MongoDB3.0 Permissions 46564141

Transferred from: Http://ibruce.info/2015/03/03/mongodb3-auth/?utm_source=tuicool

MongoDB3.0 permission, nothing to say, Google Baidu out of all is wrong. Install the blind ditch first, simply can't say.

First, start MongoDB without using the-auth parameter:

Mongodb-linux-i686-3.0.0/bin/mongod-f mongodb-linux-i686-3.0.0/mongodb.conf
At this point you show DBS will see only a local database, the so-called admin does not exist.

MongoDB No fried chicken invincible User root, only the user can manage user useradminanydatabase.

Open the MONGO Shell:

Mongodb-linux-i686-3.0.0/bin/mongo
To add an administrative user:

Use admin
Db.createuser (
{
User: "Buru",
PWD: "12345678",
Roles: [{role: ' Useradminanydatabase ', db: ' admin '}]
}
)
The DB parameter in roles is required, otherwise an error will be added: Error:couldn ' t add user:missing expected field "db". In addition, there are many articles recorded is the use of Db.adduser (...) method, this method is the old version, 3.0 no longer exists, see: Http://docs.mongodb.org/manual/reference/method/js-user-management.

Switch to admin and view the user you just created:

Show Users
Or
Db.system.users.find ()
{"_id": "Admin.buru", "User": "Buru", "db": "admin", "credentials": {"Scram-sha-1": {"IterationCount": 10000, "SA LT ":" gwvwua/dxvxgshavenlyva== "," Storedkey ":" l2qevteujpkcuqdekqfiwbsv4ms= "," Serverkey ":" M1ofnkxg2sncsfrbjbx4pxbsgvg= "}}," Roles ": [{" Role ":" Useradminanydatabase "," db ":" Admin "}]}
How do I close MongoDB? Never kill-9 pid, can kill-2 pid or db.shutdownserver ()

Use the-auth parameter below to restart MongoDB:

Mongodb-linux-i686-3.0.0/bin/mongod--auth-f mongodb-linux-i686-3.0.0/mongodb.conf
Open the MONGO shell again:

Mongodb-linux-i686-3.0.0/bin/mongo
Use admin
Db.auth ("Buru", "12345678") #认证, return 1 indicates success
Or
Mongodb-linux-i686-3.0.0/bin/mongo-u buru-p 12345678--authenticationdatabase Admin
At this time

Show collections
Error

2015-03-17t10:15:56.011+0800 E QUERY Error:listcollections failed: {
"OK": 0,
"ErrMsg": "Not authorized in Admin to execute command {listcollections:1.0}",
"Code": 13
}
At Error ()
At Db._getcollectioninfoscommand (src/mongo/shell/db.js:643:15)
At Db.getcollectioninfos (src/mongo/shell/db.js:655:20)
At Db.getcollectionnames (src/mongo/shell/db.js:666:17)
At Shellhelper.show (src/mongo/shell/utils.js:625:12)
At Shellhelper (src/mongo/shell/utils.js:524:36)
At (SHELLHELP2): 1:1 at src/mongo/shell/db.js:643
Because, the user Buru only user-managed permissions.

The user is created below, the user follows the library, and the created user is

Use Tianhe
Db.createuser (
{
User: "Bao",
PWD: "12345678",
Roles: [
{role: "ReadWrite", DB: "Tianhe"},
{role: "read", DB: "Tianhe2"}
]
}
)
View the user you just created.

Show Users

{
"_id": "Tianhe.bao",
"User": "Bao",
"DB": "Tianhe",
"Roles": [
{
"Role": "ReadWrite",
"DB": "Tianhe"
},
{
"Role": "read",
"DB": "Tianhe2"
}
]
}
View all users of the entire MONGODB:

Use admin
Db.system.users.find ()

{"_id": "Admin.buru", "User": "Buru", "db": "admin", "credentials": {"Scram-sha-1": {"IterationCount": 10000, "SA LT ":" gwvwua/dxvxgshavenlyva== "," Storedkey ":" l2qevteujpkcuqdekqfiwbsv4ms= "," Serverkey ":" M1ofnkxg2sncsfrbjbx4pxbsgvg= "}}," Roles ": [{" Role ":" Useradminanydatabase "," db ":" Admin "}]}
{"_id": "Tianhe.bao", "User": "Bao", "db": "Tianhe", "credentials": {"Scram-sha-1": {"IterationCount": 10000, "SA LT ":"//xy1v1fbqehc1gzqqzhgq== "," Storedkey ":" zs/o54zzl/fdcxlqj98kdavtff0= "," Serverkey ":" Iipnyz2gk8khyk3zgz6mubt0pi4= "}}," Roles ": [{" Role ":" ReadWrite "," db ":" Tianhe "}, {" Role ":" read "," DB ":" Tianh E2 "}]}
After creation, verify that:

Use Buru
Show collections

2015-03-17t10:30:06.461+0800 E QUERY Error:listcollections failed: {
"OK": 0,
"ErrMsg": "Not authorized in Buru to execute command {listcollections:1.0}",
"Code": 13
}
At Error ()
At Db._getcollectioninfoscommand (src/mongo/shell/db.js:643:15)
At Db.getcollectioninfos (src/mongo/shell/db.js:655:20)
At Db.getcollectionnames (src/mongo/shell/db.js:666:17)
At Shellhelper.show (src/mongo/shell/utils.js:625:12)
At Shellhelper (src/mongo/shell/utils.js:524:36)
At (SHELLHELP2): 1:1 at src/mongo/shell/db.js:643
`
Obviously do not have the authority, first Auth:

Db.auth ("Bao", "12345678")
1
Show collections
News
System.indexes
Wahaha
Complete!

Reference:
Mongo Shell:http://docs.mongodb.org/v2.2/tutorial/getting-started-with-the-mongo-shell
Enable Access control:http://docs.mongodb.org/manual/tutorial/enable-authentication
Add a User to a database:http://docs.mongodb.org/manual/tutorial/add-user-to-database
User methods:http://docs.mongodb.org/manual/reference/method/js-user-management
Role methods:http://docs.mongodb.org/manual/reference/method/js-role-management

MongoDB security rights access control

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.