MongoDB credentials are not working with StatefulSet

Viewed 17

I have this sts:

apiVersion: "apps/v1"
kind: "StatefulSet"
metadata: 
  name: "mongo-benchmark"
spec:
  serviceName: mongo-benchmark-headless
  replicas: 1
  selector: 
    matchLabels: 
      app: "mongo-benchmark"
  template: 
    metadata: 
      labels: 
        app: "mongo-benchmark"
    spec: 
      containers: 
        - name: "mongo-benchmark"
          image: "mongo:5"
          imagePullPolicy: "IfNotPresent"
          env: 
            - name: "MONGO_INITDB_ROOT_USERNAME"
              value: "admin"
            - name: "MONGO_INITDB_ROOT_PASSWORD"
              value: "admin"
          ports: 
            - containerPort: 27017
              name: "mongo-port"
          volumeMounts: 
            - name: "mongo-benchmark-data"
              mountPath: "/data/db"
      volumes: 
        - name: "mongo-benchmark-data"
          persistentVolumeClaim: 
            claimName: "mongo-benchmark-pvc"

Everything is deployed.
The root user's username and password is admin But when I go to the pod terminal and execute these commands I get:

$ mongo
$ use admin
$ db.auth("admin", "admin")
Error: Authentication failed.
0

I can't even read/write from/to other databases.
For example:

$ mongo
$ use test
$ db.col.findOne({})
uncaught exception: Error: error: {
        "ok" : 0,
        "errmsg" : "not authorized on test to execute command { find: \"col\", filter: {}, limit: 1.0, singleBatch: true, lsid: { id: UUID(\"30788b3e-48f0-4ff0-aaec-f17e20c67bde\") }, $db: \"test\" }",
        "code" : 13,
        "codeName" : "Unauthorized"
}

I don't know where I'm doing wrong. Anyone knows how to authenticate?

0 Answers
Related