简体   繁体   English

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

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

Summary:概括:

  • mysql-server 8.0.16 is running inside a Docker container . mysql-server 8.0.16在 Docker 容器内运行。
  • php 7 + Apache2 + myslnd 5.0.12 + mysqld 5.6.22 are running outside Docker. php 7 + Apache2 + myslnd 5.0.12 + mysqld 5.6.22Docker之外运行。

In PHP, mysqli fails thus:在 PHP 中,mysqli 因此失败:

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

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

Meanwhile mysql can connect thus from outside Docker:同时mysql可以从外部Docker 连接:

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

Question: How can I get mysqli_connect working?问题:如何让mysqli_connect工作?

Details:细节:

Here are some of the relevant GRANT entries.以下是一些相关的 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          |

I found the /etc/my.cnf inside the docker image and enabled我发现泊坞窗图像的/etc/my.cnf并启用

default-authentication-plugin=mysql_native_password

That allowed even an old (5.6) mysql to connect thus:这甚至允许旧的 (5.6) mysql连接:

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

And, yes, it came back with a heading including而且,是的,它返回的标题包括

Server version: 8.0.16 MySQL Community Server - GPL

It seems that the commandline mysql knows about mysql_native_password , but mysqlnd does not.似乎命令行mysql知道mysql_native_password ,但mysqlnd不知道。

More version info:更多版本信息:

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

phpinfo says this about mysqlnd : 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 (shows port mapping): #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 also fails to connect. PHP-CLI 也无法连接。

(Yes, I ran FLUSH PRIVILEGES; at the appropriate time.) (是的,我在适当的时间运行了FLUSH PRIVILEGES; 。)

You've tried setting default-authentication-plugin in my.cnf - perhaps try to do it as a command arg instead?您已经尝试在my.cnf设置default-authentication-plugin - 也许尝试将其作为command arg 来代替? I had similar problems trying to auth, but this works for me in my docker-compose.yml - I never tried setting it in my.cnf我在尝试身份验证时遇到了类似的问题,但这在我docker-compose.yml对我docker-compose.yml - 我从未尝试在my.cnf设置它

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

( command taken from the Docker docs page for MySQL). command取自 MySQL 的 Docker 文档页面)。

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

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