mysqld:拨打tcp 127.0.0.1:3306:connect:连接被拒绝

时间:2019-08-04 10:35:46

标签: mysql docker monitoring prometheus

我正在尝试使用mysqlprom/mysqld-exporter来对prom/prometheus进行监控。我已经将 docker-compose.yml 文件配置如下:

version: '3'

services:
  mysql:
    image: mysql
    container_name: mysql
    restart: always
    volumes: 
      - mysql:/var/lib/mysql

    # command: 
    #   - CREATE USER 'root'@'localhost' IDENTIFIED BY 'password' WITH MAX_USER_CONNECTIONS 3;
    #   - GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'root'@'localhost';
    environment: 
      - MYSQL_ROOT_PASSWORD= password
      - MYSQL_DATABASE= db #Defining a new Database
      - MYSQL_USER= mostafa
      - MYSQL_PASSWORD= ghadimi
    ports: 
      - 3306:3306
      - 33060:33060

  prometheus:
    image: prom/prometheus
    container_name: prometheus
    ports:
      - 9090:9090
    volumes:
      - prometheus:/prometheus
      - ./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
    depends_on: 
      - mysql

volumes:
  mysql:
  prometheus:

这是我的./mysql-exporter/.my.cnf文件:

[client]
user=mostafa
password=ghadimi

运行docker-compose up命令后,除以下错误外,其他所有内容均正常运行:

mysqld:拨打tcp 127.0.0.1:3306:connect:连接被拒绝

我不知道该如何解决!

PS:我还尝试通过其容器和docker exec -it <container-id> bash命令连接到mysql,并尝试了所有可能的密码(例如空字符串和我在docker中设置的密码) -compose文件),但我遇到另一个错误:

  

错误1045(28000):用户'root'@'localhost'的访问被拒绝(使用密码:是)

1 个答案:

答案 0 :(得分:1)

Mysql-d导出器正在localhost上寻找mysql实例,但它不在mysqld容器内的localhost上运行。您将必须与主机mysql传递外部连接字符串,因为这是docker-compose中mysql服务的名称。从mysqld-exporter docs开始,您将需要使用以下数据源连接字符串将环境变量传递到mysqld-exporter容器:

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="user:password@mysql:3306/database"
    depends_on: 
      - mysql

然后将用户,密码和数据库名称更改为所需的名称。