Using Docker I get the error: “SQLSTATE[HY000] [2002] No such file or directory”

后端 未结 3 1823
一个人的身影
一个人的身影 2020-12-07 20:59

I\'m using Docker to create a container to test my web app built on PHP and MySQL on my Mac. My PHP app is built using Fat-Free Framework for MVC and routing. I have two D

相关标签:
3条回答
  • 2020-12-07 21:21

    As someone pointed out in the comments, the docker-compose file you provided is very relevant to your question.

    The documentation for links in docker-compose files says

    Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified.

    In your case, the database container is named db, so resolving db host from the PHP container should point you at the MySQL container. Replacing localhost with db in your config file should allow the PHP container to connect to MySQL.

    0 讨论(0)
  • 2020-12-07 21:24

    php lives on a different docker image than mysql. thus localhost and 127.0.0.1 from php do not point to mysql. you should connect to the ip of the mysql docker instance.

    also make sure mysql is listening on all interfaces. In mysql.ini you need to put listen 0.0.0.0 to listen on all available interfaces. By default it only allow connections from localhost (and the php docker container is a different host).

    0 讨论(0)
  • 2020-12-07 21:39

    You can use this command

    docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

    Containers will connect with each other by their ip For example: My ip list is:

    /nginx - 172.23.0.4

    /php - 172.23.0.3

    /mysql - 172.23.0.2

    Put this ip-address into your config file instead of 127.0.0.1. Hope that it will help you!

    0 讨论(0)
提交回复
热议问题