MySQL忘记密码解决方案
可以空密码登录的情况
mysql -u root -p
修改随机密码为root
mysql>
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
flush privileges;
无法登陆的情况
可能需要安装 vim
microdnf install -y vim
1、先修改配置文件 /etc/my.cnf
令MySQL跳过登录时的权限检验,在 [mysqld]
下加入一行:
skip-grant-tables
2、重启MySQL
service mysqld restart
3、免密码登录MySQL。
mysql
4、修改root密码
mysql8之前
mysql> use mysql;
mysql> UPDATE user SET authentication_string = password('新密码') WHERE host = 'localhost' AND user = 'root';
mysql> select host,user, authentication_string, password_expired from user;
mysql> update user set password_expired='N' where password_expired='Y' //密码不过期
mysql> update user set host='%' where user='root' and host='localhost'; //远程可访问
mysql> flush privileges; //刷新权限
mysql> exit;//退出
mysql8之后
sudo mysqld_safe --skip-grant-tables
mysql -u root
UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;
exit;
接下来
mysql -u root
SET PASSWORD FOR root='123456';
5、修改配置文件 /etc/my.cnf
删除此前新增那一行 skip-grant-tables
,并重启MySQL(这一步非常重要,不执行可能导致严重的安全问题)
service mysqld restart //重启 Mysql
参考:
https://help.aliyun.com/document_detail/42520.html
错误排查
输入下面的命令然后查看日志
mysqld --console
添加链接授权
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;