MySQL忘记密码解决方法

系统:Linux(CentOS7.2)

mysql> status
--------------
mysql  Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using  EditLine wrapper

Connection id:          6
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.7.22 MySQL Community Server (GPL)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                 1 min 0 sec

修改MySQL配置文件,vim /etc/my.cnf

[mysqld]

skip-grant-tables

在[mysqld]下随便找一行填写,然后保存退出。

重启MySQL:

service mysqld restart

进入MySQL,这次不需要用户名密码:

mysql

use mysql;

// 123456为新密码
update user set authentication_string = password("123456") where user = "root";

成功之后,删除之前在配置文件添加的那行代码,保存后重启MySQL即可。

你可能感兴趣的:(【数据库】MySQL)