本例在CentOS 7下操作。
- 首先修改
/etc/my.cnf文件,在[mysqld]下添加skip-grant-tables。[mysqld] skip-grant-tables - 随后重启
mysqld服务systemctl restart mysqld - 使用
root用户无密码登录mysql -u root - 切换到
mysql库,修改密码并设置生效use mysql; update user set authentication_string=PASSWORD('newpass') where User='root'; flush privileges; exit - 修改
/etc/my.cnf文件,删除或注释在[mysqld]下添加的skip-grant-tables。 - 再次重启
mysqld服务systemctl restart mysqld - 使用
root用户有密码登录,测试是否新密码生效mysql -u root -p