Can’t Monitor MySQL using Prometheus, Docker and prom/mysqld-exporter image

I’m trying to monitor MySQL using Prometheus, Docker and prom/mysqld-exporter. I have already searched many sites and follow all of them step by step, but unfortunately I didn’t get the result that I wanted.

There are two main problem:

  1. I can’t even connect to mysql with user and password I defined in the docker-compose.yml file.

The error is:

ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)

I have also checked the popular questions in stackoverflow like this one and tried all of them, but it still doesn’t work properly. I have also add command: mysqld --default-authentication-plugin=mysql_native_password to configuration of mysql in docker-compose file.

  1. Whenever I set the DATA_SOURCE_NAME environment variable,I’ve faced with http timeout of the metrics on localhost:9104/metrics, whereas I can get localhost:9104 without any problem and while I add some flags but it doesn’t works!

Here is my files:

docker-compose.yml:

version: '3'

services:
  mysql:
    image: mysql
    container_name: mysql
    restart: always
    volumes: 
      - mysql:/var/lib/mysql
    environment: 
      - MYSQL_ROOT_PASSWORD= password
      - MYSQL_DATABASE= db
      - MYSQL_USER= mostafa
      - MYSQL_PASSWORD= ghadimi
      # - default-authentication-plugin=mysql_native_password
    command: mysqld --default-authentication-plugin=mysql_native_password
    ports: 
      - 3306:3306
      - 33060:33060

  adminer:
    image: adminer
    restart: always
    ports: 
      -  8080:8080

  prometheus:
    image: prom/prometheus
    container_name: prometheus
    ports:
      - 9090:9090
    volumes:
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
    command: 
      - --config.file=/etc/prometheus/prometheus.yml

  mysql-exporter:
    image: prom/mysqld-exporter
    container_name: mysql-exporter
    ports:
      - 9104:9104
    volumes:
      - ./mysql-exporter/.my.cnf:/root/.my.cnf
    environment: 
      - DATA_SOURCE_NAME='mostafa:[email protected](localhost:9104)/db'
      - collect.info_schema.tablestats=true
      - collect.info_schema.userstats=true
      - collect.info_schema.query_response_time=true
      - collect.auto_increment.columns=true
      - collect.binlog_size=true
      - collect.perf_schema.eventsstatements=true
      - collect.perf_schema.eventswaits=true
      - collect.perf_schema.file_events=true
      - collect.perf_schema.indexiowaits=true
      - collect.perf_schema.tableiowaits=true
      - collect.perf_schema.tablelocks=true
    depends_on: 
      - mysql

volumes:
  mysql:

.my.cnf:

[client]
user=mostafa
password=ghadimi

and prometheus.yml:

global:
    scrape_interval: 15s
    external_labels:
        monitor: 'my-monitor'
scrape_configs:
    # - job_name: 'prometheus'
    #   static_configs:
    #       - targets: ['localhost:9090']

    - job_name: 'mysql-exporter'
      static_configs:
          - targets: ['mysql-exporter:9104']

PS: I didn’t face with any error, whenever I executed docker-compose up command.

Source: StackOverflow