Laravel: SQLSTATE[HY000] [2054] 服务器请求客户端未知的身份验证方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53607322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:11:24  来源:igfitidea点击:

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

phpmysqllaravelartisanmigrate

提问by medo ampir

after installing a new laravel app 5.7 and trying to migrate I get this error:

安装新的 Laravel 应用程序 5.7 并尝试迁移后,出现此错误:

Illuminate\Database\QueryException : SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client (SQL: select * from information_schema.tables where table_schema = xxx_db and table_name = migrations)

at C:\xampp\htdocs\xxxxx\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664 660| // If an exception occurs when attempting to run a query, we'll format the error 661| // message to include the bindings with SQL, which will make this exception a 662| // lot more helpful to the developer instead of just the database's errors. 663| catch (Exception $e) {

664| throw new QueryException( 665| $query, $this->prepareBindings($bindings), $e 666| ); 667| } 668|

Exception trace:

1 PDOException::("PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]") C:\xampp\htdocs\xxxxx\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70

2 PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=xxx_db ", "root", "**********", []) C:\xampp\htdocs\xxxxx\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70

Please use the argument -v to see more details.

Illuminate\Database\QueryException : SQLSTATE[HY000] [2054] 服务器请求客户端未知的身份验证方法(SQL:select * from information_schema.tables where table_schema = xxx_db and table_name = migrations)

在 C:\xampp\htdocs\xxxxx\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664 660| // 如果尝试运行查询时发生异常,我们将格式化错误 661| // 包含与 SQL 的绑定的消息,这将使此异常成为 662| // 对开发人员更有帮助,而不仅仅是数据库的错误。663| 捕获(异常 $e){

664| throw new QueryException(665| $query, $this->prepareBindings($bindings), $e 666|); 667| 第668话

异常跟踪:

1 PDOException::("PDO::__construct(): 服务器请求了客户端未知的身份验证方法 [caching_sha2_password]") C:\xampp\htdocs\xxxxx\vendor\laravel\framework\src\Illuminate\Database\Connectors\连接器.php:70

2 PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=xxx_db", "root", "**********", []) C:\xampp\htdocs \xxxxx\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70

请使用参数 -v 查看更多详细信息。

回答by Aremu Ibrahim

  1. Re installed MySQL choosing Legacy Authentication Method as shown in iamge attached SQL Authentication method

  2. Database parameters in .env as follows

    DB_CONNECTION=mysql  
    DB_HOST=127.0.0.1  
    DB_PORT=3306  
    DB_DATABASE=database_name  
    DB_USERNAME=database_username  
    DB_PASSWORD=database_password(if any)  
    
  3. Database parameters in config/database.php

    'mysql' => [  
        'driver' => 'mysql',  
        'url' => env('DATABASE_URL'),  
        'host' => env('DB_HOST', '127.0.0.1'),  
        'port' => env('DB_PORT', '3306'),  
        'database' => env('DB_DATABASE', 'database_name'),  
        'username' => env('DB_USERNAME', 'database_username'),  
        'password' => env('DB_PASSWORD', 'database_password'),  
        'unix_socket' => env('DB_SOCKET', ''),  
        'charset' => 'utf8mb4',  
        'collation' => 'utf8mb4_unicode_ci',  
        'prefix' => '',  
        'prefix_indexes' => true,  
        'strict' => true,  
        'engine' => null,  
        'options' => extension_loaded('pdo_mysql') ? array_filter([  
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),  
        ]) : [],  
    ],  
    
  4. Run php artisan migratefrom the projects terminal

  1. 重新安装 MySQL,选择 Legacy Authentication Method,如 iamge 附加的SQL Authentication method 所示

  2. .env中的数据库参数如下

    DB_CONNECTION=mysql  
    DB_HOST=127.0.0.1  
    DB_PORT=3306  
    DB_DATABASE=database_name  
    DB_USERNAME=database_username  
    DB_PASSWORD=database_password(if any)  
    
  3. config/database.php 中的数据库参数

    'mysql' => [  
        'driver' => 'mysql',  
        'url' => env('DATABASE_URL'),  
        'host' => env('DB_HOST', '127.0.0.1'),  
        'port' => env('DB_PORT', '3306'),  
        'database' => env('DB_DATABASE', 'database_name'),  
        'username' => env('DB_USERNAME', 'database_username'),  
        'password' => env('DB_PASSWORD', 'database_password'),  
        'unix_socket' => env('DB_SOCKET', ''),  
        'charset' => 'utf8mb4',  
        'collation' => 'utf8mb4_unicode_ci',  
        'prefix' => '',  
        'prefix_indexes' => true,  
        'strict' => true,  
        'engine' => null,  
        'options' => extension_loaded('pdo_mysql') ? array_filter([  
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),  
        ]) : [],  
    ],  
    
  4. php artisan migrate从项目终端运行

回答by shivanikoko

By Providing DB_SOCKET in .env file this issue can be resolved like this :

通过在 .env 文件中提供 DB_SOCKET 这个问题可以这样解决:

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

回答by Shabeer Sha

This query solved my problem.

这个查询解决了我的问题。

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

回答by Jayjay

Go to your .env file and make sure DB_CONNECTION=mysqland DB connections are correct.

转到您的 .env 文件并确保DB_CONNECTION=mysql数据库连接正确。