简体   繁体   中英

New Laravel Install (with Sail) has unknown auth method with MySQL

I have read a number of questions discussing auth problems with MySql 8 however my problem persists. I removed ALL docker containers and created a new Laravel application, and here's what happened.

curl -s "https://laravel.build/example-app" | bash

The above command installed the application as expected.

cd my-app
./vendor/bin/sail up

The above command installed the docker images and spun them up. When I go to http://localhost I can see my application running, and I can access MySql from MySql Workbench using the default username (sail) and password (password).

php artisan migrate

The above command gives an error message as follows.

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known [...]

In Laravel's env file I updated the value of DB_HOST to 127.0.0.1, then tried the command again.

php artisan migrate

The above command gives a different error message this time around.

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

After Googling about I access MySql as root , and try to modify the authentication method for the sail user, as follows.

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

The above command gives as error message as follows.

ERROR 1396 (HY000): Operation ALTER USER failed for 'sail'@'localhost'

I tried this from MySql Workbench, and from the command line of the MySql container. I tried flushing privileges, but that didn't help.

I then tried modifying the authentication method for the root user.

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

This worked fine. However, I then updated the user to root in my application, flushed privileges, and ran the migration again, only to receive the same error message.

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

The Laravel version is 8.48.1 and the MySql version is 8.0.25.

How can I access MySql from my application?

This turned out to be VERY simple.

When you are executing artisan commands with Sail, you need to preface them with 'sail'. The correct way to run the migration above is:

./vendor/bin/sail artisan migrate

More details here: https://laravel.com/docs/8.x/sail#executing-artisan-commands

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM