title: Cant connect to a mongodb database user with php
body: <p>I've a running mongod with <code>auth=true</code> on my server.</p>
  
  <p>If I log to my admin user (from the admin database), no problem to fetch data.</p>
  
  <pre><code>&lt;?php
  $connection = new Mongo("mongodb://admin:adminpass@127.0.0.1");
  
  $db = $connection-&gt;selectDB( "mydb" );
  $collection = $db-&gt;selectCollection( "user" );
  var_dump($collection-&gt;findOne());
  ?&gt;
  </code></pre>
  
  <p>but if I replace the first line by</p>
  
  <pre><code>$connection = new Mongo("mongodb://mydbadmin:dbadminpass@127.0.0.1:27017");
  </code></pre>
  
  <p>It can not connect and get an error like :</p>
  
  <pre><code>Fatal error: Uncaught exception 'MongoConnectionException' with message 'Couldn't authenticate with database admin: username [mydbadmin]' in .....
  </code></pre>
  
  <p>So the problem is <code>new Mongo()</code> try to connect my user to the admin database instead of the "mydb" database. How can I choose the database I want to connect?</p>
  
  <p><strong>EDIT :</strong></p>
  
  <p>according to <a href="http://php.net/manual/fr/mongo.construct.php" rel="nofollow">http://php.net/manual/fr/mongo.construct.php</a><br>
  I tried this</p>
  
  <pre><code>$login = array("username" =&gt; "mydbadmin",
                 "password" =&gt; "dbadminpass",
                 "db" =&gt; "mydb",
                 "connect" =&gt; true
  );
  
  $connection = new Mongo("mongodb://localhost", $login);
  </code></pre>
  
  <p>but</p>
  
  <pre><code>Couldn't authenticate with database mydb: username [mydbadmin]' in .....
  </code></pre>