MySQL 8.0忘记密码怎么办


MySQL是极为流行的一款数据库,在更新到8.0版本之后,重置、修改密码的方法有所改变(事实上是在5.7.6之后user表中的password字段以及password()方法被废弃了,所以需要调整下旧的密码重置步骤),在此记录一下供大家查阅。


欢迎大家关注我的个人博客【数洞】 【备用站】

操作流程

1. 定位配置文件

理论上我们的mysql的目录已经在环境变量里了,所以我们直接输入以下命令:

# dain @ localhost in ~ [17:22:22]
$ mysqld --verbose --help |grep -A 1 'Default options'
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf

可以看到,我们的mysql会按顺序查阅四个位置的配置文件,如果这些文件不存在的话,我们可以创建一个新的,例如,我们在/etc/下创建/etc/my.cnf文件:

nvim /etc/my.cnf    # 我使用的是基于vim的nvim,此处可以使用自己喜欢的编辑器

2. 修改配置

在我们的配置文件中添加以下内容(若没有则创建新文件)并保存退出:

[mysqld]
skip-grant-tables

3. 重启MySQL

在Mac下:

# dain @ localhost in ~ [17:38:46] C:127
$ sudo /usr/local/mysql/support-files/mysql.server restart

在Mac下,我们也可以在系统偏好设置里通过可视化方式重启。

在Debian/Ubuntu下:

# dain @ localhost in ~ [17:38:46] C:127
$ mysql restart

在CentOS/RetHat/Fedora下:

# dain @ localhost in ~ [17:38:46] C:127
$ mysqld restart

4. 无密码登录MySQL

登录Mysql,密码留空:

# dain @ localhost in ~ [17:54:34]
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.12 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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>

5. 删除旧密码

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> update user set authentication_string = '' where user = 'root';
Query OK, 1 row affected (0.08 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> quit
Bye

6. 去除免密码登录

修改我们的配置文件,此例中为/etc/my.cnf文件。我们将skip-grant-tables一行去除,并使用步骤3重启MySQL服务。

7. 修改密码

首先,使用步骤4登录MySQL,由于已经删除了密码,所以密码仍然留空即可:

# dain @ localhost in ~ [18:04:21]
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.12 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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>

然后,我们用以下命令修改密码,需要注意的是,密码过于简单则会报错:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Data-Insights123';
Query OK, 0 rows affected (0.02 sec)

这样,我们的MySQL密码就完成了重置,要记得保存好密码哦。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老Q量化投研

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值