一、先关闭 mysql 服务,打开 mysql 的配置文件
ubuntu下是 /etc/mysql/mysql.conf.d/mysqld.cnf
window下是mysql目录下的 my.ini
二、在 [mysqld] 块下加上跳过验证的配置
skip-grant-tables
启动 mysql 服务后,直接使用 root 登陆,不用密码
三、修改root密码
alter user root@localhost identified with maysql_native_password by '123456';
如果提示:
The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
那就先执行:
flush privileges;
之后再执行修改密码的操作就不会出错了
四、将配置文件中跳过认证的配置项去掉,用密码登陆
关闭 mysql 服务,然后删掉刚刚在配置文件加上的 skip-grant-tables,再重启 mysql 服务,使用新密码登陆root