Mostafa Ghadimi
Mostafa Ghadimi

Reputation: 6736

MongoDB: server returned error on SASL authentication step: Authentication failed

I'm trying to monitor MongoDB using docker, prometheus and mongodb_exporter. I have also seen this question on stackoverflow but it doesn't help me in current situation.

I have used the following docker-compose file to make the mongodb service up.

version: '3'
services: 
  mongo:
    image: mongo
    container_name: mongo
    restart: always
    environment: 
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
  prometheus:
    image: prom/prometheus
    restart: always
    ports:
      - 9090:9090

The above configuration successfully run. I have downloaded the mongodb_exporter using the following command:

wget https://github.com/dcu/mongodb_exporter/releases/download/v1.0.0/mongodb_exporter-linux-amd64

and then run the command which is written below:

./mongodb_exporter-linux-amd64 -logtostderr -mongodb.uri 'mongodb://root:example@localhost:27017' -groups.enabled 'assert, durability, background_flushing, connections, extra_info, global_lock, index_counters, network, op_counters, op_counters_repl, memory, locks, metrics'

but I face with this error:

Listening on :9001 (scheme=HTTP, secured=no, clientValidation=no) E0810 13:49:59.679747 8412 connection.go:48] Cannot connect to server using url mongodb://root:example@localhost:27017: server returned error on SASL authentication step: Authentication failed.

Becuase of the error, I can't see all of the metrics on localhost:9001. What should I do to solve this problem?

Upvotes: 1

Views: 3371

Answers (1)

Mostafa Ghadimi
Mostafa Ghadimi

Reputation: 6736

The problem solved by changing localhost to the name of mongo container which is mongo, too.

Now the following code can be run successfully:

version: '3'

services: 
  mongo:
    image: mongo
    container_name: mongo
    restart: always
    environment: 
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  prometheus:
    image: prom/prometheus
    container_name: prometheus
    restart: always
    ports:
      - 9090:9090
    volumes:
      - /home/mostafa/Desktop/docker_lab/mongo/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
    command: 
      - '--config.file=/etc/prometheus/prometheus.yml'

  mongodb-exporter:
    build: .
    container_name: mongodb_exporter
    command: ./mongodb_exporter-linux-amd64 -logtostderr -mongodb.uri mongodb://mongo:27017 -groups.enabled 'asserts,durability,background_flusshing,connections,extra_info,global_lock,index_counters,network,op_counters,op_counters_repl,memory,locks,metrics'
    restart: always
    ports: 
      - 9001:9001

Upvotes: 0

Related Questions