首页 > 解决方案 > Doctrine\DBAL\Driver\PDOException SQLSTATE[HY000] [2006] MySQL 服务器已消失

问题描述

我有一个 Laravel 6.2 应用程序,到目前为止它是一个几乎开箱即用的基本应用程序。我使用 Laravel 开箱即用的身份验证脚手架并创建了一个用户,并且能够在我的数据库(mysql 工作台)中看到这个用户。用户已创建,但随后我使用php artisan serve命令尝试登录,因为我刚刚创建的用户浏览器挂起约 1 分钟,然后吐出以下错误:

Doctrine\DBAL\Driver\PDOException
SQLSTATE[HY000] [2006] MySQL server has gone away

我研究了与此命令相关的其他帖子并检查了我的 mysql 超时,我增加了限制,但它仍然输出此错误。可能是什么问题?我包括了 mysql 的输出,所以你们都可以看到。

SHOW VARIABLES LIKE '%timeout%'; 
+-----------------------------------+----------+
| Variable_name                     | Value    |
+-----------------------------------+----------+
| connect_timeout                   | 10       |
| delayed_insert_timeout            | 300      |
| have_statement_timeout            | YES      |
| innodb_flush_log_at_timeout       | 1        |
| innodb_lock_wait_timeout          | 50       |
| innodb_rollback_on_timeout        | OFF      |
| interactive_timeout               | 31536000 |
| lock_wait_timeout                 | 31536000 |
| mysqlx_connect_timeout            | 30       |
| mysqlx_idle_worker_thread_timeout | 60       |
| mysqlx_interactive_timeout        | 28800    |
| mysqlx_port_open_timeout          | 0        |
| mysqlx_read_timeout               | 30       |
| mysqlx_wait_timeout               | 28800    |
| mysqlx_write_timeout              | 60       |
| net_read_timeout                  | 30       |
| net_write_timeout                 | 60       |
| rpl_stop_slave_timeout            | 31536000 |
| slave_net_timeout                 | 60       |
| wait_timeout                      | 31536000 |
+-----------------------------------+----------+

SHOW STATUS WHERE variable_name = 'Threads_connected';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| Threads_connected | 2     |
+-------------------+-------+

SHOW STATUS WHERE variable_name LIKE 'Connections';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Connections   | 24    |
+---------------+-------+

mysql版本

mysql --version
mysql  Ver 8.0.18-0ubuntu0.19.10.1 for Linux on x86_64 ((Ubuntu))

PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.052 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.058 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.060 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.058 ms

标签: phpmysqllaravel

解决方案


php.net手册有解释:

当运行 7.1.16 之前的 PHP 版本或 7.2.4 之前的 PHP 7.2 时,将 MySQL 8 Server 的默认密码插件设置为 mysql_native_password 否则您将看到类似于 The server requested authentication method unknown to the client [caching_sha2_password] even when caching_sha2_password未使用。

这是因为 MySQL 8 默认使用caching_sha2_password,这是一个旧 PHP (mysqlnd) 版本无法识别的插件。相反,通过在 my.cnf 中设置 default_authentication_plugin=mysql_native_password 来更改它。未来的 PHP 版本将支持caching_sha2_password 插件。同时,mysql_xdevapi扩展确实支持它。

资源


推荐阅读