简体   繁体   English

无法使用PHP从主机登录Docker MySQL

[英]Not able to log in to Docker MySQL from host machine using PHP

I wish to run PHP code on the host machine which makes a connection to a MySQL (containerized using Docker) database. 我希望在与MySQL数据库(使用Docker进行容器化)连接的主机上运行PHP代码。 But I am getting the following error: 但是我收到以下错误:

Connection failed: MySQL server has gone away 连接失败:MySQL服务器已消失

On the host machine(Ubuntu 18.04 LTS) I have the following software installed: 在主机(Ubuntu 18.04 LTS)上,我安装了以下软件:

PHP 7.2.10 (Non-Docker) PHP 7.2.10(非Docker)
MySQL 8.0.13 (Docker) MySQL 8.0.13(Docker)
Apache 2.5.29 (Non-Docker) Apache 2.5.29(非Docker)

I am using the following connection string in the program on the host machine: 我在主机上的程序中使用以下连接字符串:

<?php
 $connection=mysqli_connect('127.0.0.1','vernemq','xyzw','some_db');
 if (!$connection) {
 die("Connection failed: " . mysqli_connect_error());
}
?>

Note: The above program is written in a PHP file which is located in directory /var/www/html 注意:以上程序是用PHP文件编写的,该文件位于目录/ var / www / html中

For understanding let's say the host machine has the IP address 172.10.10.15. 为了理解,假设主机具有IP地址172.10.10.15。

When running this same program with a LAMPP server on a different machine (IP address: 172.10.10.16) on the same network as that of the host, I am able to connect to the MySQL server. 当在与主机所在网络相同的另一台机器(IP地址:172.10.10.16)上的LAMPP服务器上运行同一程序时,我能够连接到MySQL服务器。 I am running the following modified program on the different machine: 我在另一台机器上运行以下修改的程序:

<?php
 $connection=mysqli_connect('172.10.10.15','vernemq','xyzw','some_db');
 if (!$connection) {
 die("Connection failed: " . mysqli_connect_error());
}
?>

I am even able to connect to the Dockerized MySQL server on the host machine by running the following command on the command prompt of the host machine: 通过在主机的命令提示符上运行以下命令,我什至可以连接到主机上的Dockerized MySQL服务器:

$mysql -h 127.0.0.1 -u vernemq -p

Running the following command from the MySQL prompt returns the following: 从MySQL提示符运行以下命令将返回以下内容:

mysql> SELECT user, host FROM mysql.user
    -> ;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| root             | %         |
| vernemq          | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
| vernemq          | localhost |
+------------------+-----------+

Any pointer on what could be wrong? 关于可能出什么问题的任何指针?

Update 04/01/2019 I noted following things 更新 04/01/2019我注意到以下事情

When I ran the db connection file from the command line, I got the following error. 当我从命令行运行数据库连接文件时,出现以下错误。

Getting following error when running the php file from the command prompt
PHP Warning:  mysqli_connect(): Unexpected server respose while doing caching_sha2 auth: 109 in /var/www/html/db.php on line 2
PHP Warning:  mysqli_connect(): MySQL server has gone away in /var/www/html/db.php on line 2
PHP Warning:  mysqli_connect(): (HY000/2006): MySQL server has gone away in /var/www/html/db.php on line 2
Connection failed: MySQL server has gone away

For this I changed the User authentication method from mysql_native to caching_sha2 with following command 为此,我使用以下命令将用户身份验证方法从mysql_native更改为caching_sha2

ALTER USER 'vernemq' IDENTIFIED WITH caching_sha2_password BY 'vernemq';

And it started working. 它开始工作。

But I don't want to use caching_sha2 authentication because it breaks another programs that were working with native_mysql authentication method. 但是我不想使用caching_sha2身份验证,因为它破坏了其他使用native_mysql身份验证方法的程序。 What is the way out? 出路是什么?

Probably you're running on Mac OS and Docker for Mac doesn't provide the docker0 interface, so you need to create a network alias: 可能是您在Mac OS上运行,而Mac的Docker不提供docker0接口,因此您需要创建一个网络别名:

sudo sudo ifconfig lo0 alias 10.10.0.10/24

After that try to use the connection string 10.10.0.10:3306 , it should work. 之后,尝试使用连接字符串10.10.0.10:3306 ,它应该可以工作。

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

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