解决 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

一、背景

我的数据库重装后,出现 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 错误,网上一大堆没用的解决方法,直到找到该篇 解决ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES),特地分享出来,帮助遇到相同问题的筒子。

二、解决方案

  • 首先,打开计算机管理,停止 MySQL 服务
  • 管理员运行 CMD 并执行 mysqld --skip-grant-tables

    此时,会出现阻塞状态,关闭当前 CMD,再次以管理员身份打开新的 CMD

  • 修改密码:
    1. 首先执行:
      update mysql.user set authentication_string=password('root') where user = 'root';
      flush privileges;
      
    2. 接着执行: SELECT * from mysql.user\G;
    3. 找到 root 用户的 authentication_string 这项,并把它的值记下来
    4. 然后执行:
      # xxx 为你记录下来的 authentication_string 的值
      update mysql.user set password = 'xxx' where user = 'root';
      flush privileges;
      
    5. 执行 quit; 退出,然后 mysql -u root -p 登录就 ok 了

你可能感兴趣的:(杂记)