dta25920 2012-04-22 11:40
浏览 40
已采纳

MongoDB PHP认证

I'm new to MongoDB. I started using it 2 months ago. I decided to test it for logs of one website (user behavior). At first without username and password. And it worked like a charm. I use RockMongo for admin. Then I created username and password and restarted the server. After the restart my script was unable to put data on database. However when I updated the settings of RockMongo it wirked and I can insert/update via RockMongo. I'm using MongoDB on Ubuntu 10 with PHP5. My code looks like this:

    $m = new Mongo();
    $db = $m->testlogs;
    $db->authenticate('username', 'password');

I cannot use shell too. When trying to authenticate I'm getting the following error:

"$err" : "unauthorized db:testlogs lock type:-1 client:127.0.0.1", "code" : 10057

Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • douziqian2871 2012-04-22 16:01
    关注

    You need to connect to a specific database first. Just accessing $m->testlogs doesn't do that. Instead, you need to use as first line:

    $m = new Mongo('mongodb://localhost/testlogs');
    

    And optionally you could do:

    $m = new Mongo('mongodb://username:password@localhost/testlogs');
    

    Which allows you to drop the ->authenticate() call. The docs at http://php.net/manual/en/mongodb.authenticate.php also say:

    In general, you should use the authenticate built into Mongo::__construct() in preference to this method. If you authenticate on connection and the connection drops and reconnects during your session, you'll be reauthenticated. If you manually authenticated using this method and the connection drops, you'll have to call this method again once you're reconnected.

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?