今天在连接mysql的时候,发现老是提示连接到localhost失败,找了原因,是前两天密码被非法修改了。现在不知道root登陆密码,又想连接到数据库,网站找了下方法,结合自己的实践,总结如下:
1、在my.init文件中的[mysqld]下面加上skip-grant-tables,故名思议,就是跳过授权验证表,直接操作数据库
2、重新启动mysql (net start/stop mysql)
3、在MySQL Command Line Client用root以空密码登陆。 或者在命令行中敲命令"mysql"(path设置)
4、修改密码
mysql> use mysql;
Database changed
mysql> UPDATE user SET Password = password ('root') WHERE User = 'root';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
5、查看是否修改成功
mysql> select password from user where user='root';
+-------------------------------------------+
| password |
+-------------------------------------------+
| *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-------------------------------------------+
2 rows in set (0.00 sec)
6、刷新权限,mysql新设置用户或更改密码后需用flush privileges刷新MySQL的系统权限相关表,否则会出现拒绝访问,还有一种方法,就是重新启动mysql服务器,来使新设置生效。并退出
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
7、在my.int中,把skip-grant-tables删掉,重启mysql
8、再次登陆数据库,成功
C:\Windows\system32>mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.1.57-community MySQL Community Server (GPL)