Homebrew升级后,MySQL服务器已经消失

I just did a brew upgrade to PHP 7.2.8 and MySQL 8.0.12. After this, trying to connect to MySQL from a PHP script (from Bash) times out after 10 seconds (the default MySQL timeout value).

new PDO("mysql:host=localhost;dbname=foo;charset=utf8", "user", "pass");

[2006] MySQL server has gone away

Everything in my environment seems to be normal. I haven't changed anything else in my setup. Connecting to MySQL from Bash works fine. How do I regain MySQL access from PHP?

$ php -i | grep php.ini
Configuration File (php.ini) Path => /usr/local/etc/php/7.2
Loaded Configuration File => /usr/local/etc/php/7.2/php.ini

$ tail /usr/local/etc/php/7.2/php.ini
mysql.default_socket = /tmp/mysql.sock
pdo_mysql.default_socket = /tmp/mysql.sock

$ ls /tmp/mysql*
srwxrwxrwx  0 /tmp/mysql.sock
-rw-------  5 /tmp/mysql.sock.lock
srwxrwxrwx  0 /tmp/mysqlx.sock
-rw-------  6 /tmp/mysqlx.sock.lock

$ mysql
Welcome to the MySQL monitor.
mysql> show variables like '%socket%';
+-----------------------------------------+------------------+
| mysqlx_socket                           | /tmp/mysqlx.sock |
| socket                                  | /tmp/mysql.sock  |
+-----------------------------------------+------------------+

The solution: Add this to your MySQL configuration file (my.cnf):

[mysqld]
default_authentication_plugin=mysql_native_password

PHP manual:

MySQL 8 defaults to caching_sha2_password, a plugin that is not recognized by the older PHP releases. Instead, change it by setting default_authentication_plugin=mysql_native_password in my.cnf. The caching_sha2_password plugin will be supported in a future PHP release.

I hope this will save others some headache and time.

Here is a solution that doesn't require changing the default authentication configuration.

mysql> create user 'username'@'host' identified with mysql_native_password by 'password';

MySQL 8.0 References: CREATE USER Syntax and GRANT USER Syntax

Once PHP is updated for SHA-256 authentication, you can 'alter user' to be identified with caching_sha2_password again.