mysql 密码重置
时间: 2023-11-19 09:57:40 浏览: 185
以下是两种重置 MySQL 密码的方法:
1. 使用 mysqladmin 命令重置密码
```bash
# 停止 MySQL 服务
sudo service mysql stop
# 使用 skip-grant-tables 参数启动 MySQL 服务
sudo mysqld_safe --skip-grant-tables &
# 登录 MySQL 服务
mysql -u root
# 刷新权限
FLUSH PRIVILEGES;
# 设置新密码,这里的 'new_password' 是要设置的新密码,可以根据需要进行修改。此命令将修改 root 用户在本地 MySQL 服务上的密码。
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
# 退出 MySQL
exit
# 重启 MySQL 服务
sudo service mysql restart
```
2. 使用 systemctl 命令重置密码
```bash
# 停止 MySQL 服务
sudo systemctl stop mysql
# 创建一个文件,例如 /var/run/mysqld/mysqld.sock
sudo touch /var/run/mysqld/mysqld.sock
# 修改文件的权限
sudo chown mysql:mysql /var/run/mysqld/mysqld.sock
# 使用 --skip-grant-tables 参数启动 MySQL 服务
sudo mysqld_safe --skip-grant-tables &
# 登录 MySQL 服务
mysql -u root
# 刷新权限
FLUSH PRIVILEGES;
# 设置新密码,这里的 'new_password' 是要设置的新密码,可以根据需要进行修改。此命令将修改 root 用户在本地 MySQL 服务上的密码。
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
# 退出 MySQL
exit
# 删除创建的文件
sudo rm /var/run/mysqld/mysqld.sock
# 重启 MySQL 服务
sudo systemctl start mysql
```
阅读全文
相关推荐

















