Linux 忘记MySQL密码 怎么办
时间: 2023-11-28 09:05:44 浏览: 87
以下是5种Linux忘记MySQL密码找回方法:
1.使用--skip-grant-tables选项重置密码
```shell
# 停止MySQL服务
/etc/init.d/mysql stop
# 以跳过权限验证的方式启动MySQL服务
mysqld_safe --skip-grant-tables &
# 以root用户身份登录MySQL
mysql -u root
# 切换到mysql数据库
use mysql;
# 更新root用户的密码
update user set password=PASSWORD("new_password") where User='root';
# 刷新权限
flush privileges;
# 退出MySQL
quit;
# 重启MySQL服务
/etc/init.d/mysql restart
```
2.使用mysqladmin重置密码
```shell
# 停止MySQL服务
/etc/init.d/mysql stop
# 以跳过权限验证的方式启动MySQL服务
mysqld_safe --skip-grant-tables &
# 重置root用户的密码
mysqladmin -u root password "new_password"
# 重启MySQL服务
/etc/init.d/mysql restart
```
3.使用mysqld_safe启动MySQL服务并跳过权限验证
```shell
# 停止MySQL服务
/etc/init.d/mysql stop
# 以跳过权限验证的方式启动MySQL服务
mysqld_safe --skip-grant-tables &
# 以root用户身份登录MySQL
mysql -u root
# 切换到mysql数据库
use mysql;
# 更新root用户的密码
update user set password=PASSWORD("new_password") where User='root';
# 刷新权限
flush privileges;
# 退出MySQL
quit;
# 重启MySQL服务
/etc/init.d/mysql restart
```
4.使用init-file选项重置密码
```shell
# 创建一个文件,例如reset.sql,内容如下
UPDATE mysql.user SET authentication_string=PASSWORD('new_password') WHERE User='root';
FLUSH PRIVILEGES;
# 停止MySQL服务
/etc/init.d/mysql stop
# 以指定文件的方式启动MySQL服务
mysqld_safe --init-file=/path/to/reset.sql &
# 重启MySQL服务
/etc/init.d/mysql restart
```
5.使用skip-grant-tables选项启动MySQL服务并创建新用户
```shell
# 停止MySQL服务
/etc/init.d/mysql stop
# 以跳过权限验证的方式启动MySQL服务
mysqld_safe --skip-grant-tables &
# 以root用户身份登录MySQL
mysql -u root
# 切换到mysql数据库
use mysql;
# 创建新用户
CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'new_password';
# 授权新用户
GRANT ALL PRIVILEGES ON *.* TO 'new_user'@'localhost' WITH GRANT OPTION;
# 刷新权限
flush privileges;
# 退出MySQL
quit;
# 重启MySQL服务
/etc/init.d/mysql restart
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)