如何让 MySql 8 与 laravel 一起运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52864853/
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
How to get MySql 8 to run with laravel?
提问by Zac
I'm having difficulty getting MySQL 8 to work. This is the error that appears everytime I attempt a php artisan migrate
. I've reinstalled MySQL only once so far because I didn't want to hurt my head anymore on what was going on. I've edited the database.php
from other possible answers, but that also doesn't seem to work. I saw a possible answer that it's because of MySQL 8's sha256 encryption of the root password, which is why I want to go back to MySQL 5.7 which I've looked up works with laravel just fine. Though, I want to keep the packages up to date and keep MySQL 8 only if i can get it to work with laravel.
我很难让 MySQL 8 工作。这是我每次尝试php artisan migrate
. 到目前为止,我只重新安装了 MySQL 一次,因为我不想再为发生的事情而头疼了。我已经编辑了database.php
其他可能的答案,但这似乎也不起作用。我看到一个可能的答案,这是因为 MySQL 8 对 root 密码进行了 sha256 加密,这就是为什么我想回到 MySQL 5.7,我已经查找过它与 laravel 一起工作得很好。不过,我想保持软件包是最新的,并且只有在我可以让它与 laravel 一起使用时才保持 MySQL 8。
PHP 7.2
PHP 7.2
How do I get MySQL 8 to work with Laravel?
如何让 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
UPDATE ANOTHER FIX I DID TO FIX THIS:With a fresh install of MySQL, i selected NO for encrypting passwords in the set up ( using legacy encryption, not the SHA encryption ) and it started to work with Laravel without any problems - Just use a long and strong password. reference of the installation step: https://www.percona.com/blog/wp-content/uploads/2018/05/Installing-MySQL-8.0-on-Ubuntu-2.png
更新我为解决此问题所做的另一个修复:通过全新安装的 MySQL,我选择了 NO 来加密设置中的密码(使用传统加密,而不是 SHA 加密),并且它开始与 Laravel 一起使用,没有任何问题 - 只需使用长而强的密码。安装步骤参考:https: //www.percona.com/blog/wp-content/uploads/2018/05/Installing-MySQL-8.0-on-Ubuntu-2.png
回答by danblack
Since PHP doesn't understand caching_sha2_password
, set the user back to mysql_native_password:
由于 PHP 不理解caching_sha2_password
,将用户设置回 mysql_native_password:
ALTER USER 'forge'@'localhost'
IDENTIFIED WITH mysql_native_password BY 'new_password'
回答by Vinod Raut
When running a PHP version before 7.1.16, or PHP 7.2 before 7.2.4, set MySQL 8 Server's default password plugin to mysql_native_password or else you will see errors similar to The server requested authentication method unknown to the client [caching_sha2_password] even when caching_sha2_password is not used.
当运行 7.1.16 之前的 PHP 版本或 7.2.4 之前的 PHP 7.2 时,将 MySQL 8 Server 的默认密码插件设置为 mysql_native_password 否则您将看到类似于 The server requests authentication method unknown to the client [caching_sha2_password] 的错误,即使在 caching_sha2_password 时未使用。
https://www.php.net/manual/en/mysqli.requirements.php
https://www.php.net/manual/en/mysqli.requirements.php
It would also help:
它还有助于:
https://php.watch/articles/PHP-7.4-MySQL-8-server-gone-away-fix
https://php.watch/articles/PHP-7.4-MySQL-8-server-gone-away-fix