【问题标题】:How to get MySql 8 to run with laravel? [duplicate]如何让 MySql 8 与 laravel 一起运行? [复制]
【发布时间】:2019-03-22 17:19:18
【问题描述】:

我很难让 MySQL 8 正常工作。这是每次我尝试php artisan migrate 时出现的错误。到目前为止,我只重新安装了一次 MySQL,因为我不想再因为发生的事情而伤脑筋。我已经从其他可能的答案中编辑了database.php,但这似乎也不起作用。我看到了一个可能的答案,这是因为 MySQL 8 对 root 密码进​​行了 sha256 加密,这就是为什么我想回到 MySQL 5.7 的原因,我查找过的 MySQL 5.7 可以很好地与 laravel 一起使用。不过,我想保持包是最新的,并且只有在我可以让它与 laravel 一起工作的情况下才保持 MySQL 8。

PHP 7.2

如何让 MySQL 8 与 Laravel 一起工作?

 'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'version' => 8,
            'modes' => [
                'ONLY_FULL_GROUP_BY',
                'STRICT_TRANS_TABLES',
                'NO_ZERO_IN_DATE',
                'NO_ZERO_DATE',
                'ERROR_FOR_DIVISION_BY_ZERO',
                'NO_ENGINE_SUBSTITUTION',
            ],
        ],

``

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

  at /Users/home/Projects/laravel_tut/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]")
      /Users/home/Projects/laravel_tut/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  2   PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=laravel_tut", "root", "fdgkadgaf9g7ayaig9fgy9ad8fgu9adfg9adg", [])
      /Users/home/Projects/laravel_tut/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

更新我为解决此问题所做的另一个修复: 全新安装 MySQL 后,我在设置中选择 NO 来加密密码(使用传统加密,而不是 SHA 加密),它开始与 Laravel 一起工作,没有任何问题 - 只需使用长而强的密码。 安装步骤参考: https://www.percona.com/blog/wp-content/uploads/2018/05/Installing-MySQL-8.0-on-Ubuntu-2.png

【问题讨论】:

  • 好吧,laravel 并没有直接与 mysql 接口,实际上,php 使用某种插件(PDO)处理连接。所以开始,请告诉我们php版本,你可以试试this,或者this official documentation。 PDO似乎不容易支持mysql 8 ..

标签: php mysql laravel ubuntu server


【解决方案1】:

由于PHP不理解caching_sha2_password,将用户设置回mysql_native_password

ALTER USER 'forge'@'localhost'
IDENTIFIED WITH mysql_native_password BY 'new_password'

【讨论】:

  • 这对我有用,添加到 my.ini default_authentication_plugin=mysql_native_password 并删除旧的 caching_sha2_password 值。
  • 在使用 MySQL 8.0+ 并进行 Magento 2.3.1 Web 安装时,这对我有用。不要忘记添加“;”如果您不想进行其他更改,请在命令末尾添加字符。
【解决方案2】:

当运行7.1.16之前的PHP版本,或者7.2.4之前的PHP 7.2时,将MySQL 8 Server的默认密码插件设置为mysql_native_password,否则你会看到类似The server requested authentication method unknown to the client [caching_sha2_password]的错误即使没有使用 caching_sha2_password。

https://www.php.net/manual/en/mysqli.requirements.php

这也会有所帮助:

https://php.watch/articles/PHP-7.4-MySQL-8-server-gone-away-fix

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    相关资源
    最近更新 更多