MySQL系列-修改root密码

linux中mysql忘记root密码如何登陆

1.关闭mysql服务

systemctl stop mysqld

netstat -tunlp|grep mysqld

2.进入配置文件添加一条命令

[mysqld]

skip-grant-tables    #以安全模式启动

3.重新启动服务

systemctl start mysqld

4.无密码模式登陆

mysql -uroot

5. 5.7版本以后password字段没有,更改为authentication_string

show databases;
use mysql;
show tables;
desc user;
update mysql.user set authentication_string=password(‘xxx‘) where user=‘root‘ ;
flush privileges;

6.修改完毕后将配置文件中添加的语句注释掉,并重启服务

vim /etc/my.cnf#skip-grant-tables
systemctl restart mysqld

7.新密码重新登陆后出现问题

show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
#意思是必须使用alter user命令重新设置密码

mysql> alter user  identified by ‘XXXXXX‘;

flush privileges;

 

相关推荐