连接 MySQL 8.0 时,加密方式不兼容的解决方法
问题
laravel项目使用MySQL 8.0数据库(放在docker里面),运行php artisan migrate后报错:
Illuminate\Database\QueryException : SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client (SQL: select * from information_schema.tables where table_schema = weibo and table_name = migrations and table_type = 'BASE TABLE')
运行php artisan migrate -v查看更详细信息如下:
Exception trace:
1 PDOException::("PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]")
/www/weibo/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
.
.
.
原因
Google了一番,原因是MySQL 8.0使用了新的密码加密方式:caching_sha2_password,而许多客户端还不支持这种方式,比如php的PDO扩展。
解决
这里只说明在Docker环境下的解决方法,其他环境大概类似。
-
修改
docker-compose.yml的mysql服务部分,添加一行:command: --default-authentication-plugin=mysql_native_password my.cnf配置文件中[mysqld]下添加一行:default-authentication-plugin=mysql_native_password- 重新构建服务,依次执行:
docker-compose down,docker-compose up -d - 运行
docker container exec -it <container_name or id> /bin/bash进入mysql所在的容器。登录root账号:mysql -u root -p <password>,登入mysql后依次运行:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password'; FLUSH PRIVILEGES;最后,在php-fpm所在容器运行迁移数据库命令,迁移成功!
参考
本作品采用《CC 协议》,转载必须注明作者和本文链接
关于 LearnKu
推荐文章: