Laravel and MySQL 8: Fixing MySQL Server Has Gone Away Error

Published on by

Laravel and MySQL 8: Fixing MySQL Server Has Gone Away Error image

Update December 6, 2019: Ayesh Karunaratne shared his post on fixing this issue, and clarified that PHP 7.4 does indeed support the new default MySQL password plugin. This post has been updated to correct some inaccurate information in the original post.

If you’ve tried to upgrade your Laravel applications to use MySQL 8, you might have run into the following error that left you scratching your head:

SQLSTATE[HY000] [2006] MySQL server has gone away

The php.net manual has an explanation:

When running a PHP version before 7.1.16, or PHP 7.2 before 7.2.4, set MySQL 8 Server’s default password plugin to mysql_native_password or else you will see errors similar to The server requested authentication method unknown to the client [caching_sha2_password] even when caching_sha2_password is not used.

This is because MySQL 8 defaults to caching_sha2_password, a plugin that is not recognized by the older PHP (mysqlnd) 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. In the meantime, the mysql_xdevapi extension does support it.

PHP 7.4 now supports caching_sha2_password authentication, but you still might run into the above error if your database user isn’t using the new default plugin. Ayesh Karunaratne published an in-depth post on fixing this issue, you should check it out for the real fix!

However, if you’re on an older version of PHP, follow along to get MySQL 8 working with PDO:

First, we need to find the paths MySQL will look for a configuration file. We can find out by running mysql --help. The command prints out a lot of info, but you’re looking for something like the following:

mysql --help
...
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf

Take the paths from your command output and adapt the following with the paths you get from mysql --help:

ls -la \
/etc/my.cnf \
/etc/mysql/my.cnf \
/usr/local/etc/my.cnf \
~/.my.cnf
 
ls: /etc/my.cnf: No such file or directory
ls: /etc/mysql/my.cnf: No such file or directory
-rw-r--r-- 1 paul staff 61 Dec 5 19:40 /Users/paul/.my.cnf
-rw-r--r-- 1 paul admin 113 Oct 28 2017 /usr/local/etc/my.cnf

I like to manage the file from ~/.my.cnf, but unless you’ve previously done that, you’ll probably have to create the file if you want to manage configuration from that path.

I suggest you use the ~/.my.cnf path, but whatever path you determine you need to add the following:

[mysqld]
default_authentication_plugin=mysql_native_password

If you’re using Homebrew and you want to update the /usr/local/etc/my.cnf file, add the following at the end of the file under [mysqld]:

# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 127.0.0.1
default_authentication_plugin=mysql_native_password

Last, you need to restart MySQL using whatever method is appropriate for your system. If you’re using Homebrew on a Mac, you can run brew services:

brew services restart mysql

If you’re using Docker, here’s an example Docker Compose configuration to get MySQL 8 working with Laravel and other PHP projects:

services:
# ...
mysql:
image: mysql:8.0
# PDO Doesn't support MySQL 8 caching_sha2_password Authentication
# @see https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password
command: --default-authentication-plugin=mysql_native_password
ports:
- 13306:3306
volumes:
- mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: my_database
MYSQL_ROOT_PASSWORD: root
volumes:
mysql:
driver: "local"

With that change in place, you should be able to connect to MySQL 8 from PHP applications!

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes.

Visit Larafast: Laravel SaaS Starter Kit
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
Larafast: Laravel SaaS Starter Kit logo

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with Vue and Livewire stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
New Proposed Array Find Functions in PHP 8.4 image

New Proposed Array Find Functions in PHP 8.4

Read article
Laracon AU 2024 tickets are now on sale image

Laracon AU 2024 tickets are now on sale

Read article
Dash UI is a Laravel Blade Component Library Inspired by Shopify Polaris image

Dash UI is a Laravel Blade Component Library Inspired by Shopify Polaris

Read article
A Look at What's Coming to PHP 8.4 image

A Look at What's Coming to PHP 8.4

Read article
Is class instantiation without extra parenthesis coming to PHP 8.4? image

Is class instantiation without extra parenthesis coming to PHP 8.4?

Read article
Validation Errors Card for Laravel Pulse image

Validation Errors Card for Laravel Pulse

Read article