MongoDB 3.0安全权限访问控制(Windows版)
MongoDB 3.0安全权限访问控制(Windows版)
1.首先,不使用 –auth 参数,启动 mongoDB:
mongod --dbpath "d:\mongodb\data\db" --logpath "d:\mongodb\data\log\MongoDB.log" --install --serviceName "MongoDB"
2.使用admin数据库,添加管理用户的管理员
use admin
db.createUser(
{
user: "admin",
pwd: "12345678",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
3.切换到admin下,查看刚才创建的用户:
show users
或
db.system.users.find()
4.关闭SCRAM-SHA-1认证方式
修改system.version文档里面的authSchema版本为3,初始安装时候应该是5,命令行如下:
> use admin
switched to db admin
> var schema = db.system.version.findOne({"_id" : "authSchema"})
> schema.currentVersion = 3
3
> db.system.version.save(schema)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
5.使用 –auth 参 数,重新启动 mongoDB:
mongod --dbpath "d:\mongodb\data\db" --logpath "d:\mongodb\data\log\MongoDB.log" --remove --serviceName "MongoDB"
mongod --auth --dbpath "d:\mongodb\data\db" --logpath "d:\mongodb\data\log\MongoDB.log" --install --serviceName "MongoDB"
6.打开MongoDB,使用admin,展示所有用户,会弹出以下信息
show users
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" : 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
7.加入用户验证,再展示用户,会显示所有用户
db.auth("admin","12345678")
1
show users
8.使用需要的数据库,然后创建应用数据库用户
use userdb
db.createUser({user:'bcp',pwd:'123456a',roles:[{role:'dbOwner',db:'userdb'}]})
参考地址:
http://ibruce.info/2015/03/03/mongodb3-auth/
http://21jhf.iteye.com/blog/2216103