I have a nodejs trying to mysql.createPool({host: 'host.docker.internal', ...})
but I got
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)
at Protocol._enqueue (.../node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (.../node_modules/mysql/lib/protocol/Protocol.js:51:23)
at PoolConection.connect (.../node_modules/mysql/lib/Connection.js:116:18)
at Pool.getConnection (.../node_modules/mysql/lib/Pool.js:48:16)
Could there be a bug in the mysql client in the node:12-alpine image? I've checked that docker exec -it ... ping host.docker.internal
is indeed resolved as 172.17.0.1.
Why is mysql.createPool({host: 'host.docker.internal', ...})
trying to look for 127.0.0.1:3306?
I was able to reach another container on the same host with host.docker.internal
. I believe it proves that I have run --add-host=host.docker.internal:host-gateway
correctly.
I am also able to query MySQL by running the js standalone with node xyz.js
on both localhost and 127.0.0.1 .
What am I missing?
I am trying all these on Ubuntu 20.04.3 LTS , Docker 20.10.8 , MySQL 8.0.26 (installed on the host).
************************** PS **************************
I built another image with 172.10.0.1 hardcoded in createPool()
and run the js in a container, but to no avail.
Then I did a crazy experiment by running the js standalone node xyz.js
on the host with mysql.createPool({host: '172.17.0.1', ...})
it would give
connect ECONNREFUSED 172.17.0.1:3306`
BUT if I edit /etc/hosts
and add 172.17.0.1 host.docker.internal
(on the host) and mysql.createPool({host: 'host.docker.internal', ...})
, the js could connect to MySQL standalone!
If you use 127.0.0.1 then the container would try to connect on localhost within the container, but mysql is running on the docker host.
You can try to connect to it on the network interface added by docker, you can check that by using the command ip a
, please see following output
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:53:81:5c:57 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
In my case I would use below url to connect to mysql:
172.17.0.1:3306
I am running a docker-compose NodeJS application which connects to the Linux host MySQL database on port 3306. To resolve the errors described:
Specifically for Ubuntu 20.04.3 LTS & mysql Ver 15.1 Distrib 10.3.32-MariaDB , the configuration is found in /etc/mysql/mariadb.conf.d/50-server.cnf
If you are not sure where the config is located, run the following to locate:
grep -rnw 'bind-address' /etc/mysql/
Proceed to edit the mysql server configuration, replacing 127.0.0.1
with 0.0.0.0
as described on Docker forum here .
sudo vim /etc/mysql/mariadb.conf.d/50-server.cnf
make the following change differential:
- bind-address = 127.0.0.1 + bind-address = 0.0.0.0
and restart mysql (MariaDB)
sudo systemctl restart mariadb.service
On running docker-compose
, the container which previously failed is now able to access the localhost SQL database!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.