MySQL 数据库重置密码

1.Linux 中 MySQL 数据库重置密码

停止服务:systemctl stop mysql

打开 my.cnf(没有则创建一个):vim /etc/my.cnf

在 [mysqld] 下添加:skip-grant-tables;

[mysqld]
skip-grant-tables

启动数据库:systemctl start mysqld

重置密码:

#无需密码,直接登录
mysql -uroot -p

#设置密码
mysql>alter user 'root'@'localhost' identified by 'new_password';

#刷新权限
mysql>flush privileges;

#修改后,退出
mysql>exit

再次打开 my.cnf,移除刚添加的 skip-grant-tables,保存重启 mysql 服务,使用新密码登录

 

如果设置密码时遇见报错:ERROR 1290 (HY000): The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement

 执行:mysql>flush privileges;

2.Windows 中 MySQL 数据库重置密码

管理员运行cmd,进入mysql安装目录下的bin目录
net stop mysql
mysqld --skip-grant-tables
打开第二个cmd窗口
空密码登录
将密码置为空(操作和Linux一致)
posted @ 2024-04-05 23:26  落落的学习  阅读(35)  评论(0编辑  收藏  举报