1. 修改mysqld.cnf配置文件,跳过验证
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
#添加这行代码至文件:
skip-grant-tables
29 [mysqld]
30 skip-grant-tables
31 pid-file = /var/run/mysqld/mysqld.pid
32 socket = /var/run/mysqld/mysqld.sock
33 datadir = /var/lib/mysql
34 log-error = /var/log/mysql/error.log
2. 重启mysqld,登录修改密码
sudo service mysql restart
#连接mysql
mysql -u root
#更新mysql root用户密码为null
USE mysql;
UPDATE user SET plugin=‘mysql_native_password‘, authentication_string=null WHERE user=‘root‘;
FLUSH privileges;
3. 还原mysqld.cnf,重启mysqld,再使用ALTER修改密码
# 删除之前增加的`skip-grant-tables`选项
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
#重启mysqld,现在可以在没有skip-grant-tables选项的情况下使用空密码登录
sudo service mysql restart
#
mysql -u root
# 真正地修改密码
ALTER USER ‘root‘@‘localhost‘ IDENTIFIED BY ‘password‘;
MySQL 重置密码