环境:Centos 7
1.打开配置文件
sudo vi /etc/my.cnf
2.在[mysqld]中添加skip-grant-tables,如:
[mysqld]
skip-grant-tables
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
3.重启mysql
sudo service mysqld restart
4.无用户密码登录
mysql -uroot -p (直接点击回车,密码为空)
5.选择数据库
use mysql
6.修改root密码:
update user set authentication_string=password('123456') where user='root';
7.执行:
flush privileges
8.退出并删除配置文件中的skip-grant-tables
9.重启mysql
sudo service mysqld restart
当出现错误:
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
或
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
解决办法,将123456修改为一个复杂密码(带数字、字母、特殊符), 如:
ALTER USER USER() IDENTIFIED BY '12345678zy_BJ';
再将密码修改为你需要设置的密码(非复杂密码)