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

PhpMysqlMysql 8.0

Php Problem Overview


I'm running MySQL version 8 on PHP 7.0.

I'm getting the following error when I try to connect to my database from PHP:

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

PHP might show this error

> Warning: mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password] in D:\xampp\htdocs\reg\server.php on line 10

How can I fix this problem?

Php Solutions


Solution 1 - Php

@mohammed, this is usually attributed to the authentication plugin that your mysql database is using.

By default and for some reason, mysql 8 default plugin is auth_socket. Applications will most times expect to log in to your database using a password.

If you have not yet already changed your mysql default authentication plugin, you can do so by:

  1. Log in as root to mysql

  2. Run this sql command:

    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
    Replace 'password' with your root password. In case your application does not log in to your database with the root user, replace the 'root' user in the above command with the user that your application uses.

Digital ocean expounds some more on this here Installing Mysql

Solution 2 - Php

You have to change MySQL settings. Edit my.cnf file and put this setting in mysqld section:

[mysqld]
default_authentication_plugin= mysql_native_password

Then run following command:

FLUSH PRIVILEGES;

Above command will bring into effect the changes of default authentication mechanism.

Solution 3 - Php

I've tried a lot of other solutions, but only this works for me.

Thanks for the workaround.

Check your .env

MYSQL_VERSION=latest

Then type this command

$ docker-compose exec mysql bash
$ mysql -u root -p 

(login as root)

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
ALTER USER 'default'@'%' IDENTIFIED WITH mysql_native_password BY 'secret';

Then go to phpmyadmin and login as :

  • host -> mysql
  • user -> root
  • password -> root

Hope it helps

Solution 4 - Php

None of the answers here worked for me. What I had to do is:

  1. Re-run the installer.
  2. Select the quick action 're-configure' next to product 'MySQL Server'
  3. Go through the options till you reach Authentication Method and select 'Use Legacy Authentication Method'

After that it works fine.

Solution 5 - Php

Faced the same problem, I was not able to run wordpress docker container with mysql version 8 as its default authentication mechanism is caching_sha2_password instead of mysql_native_password.

In order to fix this problem we must reset default authentication mechanism to mysql_native_password.

Find my.cnf file in your mysql installation, usually on a linux machine it is at the following location - /etc/mysql

Edit my.cnf file and add following line just under heading [mysqld]

default_authentication_plugin= mysql_native_password

Save the file then log into mysql command line using root user

run command FLUSH PRIVILEGES;

Solution 6 - Php

I'm using Laravel Lumen to build a small application.
For me it was because I didn't had the DB_USERNAME defined in my .env file.

DB_USERNAME=root

Setting this solved my problem.

Solution 7 - Php

In my.cnf file check below 2 steps.

  • check this value -

    old_passwords=0;

    it should be 0.

  • check this also-

    [mysqld] default_authentication_plugin= mysql_native_password Another value to check is to make sure

    [mysqld] section should be like this.

Solution 8 - Php

preferences -> mysql -> initialize database -> use legacy password encryption(instead of strong) -> entered same password

as my config.inc.php file, restarted the apache server and it worked. I was still suspicious about it so I stopped the apache and mysql server and started them again and now it's working.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmohammed yaserView Question on Stackoverflow
Solution 1 - PhpElias GikonyoView Answer on Stackoverflow
Solution 2 - Phpmichail_wView Answer on Stackoverflow
Solution 3 - PhpFrancesco TaioliView Answer on Stackoverflow
Solution 4 - PhpStuperfiedView Answer on Stackoverflow
Solution 5 - PhpShivinder SinghView Answer on Stackoverflow
Solution 6 - PhpCIRCLEView Answer on Stackoverflow
Solution 7 - PhpsanjayaView Answer on Stackoverflow
Solution 8 - PhpTrần ĐứcView Answer on Stackoverflow