MySQL 8.0+ 的用户"root"@"localhost"(使用密码:否)的访问被拒绝



最初mysql -u root运行良好。

然后我尝试使用以下命令重置root用户密码

UPDATE set authentication_string=PASSWORD("password") where User='root';

但当我尝试mysql -u root -ppassword时,它正在抛出

access denied for user 'root'@'localhost' (using password: NO)

因此,要修复上述解决方案,请执行以下步骤:

  1. 首先使用which mysql搜索系统中mysql脚本的位置

/usr/local/bin/mysql

  1. 运行cd /usr/local/bin
  2. 然后查找mysql.server文件并执行mysql.server stop

输出为:
关闭MySQL
。成功!

  1. 运行killall mysqld mysqld_safe
  2. 运行mysqld_safe --skip-grant-tables &
  3. 然后尝试mysql -u root,它会起作用

所以下一步是将根密码更新回<no-password>

  1. 运行use mysql;
  2. 运行Update user set authentication_string='' where User='root';

注意:要更新mysql8.0+密码,请始终使用

ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';

最新更新