Linux 环境下 MySQL 5.7 root密码忘记

您所在的位置:网站首页 腾讯云root登录不进去 Linux 环境下 MySQL 5.7 root密码忘记

Linux 环境下 MySQL 5.7 root密码忘记

2023-11-22 18:25| 来源: 网络整理| 查看: 265

1.介绍

MySQL安装完毕之后,一段时间没有进行过操作。今天发现忘记了root密码。

输入密码后提示:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

密码错误。登录不了。

通过在网络上搜索,找到了可以跨过密码的方案。这里进行一个复盘操作,同时记录一下我的操作过程。

2. 解决

我们忘记root密码之后,就需要通过免密登录,来实现登录。之后再进行root密码修改就可以了。

2.1 开启免密登录

通过Linux root管理角色,编辑mysql的配置文件my.conf 文件。示例效果如下:

[root@iZuf ~]# vim /etc/my.cnf

在打开的vim编辑界面中,按I进入编辑模式,然后在[mysqld]目录下面添加:skip-grant-tables。示例效果如下:

# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld] # ... # sort_buffer_size = 2M # read_rnd_buffer_size = 2M skip-grant-tables datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock

添加完毕后。进行保存。

操作步骤:按Esc键退出编辑模式,输入::wq 进行保存退出操作。 如果你输入后没有反应,请检查你的输入法是否为英文。中文模式下输入无效。

保存完配置后,执行:重启mysqld服务操作:

[root@iZuf ~]# systemctl restart mysqld

PS:也可以使用:service mysqld restart命令进行重启。

重启完毕后,就可以执行登录操作l ,输入:mysql -uroot -p 回车后,还是会弹出 Enter password提示输入密码,但是我们可不用输入密码,直接按回车就可以登录进去了。示例如下:

[root@iZuf ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.40 MySQL Community Server (GPL) Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>2.2 修改root密码

当我们能够免密登录之后,就可以执行root密码修改操作了

到这里,我们不能直接使用以下的命令进行账户修改。

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '在这里输入我们的密码';

会输出错误提示:ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement 也就是说我们当前属于--skip-grant-tables 登录模式,不能进行这种修改操作。

那么我们该如何修改呢?直接修改user表中的数据就可以了。

首先,通过指令use mysql;选择mysql数据库:

mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql>

然后,修改root用户密码为 httpszinyancom&1024,大家也可以设置自己的默认密码:

mysql> update user set authentication_string=password('httpszinyancom&1024') where user='root'; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql>

配置修改成功后,会输出 Query OK.

最后,通过flush privileges 刷新修改:

mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql>

这个步骤的意义是将当前user和privilige表中的用户信息/权限设置从mysql库(MySQL数据库的内置库)中提取到内存里。

本质上,我们修改完毕了。等会也是要重启MySQL的。只是通过这个步骤可以刷新一下,避免出现配置没有同步的意外。

全部配置完毕后,输入:exit;退出mysql:

mysql> exit; Bye [root@iZuf ~]# 2.3 重新登录

我们要重新登录,首先将免密登录配置 skip-grant-tables 从my.conf文件中给删除掉。

然后再执行重启服务操作:[root@iZuf ~]# systemctl restart mysqld:

[root@iZuf ~]# vim /etc/my.cnf [root@iZuf ~]# systemctl restart mysqld Redirecting to /bin/systemctl restart mysqld.service [root@iZuf ~]#

然后,我们再次通过[root@iZuf ~]# mysql -uroot -p 进行登录操作:

[root@iZuf ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.40 MySQL Community Server (GPL) Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>

在这个时候,Enter password就需要输入密码才能登录了。否则会是ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)错误提示。无法进行登录。

到这一步我们的密码就可以说是找回了。

3. 其他错误

我们如果登录成功后,提示我们需要修改密码:ERROR 1820 (HY000): You must reset your password using ALTER USER statement;

那可能是因为我们配置的密码,不符合MySQL的安全验证规范。MySQL默认对密码的要求为:最小8位,数字,字母和其他符号至少有1个。

所以我们可以通过

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '在这里输入我们的密码'; 更新我们的root账户的密码。

到这里,有关密码的问题,就结束了。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3