mysql找回密码
mysql数据库忘记密码找回 linux系统
第一步 停止数据库
service mysql stop
如果出现Failed to restart mysql.service: Unit not found. 尝试使用 systemctl restart mysqld.service 命令
第二步 设置数据库免密登录
进入/etc文件夹,编辑my.cnf文件
添加
skip-grant-tables #关闭安全策略
skip-networking #断开外网链接
第三步 重新启动数据库
service mysql start
第四步 直接进入数据库
mysql -uroot -p
在Enter password: 环节直接点击enter即可进入数据库
第五步 在mysql数据库中找到自己的用户
进入mysql管理的库;
use mysql;
5.7以前版本
select Host,User,password from user where user = 'root';
5.7及以后版本
select Host,User,authentication_string from user;
示例如下:
Host | User | authentication_string |
+-----------+------------------+-------------------------------------------+
| % | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost | root | |
第六步 更改密码
5.7之前版本
update user set password=password('123456') where user='root';
flush privileges;
5.7--5.7.9
update user set authentication_string=password('123456') where user='root';
flush privileges;
8.0及以后版本
update user set authentication_string ="" where User = 'root' and Host='%';
这里需要首先将密码设置为空。
flush privileges;
alter user'root'@'%' IDENTIFIED BY 'MyNewPass@123';
mysql8.0以上密码策略限制必须要大小写加数字特殊符号
错误提示
Operation ALTER USER failed for 'root'@'localhost'
出现这个错误,是要更改所有权限的root用户对应的密码,
即修成为'root'@'localhost'改为‘root’@‘%’=
Operation ALTER USER failed
8.0以后的版本使用之前版本的update语句更新密码会出现这个问题,需要重新将密码置为空,然后刷新权限,再更新。
update user set authentication_string ="" where User = 'root' and Host='%';
flush privileges;
alter user'root'@'%' IDENTIFIED BY 'MyNewPass@123';
"Host '***.***.***.***' is not allowed to connect to this MySQL server"
查看用户
select Host,User,authentication_string from user;
将root的Host修改为%
第七步 修改配置文件重启
停止数据库
service mysql stop;
修改配置文件
删除或注释掉第二步添加到my.cnf中的内容
#skip-grant-tables #关闭安全策略
#skip-networking #断开外网链接
启动mysql数据
service mysql start;
使用修改之后的密码登录即可。
PS:在操作和整理的过程中,借鉴了许多大佬的文章,多谢.