Trying to use MySQL as BackEnd Database to Grafana, but getting "connection refused" error

Hi,
I am trying to bring up the whole TICK STACK using docker-compose:
Here is the docker-compose.yml

version: '3'
services:
  # simple MySQL setup
  mysql:
    image: mysql:5.7
    ports:
#      - 127.0.0.1:3306:3306
      - 3306:3306
    expose:
      - '3306'
    volumes:
      - ./createDB.sh:/createDB.sh
      - ./createDB.sql:/createDB.sql
    environment:
      MYSQL_ROOT_PASSWORD: myRootPassword123
      MYSQL_DATABASE: myDb,grafana
      MYSQL_USER: myDbUser
      MYSQL_PASSWORD: myPassword123
      STARTUP_SQL: createDB.sql
    command: [mysqld, --character-set-server=utf8mb4, --collation-server=utf8mb4_unicode_ci, --innodb_monitor_enable=all, --max-connections=1001]
    #command: ./createDB.sh
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
      timeout: 10s
      retries: 10
#   restart: unless-stopped
  # grafana used for graphing mysql data
  grafana:
    image: grafana/grafana
    volumes:
      - ./grafana/provisioning/:/etc/grafana/provisioning/
    ports:
      - '3000:3000'
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=admin
#     - GF_VIRTUAL_HOST=grafana.loc
#     - GF_SERVER_ROOT_URL=http://grafana.loc
      - GF_DATABASE_NAME=grafana
      - GF_DATABASE_USER=grafana
      - GF_DATABASE_PASSWORD=password
      - GF_DATABASE_TYPE=mysql
      - GF_DATABASE_HOST=mysql:3306
      - GF_DATABASE_MAX_OPEN_CONN=300
      - GF_SESSION_PROVIDER=mysql
#     - GF_SESSION_PROVIDER_CONFIG=grafana:password@tcp(db:3306)/grafana?allowNativePasswords=true
#     - GF_SESSION_PROVIDER_CONFIG=myDbUser:myPassword123@tcp(mysql:3306)/grafana?allowNativePasswords=true
#     - GF_SECURITY_DATA_SOURCE_PROXY_WHITELIST: 192.168.192.4:3306
#     - GF_DATA_SOURCE_PROXY_WHITELIST: "127.0.0.1:3306"
#     - GF_AUTH_ANONYMOUS_ENABLED: "true"
      - GF_SERVER_ROUTER_LOGGING=true
      - GF_LOG_CONSOLE_FORMAT=json
      - GF_LOG_FILTERS:alerting.notifier=debug,alerting.notifier.slack:debug,auth:debug
      - GF_AUTH_TOKEN_ROTATION_INTERVAL_MINUTES=2
      - GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-simple-json-datasource,percona-percona-app,jdbranham-diagram-panel,mtanda-histogram-panel,natel-influx-admin-panel

  influxdb:
    image: influxdb
    container_name: influxdb
    ports:
      - "8086:8086"
      - "8083:8083"
      - "8090:8090"
    environment:
      - INFLUXDB_DB=telegraf,jmeter,jira
      - INFLUXDB_ADMIN_ENABLED=true
      - INFLUXDB_ADMIN_USER=admin
      - INFLUXDB_ADMIN_PASSWORD=admin123
      - INFLUXDB_USER=telegraf
      - INFLUXDB_USER_PASSWORD=telegraf
    volumes:
      - ./influxdb-volume:/var/lib/influxdb
      - ./etc/influxdb/scripts:/docker-entrypoint-initdb.d
    #command: /init-influxdb.sh


  # tool to connect to mysql instance, and allow prometheus to collect mysql stats
  prom_mysql_exporter:
    image: prom/mysqld-exporter
    links:
      - mysql
    ports:
      - '9104:9104'
    environment:
      #DATA_SOURCE_NAME: root:myRootPassword123@(mysql:3306)/myDb
      - DATA_SOURCE_NAME:root:myRootPassword123@(mysql:3306)/
      - DATA_SOURCE_NAME=prometheus:prometheus@(db1_mysql:3306)/
    command: /bin/mysqld_exporter collect.binlog_size=true collect.info_schema.processlist=true

  # prometheus stores and handles timeseries data for statistics around mysql
  prometheus:
    image: prom/prometheus
    volumes:
      - ./prometheus:/etc/prometheus
    links:
      - prom_mysql_exporter
    ports:
      - 9090

  graphite:
    image: graphiteapp/graphite-statsd
    ports:
      - "80:80"
      - "2003-2004:2003-2004"
      - "2023-2024:2023-2024"
      - "8125:8125/udp"
      - "8126:8126"
    volumes:
      - "./data/graphite/storage:/opt/graphite/storage"
      - "./etc/localtime:/etc/localtime:ro"
    container_name: graphite
    restart: always

# statsd:

# telegraf:
  telegraf:
    image: telegraf
    container_name: telegraf
    volumes:
      - ./telegraf.conf:/etc/telegraf/telegraf.conf:ro
    environment:
      INFLUXDB_URL: http://influxdb:8086
    ports:
      - "092:8092/udp"
      - "094:8094"
     #- "8125:8125/udp"

# Define a Chronograf service
  chronograf:
    image: chronograf
    volumes:
      - ./data/chronograf:/var/lib/chronograf
    ports:
      #- "10000:10000"
      - "8888:8888"

# ref: https://github.com/influxdata/TICK-docker/blob/master/0.12/docker-compose.yml
# Define a Kapacitor service
  kapacitor:
    image: kapacitor
    environment:
      KAPACITOR_HOSTNAME: 127.0.0.1
      KAPACITOR_INFLUXDB_0_URLS_0: http://influxdb:8086
    volumes:
      - ./data/kapacitor:/var/lib/kapacitor
    links:
      - influxdb
    ports:
      - "9092:9092"

status:

 docker-compose ps
                         Name                                        Command                       State                                    Ports
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
grafana-mysql-docker-statsd-graphite_chronograf_1         /entrypoint.sh chronograf        Up                      0.0.0.0:8888->8888/tcp
grafana-mysql-docker-statsd-graphite_grafana_1            /run.sh                          Exit 1
grafana-mysql-docker-statsd-graphite_kapacitor_1          /entrypoint.sh kapacitord        Up                      0.0.0.0:9092->9092/tcp
grafana-mysql-docker-statsd-graphite_mysql_1              docker-entrypoint.sh mysql ...   Up (health: starting)   0.0.0.0:3306->3306/tcp, 33060/tcp
grafana-mysql-docker-statsd-                              /bin/mysqld_exporter /bin/ ...   Exit 1
graphite_prom_mysql_exporter_1
grafana-mysql-docker-statsd-graphite_prometheus_1         /bin/prometheus --config.f ...   Up                      0.0.0.0:32789->9090/tcp
graphite                                                  /entrypoint                      Up                      0.0.0.0:2003->2003/tcp, 0.0.0.0:2004->2004/tcp,
                                                                                                                   2013/tcp, 2014/tcp, 0.0.0.0:2023->2023/tcp,
                                                                                                                   0.0.0.0:2024->2024/tcp, 0.0.0.0:80->80/tcp, 8080/tcp,
                                                                                                                   8125/tcp, 0.0.0.0:8125->8125/udp,
                                                                                                                   0.0.0.0:8126->8126/tcp
influxdb                                                  /entrypoint.sh influxd           Up                      0.0.0.0:8083->8083/tcp, 0.0.0.0:8086->8086/tcp,
                                                                                                                   0.0.0.0:8090->8090/tcp
telegraf                                                  /entrypoint.sh telegraf          Up                      0.0.0.0:92->8092/udp, 0.0.0.0:94->8094/tcp, 8125/udp

but, docker-compose up -d erring out for Grafana:

$ docker-compose logs | grep grafana_1

grafana_1              | {"logger":"migrator","lvl":"info","msg":"Starting DB migration","t":"2019-12-07T23:02:48.91778739Z"}
grafana_1              | {"logger":"server","lvl":"eror","msg":"Server shutdown","reason":"Service init failed: Migration failed err: dial tcp 192.168.192.7:3306: connect: connection refused","t":"2019-12-07T23:02:48.921195524Z"}

What can I do to allow remote connections for MYsql :slight_smile:

file: createDB.sql
GRANT ALL PRIVILEGES ON . to ‘grafana’@’%’ with grant option;

Not sure, HOW I would run this file against the MySql in docker-compose ?

1 Like

hi, did you fix that ? @kamal2222ahmed1

If you use a Docker container, you should use not localhost, but the container IP.
The container loophole(localhost) and host localhost didn’t connect to each other.