MongoDB 3.0 Security Access control

Source: Internet
Author: User
Tags mongo shell

MongoDB 3.0 Security Access control

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 Admindb.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/mongouse Admindb.auth ("Buru", "12345678") #认证, returning 1 means 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 on Admin to execute command {listcollections:1.0} ',  ' code ': ' At  Error ' (<anonymous>) at  Db._getcolle Ctioninfoscommand (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) @  (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 Tianhedb.createuser ({   User: "Bao",   pwd: "12345678",   roles: [      {role: "ReadWrite", DB: "Tianhe"},< c6/>{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 Admindb.system.users.find () {"_id": "Admin.buru", "User": "Buru", "db": "admin", "credentials": {"Scram-sha-1": {"IterationCount": 10000, "salt": "gwvwua/dxvxgshavenlyva==", "Storedkey": "L2qevteujpkcuqdekqfiwbsv4ms=", " Serverkey ":" M1ofnkxg2sncsfrbjbx4pxbsgvg= "}}," Roles ": [{" Role ":" Useradminanydatabase "," db ":" Admin "}]} {" _id ":" Tianhe.bao "," User ":" Bao "," db ":" Tianhe "," credentials ": {" Scram-sha-1 ": {" IterationCount ": 10000," salt ": "//xy1v1fbqehc1gzqqzhgq==", "Storedkey": "zs/o54zzl/fdcxlqj98kdavtff0=", "Serverkey": "Iipnyz2gk8khyk3zgz6mubt0pi4 = "}}," Roles ": [{" Role ":" ReadWrite "," db ":" Tianhe "}, {" Role ":" read "," DB ":" Tianhe2 "}]}

After creation, verify that:

Use burushow collections2015-03-17t10:30:06.461+0800 E QUERY    error:listcollections failed: {  "OK": 0,  " ErrMsg ":" Not authorized on Buru to execute command {listcollections:1.0} ",  " code ": ' At  Error ' (<anonymo us>) 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") 1show Collectionsnewssystem.indexeswahaha

Complete!

MongoDB 3.0 Security 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.