Fix PHP 7.4 + MySQL 8 errors with server has gone away

Published On2019-12-02

PHP 7.4 is now released ???, and it comes with support for MySQL 8's new password authentication plugin: caching_sha2_password.

If you have been using MySQL 8 with a PHP version older than 7.3, you probably were authenticating to the database using mysql_native_password. With the upgrade to 7.4, PHP natively supports the new default authentication plugin caching_sha2_password. The two following errors might pop up if you have been using MySQL 8 with PHP 7.3 or older, and using mysql_native_password plugin to deal with PHP's then-lack of support for the new caching_sha2_password plugin.

Unexpected server response while doing caching_sha2 auth: 109
mysqli_real_connect(): (HY000/2006): MySQL server has gone away

This post is to provide some quick information on how to fix this.

From PHP 7.4, PHP natively supports caching_sha2_password. The easiest solution is to change the existing users that you connect to the database to use the new plugin.

1. Connect to the database as root

Depending on your root username, you will be prompted to enter the password, a hostname, etc.
By default, typing mysql in your server terminal should work. If you have trouble logging in, try mysql -p -u root, and entering the root password when asked.

2. Check existing authentication plugin:

Replace USERNAME and YOUR_PASSWORD with your application database username and the password.

mysql> SELECT user,plugin from mysql.user;

Sample output

| user             | plugin                |
| Ayesh            | caching_sha2_password |
| debian-sys-maint | mysql_native_password |
| mysql.infoschema | caching_sha2_password |
| mysql.session    | mysql_native_password |
| mysql.sys        | caching_sha2_password |
| root             | auth_socket           |
| testuser         | mysql_native_password |

If you see mysql_native_password next to the database user's name, you can now change it to caching_sha2_password.

3. Change MySQL authentication plugin to caching_sha2_password

mysql> ALTER USER 'USERNAME'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'YOUR_PASSWORD';

This will change the user USERNAME's authentication plugin to caching_sha2_password with YOUR_PASSWORD.

Recent Articles on PHP.Watch

All ArticlesFeed 
Get Composer to suggest dev packages to `require-dev`

Get Composer to suggest dev packages to require-dev

Composer 2.4 comes with a feature that it prompts during a composer require to install packages as require-dev dependencies when certain keywords are present. Development tools and libraries now can make use of this feature to mark themselves, so Composer uses the prompt when suitable.
Security considerations when parsing user-provided INI strings and files

Security considerations when parsing user-provided INI strings and files

Security considerations when parsing user-provided INI strings and files using parse_ini_string and parse_ini_file functions.
How to extend lifetime of legacy PHP applications

How to extend lifetime of legacy PHP applications

As PHP continue to evolve with new breaking changes, and while that is great for most PHP applications, there are legacy applications that can't justify the human and financial cost of keeping up. Here is a guide on how to extend the lifetime of legacy PHP applications with security updates and maintenance.
Subscribe to PHP.Watch newsletter for monthly updates

You will receive an email on last Wednesday of every month and on major PHP releases with new articles related to PHP, upcoming changes, new features and what's changing in the language. No marketing emails, no selling of your contacts, no click-tracking, and one-click instant unsubscribe from any email you receive.