Why am I getting ECONNREFUSED connecting to localhost MySQL from docker node app?
I have a locally running MySQL server.. it’s NOT within a container. My app is going to be hitting RDS so no sense in going that route. My app was able to hit RDS no problem, as a test. But obviously I want to hit something local for local development.
From my terminal I can do
mysql --user=root --password=password mydb successfully.
And as I’m not getting a timeout error, from my container I can
ping 127.0.0.1:3306 with no issue.
I also used console to see I am definitely passing the right info, after having updated the values from RDS to locally running MySQL.
One Solution collect form web for “Why am I getting ECONNREFUSED connecting to localhost MySQL from docker node app?”
Docker container has its own network IPs, including its own
localhost. So you basically need to be sure of two things:
That your host MySQL is listening in all of its interfaces (bind-addres = 0.0.0.0 in my.cnf). Check with
netstat -na|grep 3306.
Figure out the host ip that your container can reach. So check the IP of the container:
docker inspect container-id, find the IP, and replace the last part with
.1, that should be the IP of your host in the containers own network. I.e.
172.17.0.1(it can be considered as fixed IP, for dev environment it’s ok)
So most likely that this is what you need: