“Can't connect to local MySQL server” in docker-compose

前端 未结 2 996
庸人自扰
庸人自扰 2021-01-21 20:44

I have Laravel application with MySQL and I\'m trying to run it in Docker using docker-compose. But when app trying to connect DB, it throws:

PDOException

相关标签:
2条回答
  • 2021-01-21 20:53

    Mysql connections to "localhost" by default attempt to connect over a socket present on the local machine. Your DB_HOST should be the linked container name, "db".

    From the MYSQL docs:

    On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option. For example:

    0 讨论(0)
  • 2021-01-21 21:01

    You can change DB_HOST localhost to ip in .env. To receive ip mysql docker container need write command:

    docker exec -it <CONTAINER_ID> ip add show eth0
    
    0 讨论(0)
提交回复
热议问题