Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
MaximilianCoutuer opened this issue Feb 23, 2018 · 72 comments
Labels

Comments

@MaximilianCoutuer
Copy link

MaximilianCoutuer commented Feb 23, 2018

Info:

  • Docker version ($ docker --version): Docker version 17.12.0-ce, build c97c6d6
  • Laradock commit ($ git rev-parse HEAD): 8323c659ed588ad2d2b02a13dfab651c051302e8
  • System info (Mac, PC, Linux): MacOS Sierra 10.12.6
  • System info disto/version:

Issue:

Using the mysql and nginx containers, running a migration yields the following error: The server requested authentication method unknown to the client [caching_sha2_password].

This seems to be due to mysql 8.x forcing a new password format. Attempting to force old_passwords=1 from inside the mysql container yields ERROR 1231 (42000): Variable 'old_passwords' can't be set to the value of '1'.

This does occur when trying to run a migration from inside the workspace - where both the version of php and mysql should be controlled by Laradock.


Expected behavior:

Laravel should be able to login to the database and do its thing.


Reproduce:

Pull clean copy of Laradock, set DB_HOST=127.0.0.1 in .env (for reasons unknown to me, DB_HOST=mysql yields SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known), start the mysql and nginx containers, enter the workspace container and run php artisan migrate.


@adammparker
Copy link

mysql works for me inside the .env file, but otherwise I'm experiencing the exact same problem.

Additionally, I'm using PHP_VERSION=56 in laradock/.env (have an older 5.1 Laravel project I need to stand up and don't want to troubleshoot Valet issues with older version(s) of PHP.

php artisan migrate returns two errors:

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

  [ErrorException]
  PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]

@Drakota
Copy link

Drakota commented Feb 25, 2018

+1, I'm having the same problem here.

@anh-ngo-saltovn
Copy link

anh-ngo-saltovn commented Feb 25, 2018

Me too, same problem .

i did resolved the problem following:

  1. add default authentication plugin to laradock/mysql/my.cnf
[mysqld]
default_authentication_plugin= mysql_native_password
  1. update content file mysql/docker-entrypoint-initdb.d/createdb.sql like:
CREATE USER 'admin'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpass';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;
CREATE USER 'admin'@'%' IDENTIFIED WITH mysql_native_password BY 'yourpass';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' WITH GRANT OPTION;
#
CREATE DATABASE IF NOT EXISTS `yourdb` COLLATE 'utf8_general_ci' ;
GRANT ALL ON `yourdb`.* TO 'admin'@'%' ;
FLUSH PRIVILEGES ;

important note: remove old user, database and recreate again by above script.
3. Remove mysql container and re-run it.
4. php artisan migrate is done :)

Reason bug:
Default authenticate of MySQL 8.0.4 changed.
Refer to detail information: https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-4.html
at Security Notes

hope it help you.
Thanks

@adammparker
Copy link

Thanks @rockerbom for the thorough response. I didn't try your steps, but ended up with a different solution (for anyone that doesn't necessarily need MySQL 8.x):

  1. Changed MYSQL_VERSION to 5.7 in laradock/.env
  2. docker-compose build --no-cache mysql

@danaia
Copy link

danaia commented Feb 26, 2018

@rockerbom This worked for me after a long time of forensics. Also solved how to create a DB. @adammparker I tried your option and bring everything back to 5.7 but I was not able to bash into the container for some reason.

@adammparker
Copy link

@danaia I think I may have experienced something similar. Rather than debugging I just nuked all of my Docker settings, containers, and images and just started from scratch (with the older [and more stable] version of MySQL).

@anh-ngo-saltovn
Copy link

@danaia nice man

@MaximilianCoutuer
Copy link
Author

I ended up with a similar solution as @adammparker. Thanks @rockerbom for an alternative. 👍

@wmellema
Copy link

Getting the same issue with php 7.1, any ideas on how to fix it for 7.1 without redoing all the Docker entrypoints?

@anh-ngo-saltovn
Copy link

@wmellema are you instaill php-cli?
could you share .env file?

@danaia
Copy link

danaia commented Mar 1, 2018

Have a stable solution after a few days banging thy head. MySQL 8.0 is not stable at all for me. I am surprised it is a part of Laradock. Does anyone know why a dev version is included VS a production version? Once I downgraded to 5.7 all errors above are gone and my head is healing. Like to suggest to the developers of Laradock to please use a stable version of MySQL so others may not need to travel the road of misery.

@marcosdsdba
Copy link

Some erros
The server requested authentication method unknown to the client

And downgrade mysql version to 5.6 or 5.7 doesn't work

@bestlong
Copy link
Member

bestlong commented Mar 2, 2018

Hi everyone, I think, always check config file new different after upgrade laradock is good practice.

$ diff .env env-example

not just copy paste.

@silsuer
Copy link

silsuer commented Mar 7, 2018

@adammparker I changed Mysql verision to 5.7 in .env, and rebuild it. I can run it, but its exit 2 when I use docker-compose ps, could you help me?

@capripio
Copy link

capripio commented Mar 7, 2018

@silsuer , agreed same issue occurred exit 6 to me thought!

@silsuer
Copy link

silsuer commented Mar 7, 2018

@capripio I resolved it with https://github.com/laradock/laradock/issues/955

My 'solution' - don't use DATA_SAVE_PATH
(in docker-compose.yml)

@EngEryx
Copy link

EngEryx commented Mar 7, 2018

@silsuer I had the same issue.

I set MYSQL_VERSION to 5.7,
Further removed DATA_SAVE_PATH
Then removed MYSQL_ROOT_USER since I realized an issue with Docker container for MySQL ...it already creates the root user as root. So when I removed it all worked fine.

I am also hosting multiple projects and all is working well without extra patches.

Would like to know who is still having an issue I can help...

@robindijkhof
Copy link

robindijkhof commented Apr 23, 2018

I'm NOT getting the error with mysql 8.0.0. This version might also be working.

@marcosdsdba
Copy link

marcosdsdba commented Apr 23, 2018

@robindijkhof Use this to solve problem :
#1407

**stop all service docker-compose down
Edit .env file set MYSQL_VERSION=5.7 or MYSQL_VERSION=latest

Delete mysql database

$ rm -rf ~/.laradock/data/mysql

rebuild mysql image

$ docker-compose build mysql

retry

$ docker-compose up -d nginx mysql phpmyadmin
It's working for me.**

@robindijkhof
Copy link

@MattBidas I'm NOT getting the error with v8.0.0. Just posting it as another possible solution.

@marcosdsdba
Copy link

@robindijkhof Alles Gute ! Sorry ;-)

@consule
Copy link

consule commented Jul 4, 2019

Only run it:
ALTER USER 'root'@'localhost'
IDENTIFIED WITH mysql_native_password BY 'new_password'

espipj added a commit to espipj/laradock that referenced this issue Aug 4, 2019
@iambpn
Copy link

iambpn commented Aug 23, 2019

i have solved this problem. in my case i am having this problem with my xampp maria db (10.1.38) and php(7.3.3) while connecting database through PDO.
try catch exception message was same.
in my case i have installed both xampp mariadb and mysql server(8.0) in my pc so i think pdo is confused in choosing one between them. so i added the port. like this to my code
image

and it worked fine for me. here i changed maria db port so it is on 3308 because 3306 is used by mysql server.
hope this is helpful. this is my first time posting answer in form so i apologize if its not clear.

@harishAxis
Copy link

Hi Guys,
Please help me out ,im trying to connect db through Drupal im geeting below error.

mysql --version

mysql Ver 8.0.17 for Linux on x86_64 (MySQL Community Server - GPL)

php-fpm -version

PHP 7.2.21

Capture

@iambpn
Copy link

iambpn commented Aug 26, 2019

@harishAxis have you tried adding port to your localhost in you code. like 127.0.0.1:port_of_your_db.

@harishAxis
Copy link

@harishAxis have you tried adding port to your localhost in you code. like 127.0.0.1:port_of_your_db.

No ,from were i have to change.

@harishAxis
Copy link

harishAxis commented Aug 26, 2019 via email

@harishAxis
Copy link

harishAxis commented Aug 26, 2019 via email

@iambpn
Copy link

iambpn commented Aug 26, 2019

@harishAxis which mysql server you are using??

@iambpn
Copy link

iambpn commented Aug 26, 2019

oops sorry i didn't see that you have mention that at the top.
follow this link this could be helpful for you.
https://stackoverflow.com/questions/52364415/php-with-mysql-8-0-error-the-server-requested-authentication-method-unknown-to

@harishAxis
Copy link

harishAxis commented Aug 26, 2019 via email

@iambpn
Copy link

iambpn commented Aug 26, 2019

@harishAxis follow the link i shared above.

@shivanibenosoft
Copy link

I am using laravel 5.8 and having MAMP server got this error resolved by adding DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock in .env file like below


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=dbname
DB_USERNAME=root
DB_PASSWORD=root
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

@Richard2012
Copy link

We had the same error, although not in laravel environment, and have solved it by creating a new db-user, without touching the database. Details here:
https://cubansky.com/labs/oneoff/hostgatorbug.php

@coinclear-uk
Copy link

Access your mysql from console using

docker-compose exec mysql mysql -uroot -proot

then create a new user with a native mysql password

CREATE USER 'admin'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpass';
GRANT ALL PRIVILEGES ON . TO 'admin'@'localhost' WITH GRANT OPTION;
CREATE USER 'admin'@'%' IDENTIFIED WITH mysql_native_password BY 'yourpass';
GRANT ALL PRIVILEGES ON . TO 'admin'@'%' WITH GRANT OPTION;

access phpmyadmin using your new user, change the password of your root user selecting Mysql Native Authentication method and then remove the user you've created, this works for me

Going through ALL of the above this was the only solution that worked for me

@99linesofcode
Copy link
Contributor

99linesofcode commented Dec 8, 2019

Cause

This is caused by an update to libmysqlclient since MySQL version 8.0.4 which has changed the default authentication method to a more secure sha256_password.

As soon as Laravel and other software packages such as PhpMyAdmin are updated to support this new and more secure authentication method this problem wil resolve itself.

How to fix

If you CANNOT delete and rebuild your existing mysql data volume, simply:

  1. Sign in to your mysql container (docker-compose exec mysql mysql -uroot -proot)
  2. Execute ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
  3. Repeat step 2 for any other users you may have created.

If you CAN recreate your mysql data volume:

  1. Modify your my.conf file. Under [mysqld] add the following configuration option default_authentication_plugin=mysql_native_password
  2. Delete your mysql volume rm -rf ~./laradock/data/mysql
  3. Rebuild the mysql container docker-compose -d --build mysql phpmyadmin

@fth-kvmp
Copy link

For me it works every time i vagrant ssh and execute the command php artisan migrate

@programarivm
Copy link

programarivm commented Jan 19, 2020

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]

@stale
Copy link

stale bot commented Apr 18, 2020

Hi 👋 this issue has been automatically marked as stale 📌 because it has not had recent activity 😴. It will be closed if no further activity occurs. Thank you for your contributions ❤️.

@stale stale bot added the Stale label Apr 18, 2020
@crisdiehard
Copy link

For those running old versions of MySQL, I ended up with a similar issue using the 5.5 release.

As previously stated by @anh-ngo-saltovn and @99linesofcode, MySQL 8.0.4 authentication plugin defaults to caching_sha2_password and until Laravel and other software packages are updated to support the new authentication method, laradock is fallbacking to the mysql_native_password authentication plugin setting the default_authentication_plugin server variable in the my.cnf file.

As this server variable was introduced only in MySQL 5.6, docker-compose will fail to start the mysql 5.5 container with the following error:

[ERROR] /usr/local/mysql/bin/mysqld: unknown variable 'default-authentication-plugin=mysql_native_password'

To solve the problem, do the following:

  1. Comment out the default-authentication-plugin server variable definition in the my.cnf file (located at laradock/mysql/) directory:

    #default-authentication-plugin=mysql_native_password

  2. Rebuild the mysql docker image:

    docker-compose build mysql

  3. Delete the mysql volume:

    rm -rf ~./laradock/data/mysql

  4. Restart the mysql container:

    docker-compose up -d mysql

@stale stale bot removed the Stale label Apr 23, 2020
@vinithmathews
Copy link

Only run it:
ALTER USER 'root'@'localhost'
IDENTIFIED WITH mysql_native_password BY 'new_password'

For me, this solution worked. I had created the user in the old format, i.e CREATE USER 'root'@'localhost' IDENTIFIED BY ''

@stale
Copy link

stale bot commented Aug 8, 2020

Hi 👋 this issue has been automatically marked as stale 📌 because it has not had recent activity 😴. It will be closed if no further activity occurs. Thank you for your contributions ❤️.

@stale
Copy link

stale bot commented Aug 29, 2020

Hi again 👋 we would like to inform you that this issue has been automatically closed 🔒 because it had not recent activity during the stale period. We really really appreciate your contributions, and looking forward for more in the future 🎈.

@stale stale bot closed this as completed Aug 29, 2020
@mohamedhafezqo
Copy link

mohamedhafezqo commented Jan 9, 2021

I resolved by deleting my docker binding volume and adding the --default-authentication-plugin=mysql_native_password command for MySQL service

    database:
        image: mysql
        container_name: database-container
        environment:
            MYSQL_ROOT_PASSWORD: sqlpass
        command: ["--default-authentication-plugin=mysql_native_password"]
        ports:
            - 3306:3306
        volumes:
            - ./docker/mysql-data:/var/lib/mysql
        networks:
            - symfony_test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.