MongoDB 3.0+ 安全权限访问控制

来源:互联网 发布:淘宝信誉积分怎么算 编辑:程序博客网 时间:2024/06/04 23:33

转自:http://blog.csdn.net/lk10207160511/article/details/50281883


MongoDB3.0+的权限,网上搜出来的解决方法都是3.0以下的版本的,所以不适合3.0+以上的版本,由于这版本改变的有些大,解决了很久,终于解决,下面把解决的步骤以及思路分享给大家。

一,不使用 --auth

1.首先,不使用--auth参数启动MongoDB:

[plain] view plain copy
 print?
  1. ./mongodb-linux-i686-3.0.0/bin/mongod -f mongodb-linux-i686-3.0.0/mongodb.conf  

2.接着,在命令窗口中输入:

[plain] view plain copy
 print?
  1. show dbs  

看到只有一个local数据库,admin是不存在的(这是3.0以上版本改变了的),我们需要自己给他创建个admin数据库。

3.打开 mongo shell:

[plain] view plain copy
 print?
  1. ./mongodb-linux-i686-3.0.0/bin/mongo  
4.添加管理用户

[sql] view plain copy
 print?
  1. use admin  
  2. db.createUser(  
  3.   {  
  4.     user"admin",  
  5. pwd: "admin",  
  6.     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]  
  7.   }  
  8. )  

roles 中的 db 参数是必须的,不然会报错:Error: couldn’t add user: Missing expected field “db”。另外,有很多文章记录的是使用 db.addUser(…) 方法,这个方法是旧版的,3.0中已经不存在。详细:点击打开链接

5.切换到admin下,查看刚才创建的用户:

[sql] view plain copy
 print?
  1. show users  
  2. 或  
  3. db.system.users.find()  
[html] view plain copy
 print?
  1. { "_id" : "admin.buru", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "gwVwuA/dXvxgSHavEnlyvA==", "storedKey" : "l2QEVTEujpkCuqDEKqfIWbSv4ms=", "serverKey" : "M1ofNKXg2sNCsFrBJbX4pXbSgvg=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }  
怎么关闭 mongoDB?千万不要 kill -9 pid,可以 kill -2 pid 或 db.shutdownServer()


二,使用--auth

1.使用--auth参数启动MongoDB:

[sql] view plain copy
 print?
  1. ./mongodb-linux-i686-3.0.0/bin/mongod --auth -f mongodb-linux-i686-3.0.0/mongodb.conf  
2.再次打开 mongo shell:

[sql] view plain copy
 print?
  1. ./mongodb-linux-i686-3.0.0/bin/mongo  
  2. use admin  
  3. db.auth("admin","admin") #认证,返回1表示成功  
  4. 或  
  5. ./mongodb-linux-i686-3.0.0/bin/mongo -u admin -p admin --authenticationDatabase admin  

此时

[sql] view plain copy
 print?
  1. show collections  
报错

[plain] view plain copy
 print?
  1. 2015-03-17T10:15:56.011+0800 EQUERYError: listCollections failed: {  
  2. "ok" : 0,  
  3. "errmsg" : "not authorized on admin to execute command { listCollections: 1.0 }",  
  4. "code" : 13  
  5. }  
  6.   at Error (<anonymous>)  
  7.   at DB._getCollectionInfosCommand (src/mongo/shell/db.js:643:15)  
  8.   at DB.getCollectionInfos (src/mongo/shell/db.js:655:20)  
  9.   at DB.getCollectionNames (src/mongo/shell/db.js:666:17)  
  10.   at shellHelper.show (src/mongo/shell/utils.js:625:12)  
  11.   at shellHelper (src/mongo/shell/utils.js:524:36)  
  12.   at (shellhelp2):1:1 at src/mongo/shell/db.js:643  

因为,用户admin只有用户管理的权限。


3.下面创建用户,用户都跟着库走,创建的用户都是

[sql] view plain copy
 print?
  1. use test  
  2. db.createUser(  
  3.  {  
  4.    user"test1",  
  5. pwd: "test1",  
  6.    roles: [  
  7.       { role: "readWrite", db: "test" }  
  8.    ]  
  9.  }  
  10. )  
4.查看刚刚创建的用户

[sql] view plain copy
 print?
  1. show users  

[html] view plain copy
 print?
  1. {  
  2. "_id" : "test.test1",  
  3. "user" : "test1",  
  4. "db" : "test",  
  5. "roles" : [  
  6.     {  
  7. "role" : "readWrite",  
  8. "db" : "test"  
  9.     }  
  10.   ]  
  11. }  
5.查看整个mongoDB全部的用户:

[sql] view plain copy
 print?
  1. use admin  
  2. db.system.users.find()  

[html] view plain copy
 print?
  1. use admin  
  2. db.system.users.find()  
  3.   
  4. { "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "gwVwuA/dXvxgSHavEnlyvA==", "storedKey" : "l2QEVTEujpkCuqDEKqfIWbSv4ms=", "serverKey" : "M1ofNKXg2sNCsFrBJbX4pXbSgvg=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }  
  5. { "_id" : "test.test1", "user" : "test1", "db" : "test", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "//xy1V1fbqEHC1gzQqZHGQ==", "storedKey" : "ZS/o54zzl/FdcXLQJ98KdAVTfF0=", "serverKey" : "iIpNYz2Gk8KhyK3zgz6muBt0PI4=" } }, "roles" : [ { "role" : "readWrite", "db" : "test" } ] }  

6.创建完毕,验证一下:

[sql] view plain copy
 print?
  1. use admin  
  2. show collections  
[plain] view plain copy
 print?
  1. 2015-03-17T10:30:06.461+0800 EQUERYError: listCollections failed: {  
  2. "ok" : 0,  
  3. "errmsg" : "not authorized on buru to execute command { listCollections: 1.0 }",  
  4. "code" : 13  
  5. }  
  6.   at Error (<anonymous>)  
  7.   at DB._getCollectionInfosCommand (src/mongo/shell/db.js:643:15)  
  8.   at DB.getCollectionInfos (src/mongo/shell/db.js:655:20)  
  9.   at DB.getCollectionNames (src/mongo/shell/db.js:666:17)  
  10.   at shellHelper.show (src/mongo/shell/utils.js:625:12)  
  11.   at shellHelper (src/mongo/shell/utils.js:524:36)  
  12.   at (shellhelp2):1:1 at src/mongo/shell/db.js:643  

7.显然没权限,先auth:

[sql] view plain copy
 print?
  1. db.auth("test1","test1")  
  2. 1  
  3. show collections  
  4. news  
  5. system.indexes  


完毕!

启动指令:

不带权限:

/usr/local/mongodb/bin/mongod --dbpath=/mnt/install/mongodb/data --logpath=/mnt/install/mongodb/logs/mongodb.log --port=27017 --fork

带权限:

/usr/local/mongodb/bin/mongod --dbpath=/mnt/install/mongodb/data --logpath=/mnt/install/mongodb/logs/mongodb.log --port=27017 --auth --fork

0 0