MySQL 忘记root密码

550 阅读1分钟

突然想不起来,MySQL的root密码,现在提供一种解决办法

修改配置文件my.cnf

vim /etc/my.cnf
在mysqld下面增加skip-grant-tables

[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
datadir=/data/mysql/data
socket=/data/mysql/tmp/mysql.sock
skip-grant-tables

[mysqld_safe]
log-error=/data/mysql/logs/mysqld.log
pid-file=/data/mysql/pids/mysqld.pid

[client]
default-character-set=utf8mb4

重启MySQL服务

systemctl restart mysqld

修改root密码

1、mysql -u root 以root的方式登录数据库,这里跳过了权限设置,所以是不需要密码登录的 2、修改root密码

mysql> update user set authentication_string=password('123456') where user='root';
mysql> flush privileges;

重置配置并重启

#去掉my.cnf skip-grant-tables
#重启MySQL服务
systemctl restart mysqld