1.首先关闭MySQL服务
C:\Users\Jacob>net stop mysql57
2.找到MySQL的配置文件my.ini,win7的话在C:\ProgramData\MySQL\MySQL Server 5.7目录下,为隐藏目录。打开my.ini,在[mysqld]下面加上 skip-grant-tables
[mysqld]
skip-grant-tables
3.保存修改后的my.ini,然后启动MySQL服务,直接打mysql并回车,然后就可以直接登录数据库了。
C:\Users\Jacob>net start mysql57
C:\Users\Jacob>mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.16-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
4.登录数据库后,修改密码。在5.7版本中,user表中已经不存在password字段,取而代之的是authentication_string。所以修改密码如下:
mysql> UPDATE user SET authentication_string=PASSWORD('root') WHERE user = 'root';
mysql> FLUSH PRIVILEGES;
6.退出MySQL
mysql> QUIT;