1

this is the php code I used to add the user "someuser" to the "someusersdatabase".

<?
// 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));

I can authenticate as admin, however when I authenticate someuser the log shows:

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

but when I navigate to the web view, it displays both someusersdatabase and someusersdatabase.system.users

So is the user not being added properly? no error is being thrown when the php code runs...

2
  • this was the code provided by another user on SO on my previous question Commented Jun 27, 2011 at 18:49
  • Related: stackoverflow.com/questions/6482224/… Commented Jun 27, 2011 at 18:53

1 Answer 1

3

You should probably call addUser() instead of inserting directly into the collection, in case there's some under-the-hood bookkeeping going on and/or the hashing function is different. Beyond that, the field name is user, not username.

Sign up to request clarification or add additional context in comments.

1 Comment

It's not necessary to call addUser (inserting manually works as well), but the issue here is indeed the field name that should be user and not username.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.