简体   繁体   English

无法使用docker wordpress连接到docker mysql

[英]Unable to connect to docker mysql using docker wordpress

I'm trying to connect docker wordpress container to MySQL server running another container. 我正在尝试将docker wordpress容器连接到运行另一个容器的MySQL服务器。

MySQL is running 127.0.0.1:3306 MySQL正在运行127.0.0.1:3306

WP: 127.0.0.1:10000 WP: 127.0.0.1:10000 : 127.0.0.1:10000

If I run $link = mysqli_connect("127.0.0.1", "root", "root", "wp_db"); 如果我运行$link = mysqli_connect("127.0.0.1", "root", "root", "wp_db"); from Tinker or plain PHP script, it works fine; Tinker或普通的PHP脚本,它可以正常工作; also I can connect to docker MySQL using Sequel Pro with above settings. 我也可以使用具有上述设置的Sequel Pro连接到docker MySQL。 But when I try to put these settings in WP admin page to connect to database, it says 但是,当我尝试将这些设置放在WP管理页面中以连接到数据库时,它说

Error establishing a database connection 建立数据库连接时出错

is there anything that I'm missing? 有什么我想念的吗?

Your problem is that the wp is inside a container and cannot comunicate to the other container. 您的问题是wp在一个容器内,无法与其他容器通信。

You can from the host down to the container but not from inside a container to inside another container. 您可以从主机到容器,但不能从容器内部到另一个容器。

I suggest you take a look at docker-compose, is a tool for defining and running multi-container Docker applications just with a docker-compose.yml configuration file. 我建议您看看docker-compose,这是一个仅通过docker-compose.yml配置文件即可定义和运行多容器Docker应用程序的工具。

One very basic example for your problem could be like this 您的问题的一个非常基本的示例可能是这样的

wp:
  image: yourworpressimagefromdockerhub
  ports:
    - 10000:10000
  links:
    - mysql:mysql
mysql:
  image: yourmysqlimagefromdockerhub
  ports:
    - 3306:3306

"wp" container knows of "mysql" container with the "links" entry. “ wp”容器通过“链接”条目知道“ mysql”容器。

Then whenever you want to call the mysql docker container from the wp container you just use "mysql" instead of "127.0.0.1" 然后,每当您要从wp容器调用mysql docker容器时,只需使用“ mysql”而不是“ 127.0.0.1”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM