</pre><pre name="code" class="python">0,创建用户,然后删除,否则没有authSchema
>db.createUser(
{
user: "maodan",
pwd: "maodan_2015",
roles: [ { role: "userAdmin", db: "maodan" }, { role: "readWrite", db: "maodan" }, { role: "dbOwner", db: "maodan" }]
}
)
> db.system.users.remove({_id:"maodan.joeoyon"});
WriteResult({ "nRemoved" : 1 })
1,修改权限认证版本
> schema = db.system.version.findOne({"_id" : "authSchema"})
{ "_id" : "authSchema", "currentVersion" : 5 }
> schema.currentVersion = 3
3
> db.system.version.save(schema)
2,在admin库创建root用户
db.createUser({user:"root",pwd:"leked_2015",roles:["userAdminAnyDatabase","readAnyDatabase","root"]})
3,在test库创建途同账号
db.createUser(
{
user: "maodan",
pwd: "maodan_2015",
roles: [ { role: "userAdmin", db: "maodan" }, { role: "readWrite", db: "maodan" }, { role: "dbOwner", db: "maodan" }]
}
)
4,启动mogodb服务端,指定认证策略
mongod --dbpath=/var/lib/mongo --setParameter authenticationMechanisms=MONGODB-CR --auth
{
role:"userAdminAnyDatabase",
db:"admin"
},
{
role:"readWriteAnyDatabase",
db:"admin"
},
{
role:"dbAdminAnyDatabase",
db:"admin"
}
备注:mongod --dbpath=/var/lib/mongo --fork --logpath=/var/log/mongodb/mongod.log --master --setParameter authenticationMechanisms=MONGODB