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 After I put my node application on the docker with the build command of docker, the host can't connect no matter it is or, and it will report the following error


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
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.

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"
    image: mysql:5.6
      - 3306:3306
lock This question has been locked and the reply function has been disabled.