PHP-mysqli 无法连接到 MySQL 8.0.16 运行 Docker

PHP-mysqli can't connect to MySQL 8.0.16 running in a Docker

总结:

在PHP中,mysqli因此失败:

mysqli_connect('127.0.0.1', 'test', 'test', 'test', 8016)

ERR: 2054 : The server requested authentication method unknown to the client

同时 mysql 可以从 外部连接 Docker:

mysql  -P 8016 -h 127.0.0.1 -u test -ptest test

问题: 我怎样才能让 mysqli_connect 工作?

详情:

以下是一些相关的 GRANT 条目。

mysql> select host, user, db, select_priv, grant_priv
                  from mysql.db where user='test';
+------------+------+------+-------------+------------+
| host       | user | db   | select_priv | grant_priv |
+------------+------+------+-------------+------------+
| 172.17.0.1 | test | test | Y           | N          |

mysql> select host, user, plugin, select_priv, grant_priv
                    from mysql.user where user='test';
+------------+------+-----------------------+-------------+------------+
| host       | user | plugin                | select_priv | grant_priv |
+------------+------+-----------------------+-------------+------------+
| 172.17.0.1 | test | mysql_native_password | N           | N          |

我在 docker 图像 中找到了 /etc/my.cnf 并启用了

default-authentication-plugin=mysql_native_password

即使是旧的 (5.6) mysql 也可以这样连接:

mysql  -P 8016 -h 127.0.0.1 -u test -ptest test

而且,是的,它返回时的标题包括

Server version: 8.0.16 MySQL Community Server - GPL

似乎命令行mysql知道mysql_native_password,但是 mysqlnd 没有。

更多版本信息:

PHP Version 7.0.33-0ubuntu0.16.04.6
Apache/2.4.18 (Ubuntu) 

phpinfo 关于 mysqlnd:

的说法
Version:         mysqlnd 5.0.12-dev - 20150407 - $Id: b5c5906d452ec590732a93b051f3827e02749b83 $
Loaded plugins:  mysqlnd,debug_trace,auth_plugin_mysql_native_password,auth_plugin_mysql_clear_password,auth_plugin_sha256_password
API Extensions:  mysqli,pdo_mysql 

#docker ps(显示端口映射):

CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                  PORTS                               NAMES
99193bedb36c        mysql/mysql-server:8.0.16   "/entrypoint.sh my..."   8 weeks ago         Up 12 hours (healthy)   33060/tcp, 0.0.0.0:8016->3306/tcp   o8016

PHP-CLI也连接失败

(是的,我运行FLUSH PRIVILEGES;在适当的时候。)

您已尝试在 my.cnf 中设置 default-authentication-plugin - 也许尝试将其作为 command arg?我在尝试授权时遇到了类似的问题,但这在我的 docker-compose.yml 中对我有用 - 我从未尝试过在 my.cnf

中设置它
mysql:
    container_name: myapp-db
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password
    environment:
     (snip)

command 摘自 MySQL 的 Docker 文档页面)。