配置用户和使用 PHP 连接到 Mongo 的问题 [英] Trouble with configuring users and connecting to Mongo with PHP
问题描述
我在我的 ubuntu 服务器上安装了 mongo 3.0.0.我尝试使用 PHP(安装了适当的库)连接到 mongo 以学习新东西.不幸的是,我不能再进一步了.我的用户配置如下所示:
I installed mongo 3.0.0 on my ubuntu server. I tried to use PHP (with appropriate lib installed) to connect to mongo to learn sth new. Unfortunaltey I cannot go any further. My user configuration looks like this:
> use testdb
switched to db testdb
> show users
{
"_id" : "testdb.testdb",
"user" : "testdb",
"db" : "testdb",
"roles" : [
{
"role" : "readWrite",
"db" : "testdb"
}
]
}
然后我尝试执行以下 PHP 代码:
Then I try to execute the following PHP code:
try{
$uri = "mongodb://testdb:password@xxx.xxx.xxx.xxx:27017/testdb";
$options = array("connectTimeoutMS" => 30000);
$client = new MongoClient($uri, $options );
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
die();
}
$db = $client->selectDB("testdb");
我收到消息:无法连接到:xxx.xxx.xxx.xxx:27017:用户名为testdb"的数据库testdb"上的身份验证失败:身份验证失败.
I get "Message: Failed to connect to: xxx.xxx.xxx.xxx:27017: Authentication failed on database 'testdb' with username 'testdb': auth failed".
在/etc/mongod.conf 中,我没有注释auth = true"
In /etc/mongod.conf I have "auth = true" uncommented
我还验证了这个conncetion:
I also verified the conncetion with:
> nc -w 3 -v xxx.xxx.xxx.xxx 27017 Connection to xxx.xxx.xxx.xxx 27017
> port [tcp/*] succeeded!
我通过互联网挖掘,我已经在这上面花了几个小时,我什至重新安装了 mongo 并重新设置了一切,但没有任何成功.你能指出在哪里寻找解决方案吗?
I dig through Internet, i spent few hours already on this, I even re-installed mongo and set everything up again without any success. Could you point to where to look for solution?
推荐答案
3.0 的身份验证架构与 2.x 驱动程序不兼容.您应该能够使其工作的一种方法是降级身份验证机制:
The authentication schema of 3.0 is not compatible with 2.x drivers. One way you should be able to make it work is by downgrading the authentication mechanism:
use "admin"
var schema = db.system.version.findOne({"_id" : "authSchema"})
schema.currentVersion = 3
db.system.version.save(schema)
然后创建一个用户,您应该能够与它连接.
Then create a user and you should be able to connect with it.
这篇关于配置用户和使用 PHP 连接到 Mongo 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!