Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does mysqli have support for caching_sha2_password in PHP 7.4?

When I tried upgrading from PHP 7.3 to PHP 7.4, I received this error:

Unexpected server response while doing caching_sha2 auth 109

As I see it, this indicates that PHP 7.4 MySQLi is trying to use the caching_sha2_password plugin. This article points out that PHP MySQLi does not support the plugin (it also hints future support for it), but since PHP 7.4 is new and seems to be trying to use it, I guess it should work. Also the error message is different, than if it wasn't supported (access denied vs. authentication method unknown).

So I changed my MySQL authentication plugin to caching_sha2_password (using the same password as before):

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '';
FLUSH PRIVILEGES;

But this caused another error:

Access denied for user 'root'@'localhost' (using password: YES).

Switching back to PHP 7.3 and mysql_native_password it works again.

I used the same password for both plugins, the same website and applied the same php.ini changes. I however did not change any mysqli configuration. The logs for MySQL show nothing, the apache2 log only shows the 'Access Denied' error message.

Does php7.4-mysqli have support for caching_sha2_password? YES

Why is my password being denied and how can I fix it? See my follow-up question

Also, if MySQLi still doesn't support the plugin: How can I use mysql_native_password with it?

like image 770
Minding Avatar asked Dec 07 '19 23:12

Minding


People also ask

Is MySQLi deprecated in PHP 7?

The oldest one uses the MySQL extension, which was deprecated as of PHP 5.5 and fully removed in PHP 7. The mysql() function no longer works in PHP 7. It has been replaced with mysqli().

What version of PHP is MySQLi?

The MySQLi extension was introduced with PHP version 5.0. 0.

How can I tell if MySQLi is enabled in PHP?

Check if MySQLi is Installed You can do that by visiting a phpinfo() page that you made, or by running this command: php -m | grep mysqli.

What is MySQLi PHP extension?

The MySQLi Extension (MySQL Improved) is a relational database driver used in the PHP scripting language to provide an interface with MySQL databases. There are three main API options when considering connecting to a MySQL database server: PHP's MySQL Extension. PHP's MySQLi Extension.

Does MySQL_xdevapi support caching_sha2_password?

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 MySQL with caching_sha2_password, although it is a bit unclear around older versions, there seems to be conflicting reports.

Does PHP MySQLi extension support caching_sha2 authentication?

As of PHP 7.4, this is no longer an issue. Support for caching_sha2 authentication method has been added to mysqlnd. Currently, PHP mysqli extension do not support new caching_sha2 authentication feature. You have to wait until they release an update.

Which version of PHP is compatible with MySQL 8?

Bottom line: It appears that only PHP 7.2 and above is compatible with MySQL 8.0 (the version currently used by DO’s managed MySQL dbs). I’m happy to hear that you’ve got it working at the end!

Is it possible to get the caching_sha2_password error?

I tried the query and this is the response. If you use the tempuser user in your MySQL connection string, there would be no way to get the caching_sha2_password error as the user is not using that plugin.


2 Answers

I tested this, and mysqli in PHP 7.4 does support the caching_sha2_password.

php -v
PHP 7.4.0 (cli) (built: Nov 29 2019 16:18:44) ( NTS )

Here's a simple PHP script I wrote to connect to MySQL 8.0.17 running in a docker container (using port 6603):

<?php
error_reporting(E_ALL);
$conn = new mysqli('127.0.0.1', 'root', 'root', '', 6603);
$result = $conn->query("SELECT NOW()");
$now = $result->fetch_row()[0];
echo "$now\n";

I confirmed that my MySQL instance is using caching_sha2_password:

mysql> select user,host,plugin from mysql.user where user='root';
+------+-----------+-----------------------+
| user | host      | plugin                |
+------+-----------+-----------------------+
| root | %         | caching_sha2_password |
| root | localhost | caching_sha2_password |
+------+-----------+-----------------------+

mysql> select @@default_authentication_plugin;
+---------------------------------+
| @@default_authentication_plugin |
+---------------------------------+
| caching_sha2_password           |
+---------------------------------+

Running my PHP script, it was successful.

php my.php
2019-12-08 18:11:58

However, when I tried to change the password to '' like you did, I was able to reproduce the same error:

PHP Warning: mysqli::__construct(): Unexpected server response while doing caching_sha2 auth: 0 in /Users/bkarwin/Documents/SO/my.php on line 5

When I restored the password, setting it to a non-blank string, it fixed the issue.

Don't use blank passwords.

like image 51
Bill Karwin Avatar answered Oct 16 '22 15:10

Bill Karwin


There's a couple of ways this could happen

  1. You're using the mysql_client method of authentication. This is how PHP used to do it natively, by connecting to MySQL through the MySQL CLI interface. MySQL's own client will always support its own authentication methods.
  2. You're using MySQLND under 7.4. Apparently this was an improvement to the native driver that went unannounced

    Yes, this is only in 7.4 right now, due to the hard dependency on ext/hash.

    There are still some possible issues with it.

like image 32
Machavity Avatar answered Oct 16 '22 15:10

Machavity