centos系统mysql忘记密码

友情连接

sentos安装MySQL:centos7安装mysql5.7_shuair的博客-CSDN博客_centos7 安装mysql5.7

sentos系统安装的mysql忘记了密码,重置密码的步骤

  1. 设置为无密码登录(取消登录验证)
  2. 重置密码
  3. 取消无密码登录

设置为无密码登录(取消登录验证)

修改mysql的配置文件,默认位置:/etc/my.cnf

vim /etc/my.cnf

修改[mysqld]节点,添加skip-grant-tables,表示 跳过权限验证

[mysqld]
# 其它配置...
# 其它配置...
skip-grant-tables

保存,重启mysql

# systemctl restart mysqld
systemctl stop mysqld
systemctl start mysqld

重启mysql后可能会报错

Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

这种情况多数是配置文件中仍然存在权限相关的配置,暂时先注释掉,比如下面列举的配置

#validate_password_length = 4
#validate_password_policy = 0

此时即可无密码登录了(也无需用户名)

mysql

重置密码

使用mysql数据库

use mysql

修改密码

低版本

update user set Password = password('你的新密码') where user = 'root';

高版本(mysql57亲测可用)

update user set authentication_string=password('new password') where user = 'root';

取消无密码登录

将第1步注释掉的配置还原回来,重启服务。此时就已经修改好密码了。

参考

  1. https://www.php.cn/mysql-tutorials-484971.html
  2. https://blog.csdn.net/weixin_47018630/article/details/122822498


版权声明:本文为shuair原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。