MySQL 找回 root 密码的方法

方法一(通用):

1.在MySQL配置文件中的[mysqld]字段加入skip-grant-tables

2.重启MySQL服务,进入CLI模式

mysql -uroot

3.在MySQL命令行修改root用户密码

use mysql;

-- for version < 5.7
update user set Password=password('newpassword') where user='root';

-- for version = 5.7
update user set authentication_string=password('newpassword') where user='root';

-- for version >= 8 *只能设置为空密码*
update user set authentication_string='' where user = 'root';

flush privileges;

4.删除MySQL配置文件中的skip-grant-tables,重启MySQL

MySQL8需要使用空密码登录CLI后添加密码

ALTER USER 'root'@'localhost' IDENTIFIED BY 'root密码';

方法二(Debian系统):

类Debian系统可直接使用/etc/mysql/debian.cnf[client]节提供的密码登录

# 查看系统保留密码
cat /etc/mysql/debian.cnf

# 使用系统用户登录
mysql -udebian-sys-maint -p

扩展阅读 -- MySQL/MariaDB 用户操作相关语句:

-- 查看用户
select user,host from mysql.user;
-- 创建用户
create user 'tester'@'%' identified by 'password';
-- 修改密码
alter user 'tester'@'%' identified by 'new12345';
-- 修改用户
rename user 'root'@'localhost' to 'root'@'%';
-- 管理权限
grant all privileges on `testdb`.* to 'tester'@'%' with grant option;
remove all privileges on `testdb`.* from 'tester'@'%';
-- 刷新授权
flush privileges;
文章作者: 若海; 原文链接: https://www.rehiy.com/post/42/; 转载需声明来自技术写真 - 若海

添加新评论