配置用户和使用 PHP 连接到 Mongo 时遇到问题

Trouble with configuring users and connecting to Mongo with PHP

我在 ubuntu 服务器上安装了 mongo 3.0.0。 我尝试使用 PHP(安装了适当的库)连接到 mongo 来学习新知识。不幸的是,我不能再往前走了。 我的用户配置如下所示:

> use testdb
switched to db testdb
> show users
{
    "_id" : "testdb.testdb",
    "user" : "testdb",
    "db" : "testdb",
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "testdb"
        }
    ]
}

然后我尝试执行以下 PHP 代码:

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");

我得到 "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" 未注释

我还通过以下方式验证了连接:

> nc -w 3 -v xxx.xxx.xxx.xxx 27017 Connection to xxx.xxx.xxx.xxx 27017
> port [tcp/*] succeeded!

我浏览了互联网,我已经花了几个小时在这上面,我什至重新安装了 mongo 并重新设置了所有内容,但没有任何成功。 你能指出在哪里寻找解决方案吗?

3.0 的身份验证模式与 2.x 驱动程序不兼容。您应该能够使其工作的一种方法是降级身份验证机制:

use "admin"
var schema = db.system.version.findOne({"_id" : "authSchema"})
schema.currentVersion = 3
db.system.version.save(schema)

然后创建一个用户,您应该可以连接到它。