现在的位置: 首页mongodb, Storage>正文
mongodb 3.0.3 创建user以及数据库
2015年06月15日 mongodb, Storage 暂无评论 ⁄ 被围观 2,049 view+

1.先在mongodb.conf 去除auth=true

mongodb加入了SCRAM-SHA-1校验方式,需要第三方工具配合进行验证,下面给出具体解决办法:
首先关闭认证,修改system.version文档里面的authSchema版本为3,初始安装时候应该是5,命令行如下

如果不关闭,在客户端连接的时候,mongodb日志会报如下错误:Failed to authenticate xxx@xx with mechanism MONGODB-CR: AuthenticationFailed MONGODB-CR credentials missing in the user document

//进入mongodb
/usr/local/mongodb/bin/mongo

> 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 })

//如果你原来已经创建了用户先然后后在重新创建
> use admin
switched to db admin
> db.system.users.find()
[...]
{ "_id" : "jiunile.jiunile", "user" : "jiunile", "db" : "jiunile", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "XXXXXXXXXXXXXXXXXXXXXXXX", "storedKey" : "XXXXXXXXXXXXXXXXXXXXXXXXXXX", "serverKey" : "XXXXXXXXXXXXXXXXXXXXXXXXXXX" } }, "roles" : [ { "role" : "dbOwner", "db" : "jiunile" } ] }

//解决方式就是删除刚刚创建的用户,重新重建即可:
> use jiunile
switched to db jiunile
> db.dropUser("jiunile")
true
>db.createUser({user: "xxx",pwd: "12345678",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})

//创建跟随库走的用户
>use jiunile
>db.createUser({user: "jiunile",pwd: "12345678",roles: [{ role: "readWrite", db: "jiunile" },{ role: "read", db: "jiunile2" }]})

//然后关闭服务器,开启认证,重启服务器,用Robomongo连接,一切OK

//创建DB方法:
use jiunile
db.jiunile.save({country:"England",GroupName:"D"})
show dbs

给我留言

留言无头像?


×
腾讯微博