title: Unable to specify username and password for MongoDB
body: <p>I'm attempting to amend: <a href="https://www.baeldung.com/spring-data-mongodb-tutorial" rel="nofollow noreferrer">https://www.baeldung.com/spring-data-mongodb-tutorial</a> to connect to MongoDB Atlas cloud instance (<a href="https://www.mongodb.com/cloud/atlas" rel="nofollow noreferrer">https://www.mongodb.com/cloud/atlas</a>)</p>
  <p>Here is the local DB connection:</p>
  <p>&lt;mongo:mongo-client id=&quot;mongoClient&quot; host=&quot;localhost&quot; /&gt;</p>
  <p>I'm attempting to change it to:</p>
  <pre><code>&lt;mongo:mongo-client host=&quot;cluster.test.mongodb.net/&quot; port=&quot;27017&quot; id=&quot;mongoClient&quot; credentials=&quot;dbUser:password****@mydb&quot;&gt;
  &lt;/mongo:mongo-client&gt;
  </code></pre>
  <p>but I receive error: &quot;Attribute credentials is not allowed here&quot;</p>
  <p>Where and how should I define the attribute credentials?</p>
  <p>I'm using MongoDB driver version <code>4.0.5</code></p>
  <p>entire mongoConfig.xml:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
      xmlns:p=&quot;http://www.springframework.org/schema/p&quot; xmlns:mongo=&quot;http://www.springframework.org/schema/data/mongo&quot;
      xsi:schemaLocation=&quot;
         http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/data/mongo
         http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context.xsd&quot;&gt;
      &lt;context:annotation-config /&gt;
      
      &lt;mongo:mongo-client host=&quot;cluster.test.mongodb.net/&quot; port=&quot;27017&quot; id=&quot;mongoClient&quot; credentials=&quot;dbUser:password****@mydb&quot;&gt;
      &lt;/mongo:mongo-client&gt;
  
      &lt;mongo:db-factory id=&quot;mongoDbFactory&quot; dbname=&quot;test&quot; mongo-client-ref=&quot;mongoClient&quot; /&gt;
  
      &lt;bean id=&quot;gridFsTemplate&quot; class=&quot;org.springframework.data.mongodb.gridfs.GridFsTemplate&quot;&gt;
          &lt;constructor-arg ref=&quot;mongoDbFactory&quot; /&gt;
          &lt;constructor-arg ref=&quot;mongoConverter&quot; /&gt;
      &lt;/bean&gt;
  
      &lt;bean id=&quot;mongoTemplate&quot; class=&quot;org.springframework.data.mongodb.core.MongoTemplate&quot;&gt;
          &lt;constructor-arg ref=&quot;mongoDbFactory&quot; /&gt;
          &lt;constructor-arg ref=&quot;mongoConverter&quot; /&gt;
      &lt;/bean&gt;
  
      &lt;bean class=&quot;org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor&quot; /&gt;
  
      &lt;mongo:repositories base-package=&quot;com.baeldung.repository&quot; mongo-template-ref=&quot;mongoTemplate&quot; /&gt;
  
      &lt;bean class=&quot;com.baeldung.event.CascadeSaveMongoEventListener&quot;&gt;
      &lt;/bean&gt;
  
      &lt;mongo:mapping-converter id=&quot;mongoConverter&quot; base-package=&quot;com.baeldung.converter&quot;&gt;
          &lt;mongo:custom-converters base-package=&quot;com.baeldung.converter&quot; /&gt;
      &lt;/mongo:mapping-converter&gt;
  
  &lt;/beans&gt;
  </code></pre>
  <p>I followed this <a href="https://developpaper.com/notes-for-spring-xml-mongodb-connection-configuration-specifying-user-name-and-password/" rel="nofollow noreferrer">https://developpaper.com/notes-for-spring-xml-mongodb-connection-configuration-specifying-user-name-and-password/</a> tutorial where is defined as:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
         xmlns:mongo=&quot;http://www.springframework.org/schema/data/mongo&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo.xsd&quot;&gt;
  
      &lt;mongo:mongo-client host=&quot;${mongo.host}&quot; port=&quot;${mongo.port}&quot; id=&quot;mongoClient&quot; credentials=&quot;${mongo.username}:${mongo.password}@${mongo.dbname}&quot;&gt;
      &lt;/mongo:mongo-client&gt;
      &lt;mongo:db-factory dbname=&quot;${mongo.dbname}&quot; mongo-ref=&quot;mongoClient&quot; id=&quot;mongoDbFactory&quot; /&gt;
      &lt;bean id=&quot;mongoTemplate&quot; class=&quot;org.springframework.data.mongodb.core.MongoTemplate&quot;&gt;
          &lt;constructor-arg name=&quot;mongoDbFactory&quot; ref=&quot;mongoDbFactory&quot;/&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>