mongo schema浅析

作者: wencst 分类: linux,数据库 发布时间: 2017-08-16 16:11 阅读: 6,231 次

环境:

mongo 2.6.5

现象:

mongo创建用户无法创建,会报错:

db.createUser({"user":"devUser","pwd":"root","roles":[{"role":"dbOwner", "db":"dev"}]});
Error: couldn't add user: User and role management commands require auth data to have schema version 3 but found 1 at src/mongo/shell/db.js:1004

执行db.system.user.find();

如下结果:

{
"_id" : ObjectId("595b826c0eba556dc7113fd4"),
"user" : "ceilometer",
"pwd" : "7226e5912abb7e6c898611bef46a3d40",
"roles" : [
"dbAdmin",
"readWrite"
]
}

问题:

这个报错是mongo schema版本和客户端版本不一致引起的,我所用的schema版本为2.4,客户端版本为2.6.5,所以在执行时语法不匹配导致的。

如何区分schema版本:

2.4schema:

db.system.users.find()
{
    "_id" : ObjectId("53675bc48ff842a0657e25ff"),
    "user" : "root",
    "pwd" : "c2ff9601c8590812f0d40b9f60869679",
    "roles" : [
        "userAdminAnyDatabase",
        "readWrite"
    ]
}

2.6schema:

db.system.users.find()
{
    "_id" : "admin.root",
    "user" : "root",
    "db" : "admin",
    "credentials" : {
        "MONGODB-CR" : "c2ff9601c8590812f0d40b9f60869679"
    },
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        },
        {
            "role" : "readWrite",
            "db" : "admin"
        }
    ]
}

另外,可以执行命令查看schema版本:

db.system.version.find();

如何区分客户端版本,打开客户端时提示:

解决:

需要升级schema版本:

 db.getSiblingDB("admin").runCommand({authSchemaUpgrade: 1 });

参考资料:

https://stackoverflow.com/questions/23468137/how-to-add-authentication-to-mongodb-2-6

如果文章对您有用,扫一下支付宝的红包,不胜感激!

欢迎加入QQ群进行技术交流:656897351(各种技术、招聘、兼职、培训欢迎加入)



Leave a Reply