以下经验经本人亲自试验过,确实好用:
1. 打开mysql的配置文件my.ini,默认路径是C:\ProgramData\MySQL\MySQLServer 5.7下。注意:如果找不到C:\ProgramData这个文件夹,可以在工具->文件夹选项下显示隐藏的文件夹。然后,在配置文件中添加上skip-grant-tables,可以添加到文件的末尾或者是这添加到[mysqld]的下面。这样做可以让你跳过root密码登录到数据库中。
2. 然后在命令行中重启MYSQL服务,windows环境中命令是: net stop MySQL
net start MySQL
如果出现不是有效的命令,注意环境变量的配置;或者在命令行中转到mysql的bin目录下:cd C:\ProgramFiles\MySQL\MySQL Server 5.7\bin就可以了
-
修改root密码
在命令行输入 mysql –uroot–p
出现让你输入密码,直接enter键就好了
这样你就进入数据库了,你可以show databases;查看目前有哪些数据库;
接下来,选择数据库, 命令行输入usemysql
现在我们执行如下语句把root密码更新为123456:
updatemysql.user set authentication_string=password('123456') where user='root' andHost = 'localhost';
quit 退出MySQL
4、还原配置文件并重启
再把上面配置文件中的的“skip-grant-tables”删掉,重启服务,这样就OK了,可以用新的密码登录了
如下指令:
mysql_safe --skip-grant-tables --skip-networking &
mysql -uroot -p //可以空密码进入。
update mysql.user set authentication_string=password('123qwe') where user='root';
flush privileges;
alter user 'root'@'localhost' identified by '123';
set password for 'root'@'localhost'=password('123')
quit;
重启mysqld。