dstew32424 2015-03-14 15:07
浏览 36
已采纳

配置用户和使用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"
        }
    ]
}

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

I get "Message: Failed to connect to: xxx.xxx.xxx.xxx:27017: Authentication failed on database 'testdb' with username 'testdb': auth failed".

In /etc/mongod.conf I have "auth = true" uncommented

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!

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?

  • 写回答

1条回答 默认 最新

  • duanjiaren8188 2015-03-14 17:32
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭