title: How can I connect to mongodb replica sets and authenticate by php script?
body: <p>I use following php script to connect to mongodb replica set:</p>
  
  <pre><code>$conn = new MongoClient("mongodb://mongodb1:27017,mongodb2:27017/?replicaSet=MyRepSet");
  
  $db = $conn-&gt;test;
  $collection = $db-&gt;items;
  
  $item = array(
      'name' =&gt; 'milk',
      'quantity' =&gt; 10,
      'price' =&gt; 2.50,
      'note' =&gt; 'skimmed and extra tasty'
    );
  
    $collection-&gt;insert($item);
  
   $conn-&gt;close();
  </code></pre>
  
  <p>This works when I start mongodb without --auth and --key</p>
  
  <p>However, when I start mongodb replica set with --auth and --key,  and create a user, like this:</p>
  
  <pre><code>&gt; use test;
  &gt; db.addUser("test","123456")
  </code></pre>
  
  <p>When I try again with the above php script, I get this error:</p>
  
  <pre><code>Uncaught exception 'MongoCursorException' with message 'mongodb3:27017: not authorized for insert on test.items
  </code></pre>
  
  <p>How can I connect to mongodb replica set and authenticate by php script?</p>