首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mongodb:已创建数据库并向其中添加用户,但无法对用户进行身份验证

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

Stack Overflow用户
提问于 2011-06-28 02:33:23
回答 1查看 1.6K关注 0票数 0

这是我用来将用户“某个用户”添加到“某个用户数据库”的php代码。

代码语言:javascript
复制
<?
// 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));

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

代码语言:javascript
复制
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.system.users

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-28 02:43:26

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6497306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档