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 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真