<p>Hi I have deployed mongodb V-4.2.6 on kubernetes and the below is the yaml file.</p>

<pre><code># Service to expose MongoDB
apiVersion: v1
kind: Service
metadata:
  name: mongo
  namespace: namespace-name 
  labels:
    app: mongo
spec:
  ports:
  - name: mongo
    port: 27017
    targetPort: 27017
  clusterIP: None
  selector:
    app: mongo
    tier: db
  type: ClusterIP

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo
  namespace: namespace-name
  labels:
    tier: "db"
    app: "mongo"   
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongo
      tier: "db"
  template:
    metadata:
      labels:
        app: mongo
        tier: "db"
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: mongo
        image: mongo:4.2.6
        resources:
            limits:
              memory: "2Gi"
            requests:
              memory: "512Mi"
        env:
        - name: MONGO_INITDB_ROOT_USERNAME
          value: "admin"
        - name: MONGO_INITDB_ROOT_PASSWORD
          value: "password"
        - name: MONGO_INITDB_DATABASE
          value: admin
        command: 
          - mongod
          - --auth
        ports:
        - containerPort: 27017
        volumeMounts:
        - name: nfs1
          mountPath: /data/db
      volumes:
        - name: nfs1
          nfs:
            server: 0.0.0.0
            path: "/path/to/volumes"
</code></pre>

<p>If I try to login with the credentials(  db.auth("admin", "password")  )specified in the above yaml file I'm getting authentication failed message in mongodb. I have found related issues in stackoverflow but couldn't find the solution for this. Can any one help me to create admin user from the yaml file.</p>

p.durga shankar
<p>Remove <code>command</code> from your deployment file and it'll work. Because when you set <code>MONGO_INITDB_ROOT_USERNAME</code> and <code>MONGO_INITDB_ROOT_PASSWORD</code> ENV variables at your manifest. Mongo container will enable <code>--auth</code> by itself. So, you don't need to specify explicitly. Take a look at <a href="https://github.com/docker-library/mongo/blob/master/4.2/docker-entrypoint.sh#L197" rel="nofollow noreferrer">here</a>.</p>

<p>Updated YAML</p>

<pre><code>env:
        - name: MONGO_INITDB_ROOT_USERNAME
          value: "admin"
        - name: MONGO_INITDB_ROOT_PASSWORD
          value: "password"
        - name: MONGO_INITDB_DATABASE
          value: admin
        ports:
        - containerPort: 27017
</code></pre>
