title: Authentication failure while trying to save to mongodb
body: <p>I have following code to save to a local running mongo instance:</p>
  <pre><code>MongoCredential credential = MongoCredential.createCredential(&quot;myuser&quot;, &quot;mydatabase&quot;, &quot;mypassword&quot;.toCharArray());
  
  MongoClient mongo = MongoClients.create(MongoClientSettings.builder()
                      .applyToClusterSettings(builder -&gt; builder.hosts(Arrays.asList(new 
  ServerAddress(&quot;localhost&quot;, 27017))))
                      .credential(credential)
                      .build());
  MongoDatabase database = mongo.getDatabase(&quot;mydatabase&quot;);
  MongoCollection&lt;Document&gt; collection = database.getCollection(&quot;mycollection&quot;);
  collection.insertOne(document);
  </code></pre>
  <p>I have created a user for usernmae/password used  in code above using db.createUser() command in mongo.exe shell and these are same credentials I provided while installing mongodb.</p>
  <pre><code>db.createUser(
  {   user: &quot;myuser&quot;,
      pwd: &quot;mypassword&quot;,
  
      roles:[{role: &quot;userAdminAnyDatabase&quot; , db:&quot;admin&quot;}]})
  </code></pre>
  <p>But code fails with:</p>
  <pre><code>Exception in thread &quot;main&quot; com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='myuser', source='mydatabase', password=&lt;hidden&gt;, mechanismProperties={}}
  </code></pre>
  <p>What am I missing here?</p>