How to Fix It: Error connecting to database: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client with PHP and MySQLd

So you updated MySQLd, or perhaps you just updated your distro, like to Ubuntu 20 LTS or whatever the latest and great flavor of the day is… And now you keep getting these pesky errors every time you try to use your website.

Error connecting to database: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

You know you have the right username and password for your database. You can likely even connect to your db via the mysql command line.

Well, MySQL version 8 has some problems with certain versions of PHP. (For more details you can see this post from PHP.) MySQL changed the default password type, and older versions of PHP have not adapted to this yet.

The Fix

So how do you fix the “Error connecting to database: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client” error? Very easy!

Modify your mysqld.cnf file for your MySQLd daemon. This is typically someplace like /etc/mysql/. In Ubuntu 20 LTS it is in /etc/mysql/mysql.conf.d/mysqld.cnf

In Ubuntu 20 LTS use the following:

# echo 'default_authentication_plugin=mysql_native_password' >>  /etc/mysql/mysql.conf.d/mysqld.cnf 

# systemctl restart mysql

The line above uses your shell to append (>>) the correct line to the mysqld.cnf file.

In other linux or *nix distributions:

Just edit your mysqld.cnf file and add the following line at the end:

 default_authentication_plugin=mysql_native_password 

Then restart the MySQL server using your system’s method for doing so. This is often something like:

# service mysql restart

Or perhaps:

# service mysqld restart

Or sometimes:

 /etc/init.d/mysqld restart

On FreeBSD it would be something like:

# /usr/local/etc/rc.d/mysql-server stop
# /usr/local/etc/rc.d/mysql-server start

You get the idea, and can always web search for your OS.

Basically this tells MySQL to use the old password style by default and all your old PHP scripts should work just fine again. You will have fixed the “Error connecting to database: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client”

Enjoy.

This entry was posted in Uncategorized and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.