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.
4
Answers
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
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/
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
You can connect both container with two options:
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.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 helpfulTIP: You can list networks with this command
docker network ls
, to check the etworks.