这篇文章给大家分享的是有关如何解决Mongodb 3.2.9开启用户权限认证问题的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
一、在老版的Mongodb(大概3.0以前)可以这样开启认证:
1、Linux环境下mongo shell方式认证:
>show dbs
##看到有如下数据
admin (empty)
comment 0.203125GB
dbtest (empty)
foo 0.203125GB
local (empty)
test 0.203125GB
>use admin
switched to db admin
> db.addUser('admin','12345678') ##添加用户
Mon Nov 5 23:40:00 [FileAllocator] allocating new datafile /data/db/admin.ns, filling with zeroes...
{
"user" : "admin",
"readOnly" : false,
"pwd" : "89e41c6c28d88d42c21fe501d82969ea",
"_id" : ObjectId("5097ddd00342c63efff3fbfb")
}
##之后运行
>showdbs
Mon Nov 5 23:45:13 uncaught exception: listDatabases failed:{ "errmsg" : "need to login", "ok" : 0 } ##提示需要登录
添加--auth 启动
./mongod -auth
./mongo
>use admin
switched to db admin
> db.auth('admin','12345678') ##用添加的账户密码查看
Mon Nov 5 23:49:32 [conn56] authenticate db: admin { authenticate: 1, nonce: "304f5242601fafa4", user: "admin", key: "58260df384b1146466efca5c90a5ff05" }
1
#1 说明登录成功
> show dbs
admin 0.203125GB
comment 0.203125GB
dbtest (empty)
foo 0.203125GB
local (empty)
test 0.203125GB
> use admin
switched to db admin
> show collections;
system.indexes
system.users
> db.system.users.find() ##查找数据
{ "_id" : ObjectId("5097ddd00342c63efff3fbfb"), "user" : "admin", "readOnly" : false, "pwd" : "89e41c6c28d88d42c21fe501d82969ea" }
2、php代码连接认证:
<?php
##1 使用超级用户连接mongodb
$m = new Mongo("mongodb://admin:12345678@192.168.138.35:27017");
$db = $m->melon;
$collection = melonco;
$collection = $db->$collection;
$array = array('name'=>'melon','age'=>'24','sex'=>'Male','birth'=>array('year'=>'1988','month'=>'07','day'=>'13'));
$collection->insert($array);
$cursor = $collection->find();
foreach ($cursor as $id => $value) {
echo "$id: "; var_dump($value); echo "<br>";
}
###2 使用数据库用户
$m = new Mongo("192.168.138.35:27017");
$db = $m->melon;
$db->authenticate("melon", "melon");
$collection = $db->melonco;
$array = array('name'=>'melon_son','age'=>'0','sex'=>'Male','birth'=>array('year'=>'201X','month'=>'07','day'=>'13'));
$collection->insert($array);
$cursor = $collection->find();
foreach ($cursor as $id => $value) {
echo "$id: "; var_dump($value); echo "<br>";
}
二、在3.0版之后的Mongodb,shell中依旧可以使用上述方法验证,但是php认证一直失败,日志中会报错( Failed to authenticate myuser@userdb with mechanism MONGODB-CR: AuthenticationFailed MONGODB-CR credentials missing in the user document
),原来新版的mongodb加入了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 })
不过如果你现在开启认证,仍然会提示AuthenticationFailed MONGODB-CR credentials missing in the user document
原因是原来创建的用户已经使用了SCRAM-SHA-1认证方式
> use admin
> db.auth('root','123456')
> db.system.users.find()
{ "_id" : "admin.root", "user" : "root", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "XoI5LXvuqvxhlmuY6qkJIw==", "storedKey" : "VAT7ZVMw2kFDepQQ6/E0ZGA5UgM=", "serverKey" : "TebHOXdmY6IHzEE1rW1Onwowuy8=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
{ "_id" : "mydb.test", "user" : "test", "db" : "mydb", "credentials" : { "MONGODB-CR" : "c8ef9e7ab00406e84cfa807ec082f59e" }, "roles" : [ { "role" : "readWrite", "db" : "mydb" } ] }
解决方式就是删除刚刚创建的用户,重新重建即可:
> db.system.users.remove({user:"test"});
> use mydb
>db.createUser({user:'test',pwd:'123456',roles:[{role:'readWrite',db:'mydb'}]})
然后开启认证,重启服务器,用php连接,一切OK
<?php
#1 使用数据库用户认证连接mongodb
$m = new Mongo("mongodb://test:12345678@localhost:27017/mydb");
$db = $m->mydb;
$collection = $db->stu;
$array = array('name'=>'melon','age'=>'24','sex'=>'Male','birth'=>array('year'=>'1988','month'=>'07','day'=>'13'));
$collection->insert($array);
$cursor = $collection->find();
foreach ($cursor as $id => $value) {
echo "$id: "; var_dump($value); echo "<br>";
}
感谢各位的阅读!关于“如何解决Mongodb 3.2.9开启用户权限认证问题”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!