mongoDB 3.0 安全权限访问控制
2015-12-05 16:09
489 查看
MongoDB3.0权限,啥都不说了,谷歌百度出来的全是错的。先安装好盲沟,简单的没法说。
首先,不使用 —auth 参数,启动 mongoDB:
此时你 show dbs 会看到只有一个local数据库,那个所谓的admin是不存在的。
mongoDB 没有炒鸡无敌用户root,只有能管理用户的用户 userAdminAnyDatabase。
打开 mongo shell:
添加管理用户:
roles 中的 db 参数是必须的,不然会报错:Error: couldn’t add user: Missing expected field “db”。另外,有很多文章记录的是使用 db.addUser(…) 方法,这个方法是旧版的,3.0中已经不存在,详见:http://docs.mongodb.org/manual/reference/method/js-user-management。
切换到admin下,查看刚才创建的用户:
怎么关闭 mongoDB?千万不要 kill -9 pid,可以 kill -2 pid 或 db.shutdownServer()
下面使用 —auth 参 数,重新启动 mongoDB:
再次打开 mongo shell:
此时
报错
因为,用户admin只有用户管理的权限。
下面创建用户,用户都跟着库走,创建的用户都是
查看刚刚创建的用户。
查看整个mongoDB全部的用户:
创建完毕,验证一下:
显然没权限,先auth:
完毕!
参考:
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
首先,不使用 —auth 参数,启动 mongoDB:
./mongodb-linux-i686-3.0.0/bin/mongod -f mongodb-linux-i686-3.0.0/mongodb.conf |
mongoDB 没有炒鸡无敌用户root,只有能管理用户的用户 userAdminAnyDatabase。
打开 mongo shell:
./mongodb-linux-i686-3.0.0/bin/mongo |
use admin db.createUser( { user: "admin", pwd: "admin", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } ) |
切换到admin下,查看刚才创建的用户:
show users 或 db.system.users.find() |
{ "_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" } ] } |
下面使用 —auth 参 数,重新启动 mongoDB:
./mongodb-linux-i686-3.0.0/bin/mongod --auth -f mongodb-linux-i686-3.0.0/mongodb.conf |
./mongodb-linux-i686-3.0.0/bin/mongo use admin db.auth("admin","admin") #认证,返回1表示成功 或 ./mongodb-linux-i686-3.0.0/bin/mongo -u admin -p admin --authenticationDatabase admin |
show collections |
2015-03-17T10:15:56.011+0800 EQUERYError: listCollections failed: { "ok" : 0, "errmsg" : "not authorized on admin to execute command { listCollections: 1.0 }", "code" : 13 } at Error (<anonymous>) 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 |
下面创建用户,用户都跟着库走,创建的用户都是
use test db.createUser( { user: "test1", pwd: "test1", roles: [ { role: "readWrite", db: "test" } ] } ) |
show users { "_id" : "test.test1", "user" : "test1", "db" : "test", "roles" : [ { "role" : "readWrite", "db" : "test" } ] } |
use admin db.system.users.find() { "_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" } ] } { "_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" } ] } |
use admin show collections 2015-03-17T10:30:06.461+0800 EQUERYError: listCollections failed: { "ok" : 0, "errmsg" : "not authorized on buru to execute command { listCollections: 1.0 }", "code" : 13 } at Error (<anonymous>) 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 ` |
db.auth("test1","test1") 1 show collections news system.indexes |
参考:
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
- mongodb格式化输出查询结果
- Mongodb mmapv1存储引擎解析
- 启动MongoDB的授权认证校验
- Mongodb如何使用wiredTiger?
- linux下安装mongodb
- mongodb查询部分字段方法
- mongodb3.0 无法运行问题
- Mongodb分布式分片集群安装和理解
- MongoDB分组统计的一个例子
- mongodb的基本操作(crud)
- 使用NodeJS+AngularJS+MongoDB实现一个Web数据扒取-分析-展示的系统
- mongodb3.0.6的下载和安装
- mongodb 数据库操作--备份 还原 导出 导入
- 在windows下把Mongodb设置系统服务
- C++ 操作mongodb 的代码汇总
- 在 MongoDB 中执行两阶段提交
- 安装MongoDB
- MongoDB下载安装测试及使用
- 8天学通MongoDB——第八天 驱动实践