PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client

PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client

When upgrading a database from MySQL 5.x to version 8.0, one of the most immediate friction points is the default authentication plugin change. In older versions, mysql_native_password was the standard for user authentication. However, starting with MySQL 8.0, the default switch has been flipped to caching_sha2_password. This algorithm offers better security by caching a password hash in memory and using SHA-256 hashing, but it creates a compatibility wall for clients that do not support this new protocol by default.

If you are running PHP 7.0 against a MySQL 8.0 server, you will likely encounter the SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client error. The specific warning often reads: mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password]. This happens because the PHP MySQLi extension bundled with version 7.0 defaults to trying mysql_native_password first, and if the server responds with caching_sha2_password, the client fails immediately because it does not have the necessary code to handle that hash format.

The root cause is a mismatch in capabilities between the server and the client library. The server insists on using its preferred, more secure method, while the PHP library is stuck on legacy support methods. To resolve this without upgrading your PHP runtime (which might be constrained by other legacy dependencies), you must align the authentication expectations.

The most direct solution involves altering the user account on the MySQL server to use the legacy plugin. You can connect to the database using a different client (like mysql CLI or another application) that supports both methods and run an ALTER USER command. For example:

ALTER USER 'your_username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';
FLUSH PRIVILEGES;

This forces the server to accept the legacy hash format when your PHP application connects. While this solves the immediate error, it is important to understand that mysql_native_password has known collision vulnerabilities (specifically the "hash-collision attack") which were addressed in MySQL 8.0 by making caching_sha2_password the default. Using the legacy plugin essentially reverts a security improvement.

However, there is an alternative approach if you cannot alter user accounts or prefer to keep the server configuration as-is. You can update the PHP client library configuration to explicitly request the legacy authentication method. In PHP 7.0, this is achieved by setting the MYSQLI_INIT_COMMAND option before connecting. This forces the client to send a flag indicating it prefers mysql_native_password.

In your connection code, ensure you pass this parameter:

$mysqli = new mysqli(
    $host, 
    $user, 
    $password, 
    $database,
    "SET default_authentication_plugin='mysql_native_password'"
);

Alternatively, if using the PDO extension instead of MySQLi, you can set the attribute PDO::MYSQL_ATTR_AUTH_PLUGIN directly. Note that this specific attribute is often only available in PHP versions 7.1 and later via mysqli_set_option, but since you are on PHP 7.0, the server-side user alteration remains the most reliable path for PHP 7.0 specifically.

It is crucial to recognize that upgrading your PHP runtime is often the long-term recommendation. Modern PHP versions (8.x) include native support for caching_sha2_password by default, allowing seamless interoperability with MySQL 8.0 without any configuration hacks. If your application stack allows for an upgrade, addressing this incompatibility by modernizing the PHP version eliminates the need to downgrade security protocols on the database side.

If you are managing a Laravel project, you might consider leveraging resources that discuss modernizing legacy stacks. For instance, LaravelCompany offers insights into maintaining robust applications, though for specific MySQL 8 integration issues, sticking to standard MySQL administration practices is generally more effective than relying on framework-specific workarounds unless the framework version itself has updated its database drivers.

Ultimately, fixing this error requires a conscious decision between security and compatibility. Downgrading the user's authentication plugin restores connectivity but lowers the security posture of that specific account. Upgrading PHP or the MySQL client library restores both connectivity and security alignment.

Our network