PHP应用无法在127.0.0.1连接到docker mysql容器 [英] PHP app cannot connect to docker mysql container at 127.0.0.1
问题描述
Mysql在它自己的docker-compose.yml中,因为我希望一个mysql服务器启动并运行,任何其他php应用程序都可以连接到它.所以我在相同的docker-compose.yml中没有php和mysql.在php应用程序中,如果我使用mysql容器的网关ip地址,则可以通过查找它,然后将其硬编码到php应用程序中,从而连接到mysql. docker检查mysql-db
.但是,每次mysql重新启动时,docker都会将其更改为172 ... ip地址,因此不适合开发.
Mysql is in it's own docker-compose.yml as I want a mysql server up and running that any other php application can connect to. So I do not have php and mysql in the same docker-compose.yml. From the php application, I can connect to mysql if I use the mysql container's gateway ip address by looking it up and then hard coding it into the php application. docker inspect mysql-db
. But docker will change that 172... ip address each time mysql restarts so that is not ideal for development.
我可以通过 mysql -h 127.0.0.1
连接到mysql,没问题,但是如果我尝试使用127.0.0.1,则从php应用程序中获得连接被拒绝.我只有在使用172 ...网关IP地址的情况下才能连接.
I can connect to mysql via mysql -h 127.0.0.1
no problem, but from the php application if I try to use 127.0.0.1 I get connection refused. I can only connect if I use the 172... gateway ip address.
如何让mysql容器侦听从主机到127.0.0.1的连接?
How do I get the mysql container listening for connections from the host to 127.0.0.1?
用于mysql的docker-compose.yml
version: "3"
services:
mysql:
container_name: mysql-db
image: mysql
build:
dockerfile: Dockerfile
context: ./server/mysql
environment:
- MYSQL_ROOT_PASSWORD=admin
volumes:
- ./data/mysql:/var/lib/mysql
ports:
- 3306:3306
适用于php的docker-compose.yml
version: "3"
services:
nginx:
container_name: nginx_myapp
image: nginx
build:
dockerfile: Dockerfile
context: ./server/nginx
ports:
- 80:80
- 443:443
volumes:
- ./app:/var/www/html
networks:
- myapp
php:
container_name: php_myapp
image: php:7.3-fpm
build:
dockerfile: Dockerfile
context: ./server/php-fpm
environment:
CI_ENV: development
volumes:
- ./app:/var/www/html
networks:
- myapp
networks:
myapp:
推荐答案
127.0.0.1是回送地址.它指向本地主机.在docker的上下文中,localhost是容器本身.您的php容器上没有运行数据库,因此连接将永远不会成功.
127.0.0.1 is the loopback address. It points to localhost. In the context of docker, localhost is the container itself. There is no db running on your php container so the connection will never succeed.
您需要做的是在mysql撰写文件中配置默认网络,以便您可以预测地控制其名称,以便以后使用(否则,它将根据您的撰写项目名称计算得出,如果您重命名包含的文件夹,该名称可能会更改...):
What you need to do is to configure the default network in you mysql compose file so that you will predictably control its name for later convenience (else it will be calculated from your compose project name which could change if you rename the containing folder...):
重要说明:要使以下内容正常工作,您需要使用撰写文件 version> = 3.5
Important note: for the below to work, you need to use compose file version >= 3.5
---
version: '3.7'
#...
networks:
default:
name: shared_mysql
现在,您可以将任何其他撰写项目中的 shared_mysql
网络用作 external
.
You can now use that shared_mysql
network as external
from any other compose project.
version: '3.7'
services:
nginx:
#...
networks:
- myapp
php:
#...
networks:
- myapp
- database
networks:
myapp:
database:
external: true
name: shared_mysql
然后您可以使用服务名称 mysql
(例如 mysql -h mysql -u user -p
)从 php
容器连接到mysql)
You can then connect to mysql from your php
container using the service name mysql
(e.g. mysql -h mysql -u user -p
)
参考: https://docs.docker.com/compose/networking/
这篇关于PHP应用无法在127.0.0.1连接到docker mysql容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!