Mongodb:创建了一个数据库并向其添加了用户,但无法对用户进行身份验证

时间:2011-06-27 18:33:23

标签: php mongodb

这是我用来将“someuser”用户添加到“someusersdatabase”的php代码。

<?
// open connection

  $mongo = new Mongo("mongodb://admin:passwd@remotemongoserver:27017");
  $db = $mongo->selectDB("someusersdatabase");

$mongo->selectDB("someusersdatabase")->createCollection('__tmp_collection_');
$mongo->selectDB("someusersdatabase")->dropCollection('__tmp_collection_');

 // $collections = $db->selectCollection("tmp");
 // $collections->insert(array('t' => '1'));

  // user info to add
  $username = "someuser";
  $password = "apassword";

  // insert the user - note that the password gets hashed as 'username:mongo:password'
  // set readOnly to true if user should not have insert/delete privs
  $collection = $db->selectCollection("system.users");
  $collection->insert(array('username' => $username, 'pwd' => md5($username . ":mongo:" . $password), 'readOnly' => false));

我可以作为管理员进行身份验证,但是当我验证某些用户时,日志会显示:

Mon Jun 27 14:01:38 [initandlisten] connection accepted from client:62708 #1
Mon Jun 27 14:01:38 [conn1] auth: couldn't find user someuser, someusersdatabase.system.users

但是当我导航到Web视图时,它会显示someusersdatabase和someusersdatabase.system.users

用户没有正确添加? php代码运行时没有抛出任何错误......

1 个答案:

答案 0 :(得分:3)

你可能应该call addUser()而不是直接插入集合中,以防万一发动机内部记账和/或散列函数不同。除此之外,字段名称为user,而不是username