使用Laravel构建项目,执行迁移数据库命令[php artisan migrate]报 如下错
[PDOException]
SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
[PDOException]
PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]
猜想,应该是mysql8的验证机制改变了,mysql8使用caching_sha2_password的身份验证机制,以往的验证机制则是mysql_native_password,执行语句修改root用的验证机制,执行如下语句修改
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES; #刷新权限
查看是否修改成功,
use mysql;
select Host,User,plugin from user;
localhost | root | mysql_native_password
上述结果,表示修改成功
再次执行php artisan migrate依旧报错。
百度找大神经验,发现一篇博客上面说要改mysql的配置
解决方法:
执行如下命令新建my.cnf文件(mysql8没有该配置文件)
sudo vim /etc/my.cnf
在该文件中添加如下内容
[mysqld]
default_authentication_plugin=mysql_native_password
保存,重启mysql数据库,再次执行不报错了