忘记MySQL密码如何进行修改

本文详细介绍了一种在忘记MySQL数据库密码时的解决方案,通过修改配置文件、重启服务、直接登录数据库、更改密码并重新配置服务等步骤,帮助读者安全有效地重置MySQL密码。

在日常使用MySQL数据库时如果出现忘记密码无法登录的情况,总不能选择重新去安装MySQL的方式设置密码吧,
在这里插入图片描述
重置密码的步骤

第一步更改MySQL的配置文件,写入一条配置

编辑/etc/my.cnf

[mysqld]
skip-grant-tables
第二步重启mysqld
systemctl restart mysqld
第三步直接登录MySQL数据库

使用 msyql -uroot 直接登录

[root@server1 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30-log Source distribution

Copyright (c) 2000, 2020, 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后,使用msql库,再进行密码更改
使用库use mysql
更新密码:update mysql.user set authentication_string=password('root_password') where user='root'; root_password就是你自定义的密码

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 mysql.user set authentication_string=password('root_password') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

第五步设置完密码退出,在配置文件删除之前写入的参数,重启mysqld
vim /etc/my.cnf

在这里插入图片描述

systemctl restart mysqld
第六步使用新密码登录
mysql -uroot -p******

在这里插入图片描述

忘记 MySQL 密码修改密码的方法如下: #### 通用环境 以管理员身份打开命令行,在命令行中进入 MySQL 的 bin 目录所在文件夹,即输入 `cd [路径]`,路径查找可在命令行输入相应命令。跳过 MySQL 用户验证登录数据库,命令行输入 `mysqld --skip-grant-tables`,注意输入此命令后,当前的命令行无法操作,需再打开一个新的命令行,且在输入此命令之前先在任务管理器中结束 mysqld.exe 进程,确保 mysql 服务器端已结束运行再进行此操作。在新开的命令行中登录 MySQL,先切换至 MySQL 的 bin 目录下,然后直接输入 `mysql` 回车即可登录。查看 MySQL 中所有的数据库,输入 `show databases;` 回车,其中 mysql 存放了用户名密码等信息。选择 mysql 数据库,输入 `use mysql;` 回车,显示 mysql 中的所有表,输入 `show tables;` 回车,user 表中存放用户名密码等信息。查看本人的用户名密码信息,命令行输入 `select user,host,authentication_string from user;`,若个人密码简单,可复制该密文找解密网站查看原密码密码复杂则无法解密查看。修改密码,命令行输入 `update user set authentication_string=password('123456') where user='root';` 即可把密码修改为 123456,可根据个人需求修改为任何密码,刷新权限表 `flush privilege;`,重启数据库即可用新密码重新登录[^4]。 #### Ubuntu 环境 - **忘记普通账户密码**:直接登录 root 账户修改普通账户密码,使用命令 `set password for '用户名'@'主机名'=password('新的密码');` [^3]。 - **忘记 root 账户密码**:在 mysql 的配置文件中配置免密登录,Ubuntu 环境下 MySQL 默认的配置文件位置在 `/etc/mysql/mysql.conf.d/mysqld.cnf` 中,使用 `sudo` 借用 root 权限打开该配置文件,考虑到后面还要登录 MySQL 的 root 用户,可直接切成 root 用户,输入 `sudo Password: #输入你自己 root 账户的密码`,然后使用 `vim /etc/mysql/mysql.conf.d/mysqld.cnf` 打开文件,在里面增加 `skip-grant-tables` 以设置免密登录。修改配置文件后,重启 MySQL 服务器 `service mysql restart` 使其生效。重启服务器后,输入 `mysql -uroot -p` 回车,直接回车即可进入 MySQL。重置 root 密码,使用命令 `update user set authentication_string='新密码' where user='root';`,之后注释掉/删除掉 `skip-grant-tables`,再重启服务器 `service mysql restart` 就能使用新密码进行登录 [^3]。 #### 修改 user 表方式 修改 user 表来更改密码,使用命令 `update mysql.user set authentication_string=PASSWORD("123456") where user="root" and host="localhost"` [^5]。 ```sql -- 通用环境修改密码示例 update user set authentication_string=password('newpassword') where user='root'; flush privilege; -- Ubuntu 环境重置 root 密码示例 update user set authentication_string='newpassword' where user='root'; -- 修改 user 表示例 update mysql.user set authentication_string=PASSWORD("newpassword") where user="root" and host="localhost"; ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Howei__

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

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

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

打赏作者

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

抵扣说明:

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

余额充值