Centos7下忘记mysql5.7密码后,重置root密码的方式

Centos7下忘记mysql5.7密码后,重置root密码的方式

Centos7下,忘记了MySQL的密码,不要急着重装。试试看下面的方法,完美解决。

编辑配置文件

[root@localhost ~]# vim /etc/my.cnf

[mysqld] 配置节下新增 skip-grant-tables 配置项

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

skip-grant-tables

# Disabling symbolic-links is recommended to prevent assorted security risks

保存配置文件后,重启mysql服务。

[root@localhost ~]# systemctl restart mysqld.service

使用mysql命令登录mysql数据库。

[root@localhost ~]# mysql

使用下面语句修改root密码,注意和之前版本的mysql语句不太一样。

mysql>update mysql.user set authentication_string=password('新密码') where user='root' ;
mysql>flush privileges ;
mysql>quit

再次编辑mysql配置文件,将之前添加的 skip-grant-tables 去掉。

重启mysql服务后,使用新密码进行登录

[root@localhost ~]# systemctl restart mysqld.service

最后,附上Centos7安装MYSQL的教程


参考原文:https://www.cnblogs.com/yanziwen/p/9215264.html