undefinedfix
Sign in

The application inside docker cannot connect to MySQL

blwqh edited in Sat, 11 Jun 2022

I installed MySQL in the docker, and it can be used in the Mac. The host is 127.0.0.1. After I put my node application on the docker with the build command of docker, the host can't connect no matter it is 127.0.0.1 or 0.0.0.0, and it will report the following error

connect ECONNREFUSED 0.0.0.0:3306

at Protocol.Object.<anonymous>.Protocol._enqueue (node_modules/mysql/lib/protocol/Protocol.js:144:48)
      at Protocol.handshake (node_modules/mysql/lib/protocol/Protocol.js:51:23)
      at Connection.connect (node_modules/mysql/lib/Connection.js:118:18)
      at new connect (sdk.js:23:29)
      at Object.test (sdk.test.js:44:24)
2 Replies
manarinian
commented on Sat, 11 Jun 2022

Because node is in the container and MySQL is also in the container, these two are not the same container (it can be roughly understood that they are not running on the same machine), so in the node code, the host of MySQL is not the local network . 0 . 0 . 1, but the host name specified for MySQL in the compose file.

user282
commented on Sun, 12 Jun 2022

When I use docker compose, the code is filled with docker- compose.yml Corresponding service name mysql56

version: "3"
services:
  mysql56:
    image: mysql:5.6
    ports:
      - 3306:3306
lock This question has been locked and the reply function has been disabled.