php - MongoDB PHP 认证

我是 MongoDB 的新手。我 2 个月前开始使用它。我决定测试一个网站的日志(用户行为)。起初没有用户名和密码。它就像一个魅力。我使用 RockMongo 作为管理员。然后我创建了用户名和密码并重新启动了服务器。重新启动后,我的脚本无法将数据放入数据库。但是,当我更新 RockMongo 的设置时,它出现了问题,我可以通过 RockMongo 插入/更新。我在带有 PHP5 的 Ubuntu 10 上使用 MongoDB。我的代码看起来像 这个:

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

我也不能使用 shell。尝试进行身份验证时,我得到了 以下错误:

“$err”:“未经授权的 db:teSTLogs 锁类型:-1 客户端:127.0.0.1”, “代码”:10057

感谢任何帮助。

最佳答案

您需要先连接到特定的数据库。仅仅访问 $m->teSTLogs 并不能做到这一点。相反,您需要在第一行使用:

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

您还可以选择:

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

这允许您放弃 ->authenticate() 调用。 http://php.net/manual/en/mongodb.authenticate.php 处的文档 还说:

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.

https://stackoverflow.com/questions/10267634/

相关文章:

ms-access - Access : Truncation error when appendi

php - 是否可以使用 PHP 添加事件监听器?

maven - 你如何使用 Maven 设置 jetty-start?

r - 在 R 中从循环或 lapply 按名称调用列表

unix - 在 Solaris 上如何找到谁或哪个进程向我的进程发送了信号

sql - Oracle CONNECT BY 分层查询中对父列的引用

php - 清理 XSS 到 Textarea 的输出

perl - Perl 中的非本地返回(从调用者返回)

visual-studio-2010 - 如何断开 VisualSVN 与我的解决方案的连接?

winapi - 如何更新进度条?