方法一
1、关闭 MySQL 服务
2、修改 MySQL 配置文件 my.cnf 在 [mysqld] 下添加 skip-grant-tables
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#跳过检测
skip-grant-tables
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
3、启动MySQL登陆数据库
4、更改 mysql.user 密码字段
update mysql.user set password=password('123') where user='root' and host='ip';
flush privileges;
5、删除 [mysqld] 下 skip-grant-tables 重启MySQL
方法二
1、启动 MySQL 时加上 mysqld_safe --skip-grant-tables &
2、重复第方法一第四步即可