The server requested authentication method unknown to the client ошибка

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:xampphtdocsregserver.php on line 10

How can I fix this problem?

Machavity's user avatar

Machavity

30.7k27 gold badges91 silver badges100 bronze badges

asked Sep 17, 2018 at 9:17

mohammed yaser's user avatar

mohammed yasermohammed yaser

2,2612 gold badges12 silver badges16 bronze badges

2

@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

answered Dec 21, 2018 at 8:09

Elias Gikonyo's user avatar

8

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.

George's user avatar

George

6,7593 gold badges44 silver badges56 bronze badges

answered Sep 17, 2018 at 9:18

michail_w's user avatar

michail_wmichail_w

4,2534 gold badges26 silver badges43 bronze badges

10

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

starball's user avatar

starball

16k6 gold badges29 silver badges147 bronze badges

answered Dec 23, 2018 at 17:38

Francesco Taioli's user avatar

Francesco TaioliFrancesco Taioli

2,6471 gold badge19 silver badges34 bronze badges

11

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.

answered May 12, 2019 at 12:11

Stuperfied's user avatar

StuperfiedStuperfied

3222 silver badges10 bronze badges

3

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;

answered Apr 21, 2019 at 18:03

Shivinder Singh's user avatar

1

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.

answered Feb 23, 2019 at 16:12

CIRCLE's user avatar

CIRCLECIRCLE

4,4405 gold badges36 silver badges56 bronze badges

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.

answered Feb 24, 2019 at 5:01

sanjaya's user avatar

sanjayasanjaya

1942 gold badges3 silver badges11 bronze badges

2

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.

answered Apr 17, 2019 at 7:17

Tăng View Youtube - BUYFAN.NET's user avatar

0

Полностью ошибка звучит так

SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

Причины возникновения — связка MySQL 8 и PHP 7.  Дословно — при подключении к бд не воспринимается метод аутентификации. Причина в том, что в MySQL 8 используется методика аутентификации, отличная от предыдущих версий. В MySQL 8 была изменена схема хранения пароля, начиная с версии 8.0.4 длина хэша пароля увеличена до максимального (255 символов), а также изменили плагин аутентификации – вместо mysql_native_password используется caching_sha2_password. Потому, авторизоваться паролем, который был создан для подключения например пользователем bazauser при установке базы данных, не получиться.

Чтобы авторизация прошла достаточно выполнить следующую команду в консоли MySQL 

ALTER USER ‘bazauser’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘newpassword’;

где bazauser — ваше имя пользователя, newpassword — ваш пароль пользователя

Oops! MySQL just showed the error ‘The server requested authentication method unknown to the client‘.

Usually, the error shows up when a user tries to connect to the database.

The reason for this error is improper authentication plugin settings in MySQL.

At Bobcares, we often get requests to fix MySQL errors, as a part of our Server Management Services.

Today, let’s see how our Support Engineers fix this error.

How does the MySQL server authenticate a client?

A PHP application connects to the MySQL server using the username provided by the server/hosting provider.

The server validates the user and returns the connection status. MySQL uses caching_sha2_password and auth_socket plugins for validation.

The caching_sha2_password plugin uses an SHA-2 algorithm with 256-bit password encryption. MySQL 8 prefers this auth method.

Whereas the auth_socket plugin checks if the socket username matches with the MySQL username. If the names don’t match, it checks for the socket username of the mysql.user. If a match is found, the plugin permits the connection.

But to serve the pre 8.0 clients and avoid compatibility errors, it is preferred to revert back the auth method. Older versions of MySQL use mysql_native_password plugin for validation.

Why does MySQL show the authentication method unknown to the client?

Recently one of our customers approached us with a MySQL auth error. The error showed up in MySQL 8. But the user had a PHP version 7.0. The default authentication plugin used by the MySQL is auth_socket.

Here MySQL client like PHPMyAdmin authenticates the user to login to the database by a password. Hence when a user tries to access the database using PHPMyAdmin ends up in the auth error. But the actual reason was compatibility error.

The error message is ‘The server requested authentication method unknown to the client‘. In PHPMyAdmin, the error appears as,

MySQL the server requested authentication method unknown to the client

Now let’s see how our Support Engineers fix this error.

How we fix the error authentication method unknown to the client?

To fix this error we changed the default authentication plugin used by MySQL. For this, we logged in to MySQL as the root user. Then we run the command,

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Here we replaced the ‘password‘ with the password of the root user. If the database user is not root user, replace the username and password respectively.

Finally, this fixed the error. And the user was able to log in to PHPMyAdmin successfully.

But this is a temporary fix as the MySQL 8 uses PHP 7.0. The preferred auth plugin for this version is caching_sha2_password. 

Hence we also recommend the users to upgrade the pre 8.0 clients to avoid further errors.

[Still, having trouble in fixing MySQL errors? – We can help you.]

Conclusion

In short, MySQL error the server requested authentication method unknown to the client occurs due to the default authentication plugin used. Today, we saw how our Support Engineers changes this to fix the error.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = «owonCMyG5nEQ0aD71QM»;

I had received a similar issue like you which was:

(HY000/2054): The server requested authentication method unknown to the client
Server requested authentication method unknown to the client [caching_sha2_password] mysqli_real_connect():

This was after I upgraded my Mysql to 8.0.22. Mysql 8.0.22 uses caching_sha2_password as its main authentication plugin whilst the previous versions have been using mysql_native_password as their default authentication plugin. To resolve this one of the methods is to revert the MySQL version 8.0 to use MySQL_native_password as its default auth plugin. Tod do this(Ubuntu users):

cd /etc/mysql

Using your favorite text editor open the my.cnf file and after the [mysqld] add the code below.

default_authentication_plugin=mysql_native_password

after that restart service

systemctl restart mysql.service

and that should be able to fix your issue if at all we faced a similar issue.

The second option is to create a new user to DB and authenticate them using the native_password plugin. For full procedure how to do so follow this link

 https://www.digitalocean.com/community/tutorials/how-to-create-a-new-user-and-grant-permissions-in-mysql

It seems as if with PHP 7.4 the issue is solved.

The following minimalist Docker set up does the trick without even touching the configuration in my.cnf.

docker-compose.yml:

version: "3.6"

services:
  php_fpm:
      build: .
      container_name: cheetah_php_fpm
      working_dir: /cheetah
      volumes:
          - .:/cheetah
          - ./docker/php/php.ini:/usr/local/etc/php/php.ini

  mysql:
      image: mysql:8.0
      container_name: cheetah_mysql
      volumes:
          - ./docker/mysql/conf.d/mysql.cnf:/etc/mysql/conf.d/mysql.cnf
      ports:
          - "3306:3306"
      environment:
          - MYSQL_DATABASE=${DB_DATABASE}
          - MYSQL_ROOT_PASSWORD=${DB_PASSWORD}

Dockerfile:

FROM php:7.4-fpm

RUN apt-get update && apt-get install -y 
    git

RUN docker-php-ext-install pdo_mysql

RUN curl --silent --show-error https://getcomposer.org/installer | php && 
    mv composer.phar /usr/local/bin/composer

RUN pecl install mailparse

docker/mysql/conf.d/mysql.cnf:

# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA

#
# The MySQL  Client configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysql]

Возможно, вам также будет интересно:

  • The rise of the witch king ошибка при запуске
  • The requested item could not be located rufus ошибка
  • The request was aborted could not create ssl tls secure channel ошибка
  • The request has timed out ошибка
  • The remote name could not be resolved ошибка

  • Понравилась статья? Поделить с друзьями:
    0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии