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


Mongodb: created a database and added user to it, but cannot authenticate user

这是我用来将用户"someuser"添加到"someusers数据库"的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和someusersddatabase.system.users

那么,用户是否没有被正确添加?php代码运行时没有抛出任何错误。。。

您可能应该call addUser(),而不是直接插入到集合中,以防有一些后台记账和/或哈希函数不同。除此之外,字段名称是user,而不是username