2

onto my web server I installed with Plesk a Grafana docker container. There is a MySQL database installed onto the Server too.

I started the container, and into Grafana I started with setting up a MySQL datasource.

I tried localhost:3306 as host, and I pasted the credentials. But after "Save & Test" I get the error message:

dial tcp 127.0.0.1:3306: connect: connection refused

I think I have a understanding Problem with my Host address. Is there someone who can help me?

Thanks.

Mondy
  • 2,055
  • 4
  • 19
  • 29
  • 1
    localhost/ 127.0.0.1 within a container usually refers to another service within the container. You may need to look for another local address of your server that MySQL listens on. – danblack Aug 18 '18 at 07:19
  • 1
    Possible duplicate of [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – David Maze Aug 18 '18 at 10:36

4 Answers4

3

For development purpose alone, we can use the "host.docker.internal"

Refer the section "I WANT TO CONNECT FROM A CONTAINER TO A SERVICE ON THE HOST" from the official website

enter image description here

Anver Sadhat
  • 3,074
  • 1
  • 25
  • 26
0

You have the error because when you use 127.0.0.1, you refer the IP address of your container.

For example: On Windows, your Docker (if you use Docker Toolbox) have the IP address 192.168.99.100 and your PC have the IP address 192.168.99.1

So if you host your MySQL DB on your local machine, the database host is 192.168.99.1 and your connection must refer 192.168.99.1:3306

Gon
  • 183
  • 7
0

On the mac I got through by using host.docker.internal because grafana is in docker container and mysql is also running in docker container.

https://docs.docker.com/docker-for-mac/networking/

Brent Fisher
  • 132
  • 3
  • 12
0

You can connect both container with two options:

  1. Via internet. If you want to access to mysql from grafana, you can configure the data source, to access host-ip-adress:default-mysql-address (3306). So the mysql container must be run in that port (docker run -d -p 3306:3306 --name name-mysql-container mysql-image) and grafana can access via internet. This option es more easier, but is a bad practice. The reason to expose a database to internet is only there are other apps connected to that database, if is not, database must be closed to internet.

  2. Via network. Both containers must be in the same network. You can create a network (docker network create --driver bridge my_grafana_mysql_network), and then when you run both containers set the network (docker run -d -p 3306:3306 --name name-mysql-container --network my_grafana_mysql_network mysql-image) flag, and both container can be see each to the other. Then in the data source from grafana you call to the name of the container.

TIP: You can use docker inspect docker-container-name, for validate if both containers are in the same network and for check the name of the containers. Is very helpful

TIP: You can list networks with this command docker network ls, to check the etworks.

Felipe Illanes
  • 419
  • 5
  • 8