先停止MySQL服务
service mysqld stop
编辑my.cnf配置文件
vi /etc/my.cnf 添加配置
[mysqld]
skip-grant-tables
重启mysql服务
service mysqld restart
登录数据库修改密码
mysql -uroot -p #直接回车即可
mysql> use mysql;
mysql> update user set authentication_string = password(‘密码’) where user = ‘root’;
mysql> flush privileges;
mysql> grant all privileges on . to ‘root’@’%’ identified by ‘密码’;
//如果不执行这句,登录的时候会报错
ROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
mysql> flush privileges;
注意:5.7的mysql密码必须是必须含有数字,小写或大写字母,特殊字符。如果想设置的简单点,执行以下sql
修改validate_password_policy参数的值
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
//validate_password_length(密码长度)参数默认为8,我们修改为1
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
完成之后再次执行修改密码语句即可成功
mysql> alter user ‘root’@‘localhost’ identified by ‘123456’;
Query OK, 0 rows affected (0.00 sec)
将my.cnf配置文件中跳过密码啊验证注释,再重启服务
service mysqld restart
使用账户密码登录mysql