1、使用 --init-file 重置密码
# 创建文件并写入以下内容
cd /usr/local/mysql/mysql-files/
echo 'ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysql0102';' > mysql-init-password
# 停止mysql
/etc/init.d/mysql stop
# 启动mysql(注意:加上 --init-file)
/etc/init.d/mysql start --init-file=/usr/local/mysql/mysql-files/mysql-init-password
# 登陆mysql
# 使用旧密码、新密码登陆测试查看是否修改成功
2、使用 --skip-grant-tables 免密登陆
使用 --skip-grant-tables 启动mysql,不会加载授权表,这样就可以实现免密登陆,然后通过 flush privileges重新加载授权,这样就可以实现密码的修改
在使用此参数会自动启用 --skip-networking来禁止远程连接,所以我们修改完毕后记得正常重新启动
# 停止mysql
/etc/init.d/mysql stop
# 启动mysql
/etc/init.d/mysql start --skip-grant-tables
# 登陆mysql(密码直接回车即可)
mysql -u root -p
# 执行sql,刷新权限,如果不执行此指令,会报错`ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement`
flush privileges;
# 修改密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
# 然后重启mysql
/etc/init.d/mysql restart
# 使用新密码登陆测试
mysql -u root -p123456
3、重装
如题
- 备份
- 卸载
- 安装
参考: MySQL8.0-安装 & 卸载