一、先关闭 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

本文详细介绍了在Ubuntu和Windows环境下如何关闭MySQL服务,编辑配置文件添加跳过验证选项,然后无密码登录并修改root用户的密码。过程中提到了可能遇到的问题及解决方法,包括执行`flush privileges`命令来避免错误。最后,指导读者移除配置文件中的认证跳过选项,重启服务并使用新密码登录。
2785





