前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux 环境下 MySQL 5.7 root密码忘记-解决方法

Linux 环境下 MySQL 5.7 root密码忘记-解决方法

作者头像
zinyan.com
发布2022-12-07 18:27:56
1.2K0
发布2022-12-07 18:27:56
举报
文章被收录于专栏:zinyanzinyan

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 文件。示例效果如下:

代码语言:javascript
复制
[root@iZuf ~]# vim /etc/my.cnf

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

代码语言:javascript
复制
# 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服务操作:

代码语言:javascript
复制
[root@iZuf ~]# systemctl restart mysqld

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

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

代码语言:javascript
复制
[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密码修改操作了

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

代码语言:javascript
复制
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数据库:

代码语言:javascript
复制
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,大家也可以设置自己的默认密码:

代码语言:javascript
复制
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 刷新修改:

代码语言:javascript
复制
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

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

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

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

代码语言:javascript
复制
mysql> exit;
Bye
[root@iZuf ~]# 

2.3 重新登录

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

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

代码语言:javascript
复制
[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 进行登录操作:

代码语言:javascript
复制
[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账户的密码。

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-11-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 zinyan 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.介绍
  • 2. 解决
    • 2.1 开启免密登录
      • 2.2 修改root密码
        • 2.3 重新登录
        • 3. 其他错误
        相关产品与服务
        云数据库 SQL Server
        腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档