为什么Wordpress在这个docker-compose应用程序中无法连接到MySQL?

时间:2018-03-12 11:37:49

标签: mysql wordpress docker docker-compose

我正在尝试设置一个运行多个wordpress网站的docker-compose应用程序,共享一个单个mysql容器

配置

以下是相应的version: '3.3' services: ################## # Gateway Server # ################## # # NOTE: nginx is included for completenes, but (a priori) not # relevant to the issue at hand. nginx: image: nginx:latest restart: always depends_on: - site0 ports: - "80:80" volumes: - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro ################### # Database Server # ################### mysql: image: mysql:latest restart: always volumes: - ./mysql/data:/var/lib/mysql - ./mysql/wpinit.sql:/docker-entrypoint-initdb.d/wpinit.sql:ro env_file: - ./mysql/mysql.env # sets MYSQL_ROOT_PASSWORD ################### # Wordpress Sites # ################### site0: image: wordpress:latest restart: always depends_on: - mysql env_file: - ./wordpress/wordpress.env # contains `WORDPRESS_DB_HOST=mysql:3306` - ./wordpress/site0/.env # contains WORDPRESS_DB_(USER|NAME|PASSWORD)

mysql

由于多个wordpress站点将共享/docker-entrypoint-initdb.d/容器,因此使用环境变量初始化users / tables / non-root-password已不再可行。因此,我在CREATE DATABASE site0; CREATE USER 'site0'@'localhost' IDENTIFIED BY 'supersecretpassword'; GRANT ALL PRIVILEGES ON site0.* TO 'site0'@'localhost'; FLUSH PRIVILEGES;

中卷了以下脚本
docker-compose up

症状

运行mysql会从mysql_1 | 2018-03-12T11:09:28.787698Z 2 [Note] Access denied for user 'site0'@'172.18.0.3' (using password: YES) 生成以下输出:

site0

site0_1 | MySQL Connection Error: (1045) Access denied for user 'site0'@'172.18.0.3' (using password: YES) site0_1 | site0_1 | Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'site0'@'172.18.0.3' (using password: YES) in Standard input code on line 22 容器回显:

mysql_1       | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/wpinit.sql
mysql_1       | mysql: [Warning] Using a password on the command line interface can be insecure.
mysql_1       | 2018-03-12T11:09:25.094158Z 5 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
mysql_1       | 2018-03-12T11:09:25.095076Z 5 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
mysql_1       | 2018-03-12T11:09:25.097564Z 5 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
mysql_1       | 2018-03-12T11:09:25.097787Z 5 [Warning] 'user' entry 'site0@localhost' ignored in --skip-name-resolve mode.
mysql_1       | 2018-03-12T11:09:25.101138Z 5 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
mysql_1       | 2018-03-12T11:09:25.102141Z 5 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
mysql_1       | 2018-03-12T11:09:25.102677Z 5 [Warning] 'db' entry 'site0 site0@localhost' ignored in --skip-name-resolve mode.
mysql_1       | 2018-03-12T11:09:25.103542Z 5 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
mysql_1       | 2018-03-12T11:09:25.103945Z 5 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
mysql_1       | 2018-03-12T11:09:25.104155Z 5 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.

在日志输出中,当调用mysql init脚本时,我们得到:

$ docker exec -ti wpsvc_mysql_1 bash
$ mysql -uroot -psupersecretpassword

mysql> SELECT EXISTS(SELECT * FROM mysql.user WHERE user = 'site0');
+-------------------------------------------------------+
| EXISTS(SELECT * FROM mysql.user WHERE user = 'site0') |
+-------------------------------------------------------+
|                                                     1 |
+-------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| site0              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.02 sec)

诊断

我首先要检查是否实际在mysql容器中创建了表,用户和密码:

site0

./wordpress/site0/.env./mysql/wpinit.sql中仔细检查{{1}}的数据库密码是否完全相同后,我来这里匆匆忙忙。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我建议尝试'site0'@'%',因为它不会从同一主机调用。