MYSQL忘记密码-重置密码
问题 -忘记密码:
登录Mysql,密码错误的时候出现错误ERROR 1045 (28000)
mysql -u root -p
Enter Password > 'password'
错误:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
密码忘记了,这样来重置。
解决方法 - 重置密码:
- 修改my.cnf配置文件(默认位置:/etc/my.cnf):
- 在[mysqld]下添加skip-grant-tables
- 重启mysql服务
- systemctl restart mysql
- 登录mysql
mysql -u root -p
不用输入密码,直接回车即可登录成功
- 修改root的密码
MySQL 5.7 的版本,user表中没有password字段,通过下面的方法来重置root密码
use mysql
update user set authentication_string = password(“root”) where user = “root”;
Mysql 5.7.9以后废弃了password字段和password()函数,authentication_string字段表示用户密码,只能是mysql加密后的41位字符串密码。通过下面方法来重置。
- 先检查root对应的host
mysql> select user,host from user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| root | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| test | % |
+---------------+-----------+
5 rows in set (0.00 sec)
- 修改root密码:
ALTER user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword';
MySql8.0开始修改密码又有了变化,user表中加了字段authentication_string,修改密码前要先检查authentication_string是否为空
- 如果authentication_string不为空
update user set authentication_string='' where user='root';--将字段置为空
ALTER user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'rootpwd';--修改密码为rootpwd
- 如果authentication_string为空,直接修改
ALTER user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'rootpwd';--修改密码为rootpwd
如果遇到一下错误
mysql> ALTER user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'rootpwd';
ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'%'
则先执行
flush privileges;
然后再执行
ALTER user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'rootpwd';
亲测修改成功