title: MongoDB Authentication Database Purpose
body: <p>My question is about the mongo shell --authenticationDatabase option.</p>
  
  <p>I was confused by it for a bit thinking that the the database I created the user in was the admin database since that is where the user is actually defined after reading this documentation here: <a href="https://docs.mongodb.com/manual/core/security-users/#user-management-interface" rel="nofollow">https://docs.mongodb.com/manual/core/security-users/#user-management-interface</a></p>
  
  <p>I understand I was wrong; the authentication database is the database the user has a role for. My question then is what is the purpose of the option "--authenicationDatabase?" See my scenarios below.</p>
  
  <p>The command below will connect to the myapp database and also authenticate against the myapp database</p>
  
  <pre><code>mongo -u "bob" -p "bobspassword" --authenticationDatabase "myapp" myapp
  </code></pre>
  
  <p>The command below will connect to the myapp database and also authenticate against the myapp database by default. This seems to make the command above obsolete.</p>
  
  <pre><code>mongo -u "bob" -p "bobspassword" --authenticationDatabase "myapp" myapp
  </code></pre>
  
  <p>The command below will authenticate against the myapp database but not connect to it. Why? Is it simply to test credentials?</p>
  
  <pre><code>mongo -u "bob" -p "bobspassword" --authenticationDatabase "myapp"
  </code></pre>
  
  <p>This command would tell me "Error: Authentication failed." as well so I still wonder why I would need the previous command to find out my credentials are invalid.</p>
  
  <pre><code>mongo -u "bob" -p "bobspassword" myapp
  </code></pre>
  
  <p>Lastly, is there a situation where I need to authenticate against one database and connect to a different one like the command below? I can only connect to databases for which I have a role defined so why wouldn't I authenticate against the database I was trying to connect to?</p>
  
  <pre><code>mongo -u "bob" -p "bobspassword" --authenticationDatabase "myapp" myotherapp
  </code></pre>
  
  <p>Thank you for your insight!</p>