Windows下mysql忘記root密碼的解決方法

 Mysql版本:5.1.55-community MySQL Community Server (GPL)

 

1、  首先檢查mysql服務是否啓動,若已啓動則先將其停止服務,可在開始菜單的運行,使用命令:

net stop mysql

或者在windows任務管理器中結束mysqld.exe進程,或者在控制面板,管理工具裏面的服務找到mysql,將其停止服務。

 

打開第一個cmd窗口,切換到mysql的bin目錄,運行命令:

mysqld --defaults-file="C:\Program Files\MySQL\MySQL Server 5.1\my.ini" --console --skip-grant-tables

註釋:

該命令通過跳過權限安全檢查,開啓mysql服務,這樣連接mysql時,可以不用輸入用戶密碼。

"C:\Program Files\MySQL\MySQL Server 5.1\my.ini"指配置文件my.ini,一般在mysql安裝目錄裏面。

如:

===============================================================================

C:\Program Files\MySQL\MySQL Server 5.1\bin>mysqld --defaults-file="C:\Program Files\MySQL\MySQL Server 5.1\my.ini" --console --skip-grant-tables

110301  9:20:07 [Warning] '--default-character-set' is deprecated and will be removed in a future release. Please use '--character-set-server' instead.

110301  9:20:07 [Note] Plugin 'FEDERATED' is disabled.

110301  9:20:07  InnoDB: Initializing buffer pool, size = 46.0M

110301  9:20:07  InnoDB: Completed initialization of buffer pool

110301  9:20:07  InnoDB: Started; log sequence number 0 44233

110301  9:20:07 [Note] mysqld: ready for connections.

Version: '5.1.55-community'  socket: ''  port: 3306  MySQL Community Server (GPL)

 

 

2、打開第二個cmd窗口,連接mysql:

輸入命令:

mysql -uroot -p

出現:

Enter password:

在這裏直接回車,不用輸入密碼。

然後就就會出現登錄成功的信息,

如:

===============================================================================

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.1.55-community MySQL Community Server (GPL)

 

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

This software comes with ABSOLUTELY NO WARRANTY. This is free software,

and you are welcome to modify and redistribute it under the GPL v2 license

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql>

 

===============================================================================

使用命令:

show databases;

顯示已有數據庫:

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| test               |

+--------------------+

3 rows in set (0.00 sec)

 

使用命令切換到mysql數據庫:

use mysql;

使用命令更改root密碼:

UPDATE user SET Password=PASSWORD('newpassword') where USER='root';

 

刷新權限:

FLUSH PRIVILEGES;

然後退出,重新登錄:

quit

重新登錄:

mysql -uroot -p

出現輸入密碼提示,輸入新的密碼即可登錄:

Enter password: ***********

顯示登錄信息:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 9

Server version: 5.1.55-community MySQL Community Server (GPL)

 

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

This software comes with ABSOLUTELY NO WARRANTY. This is free software,

and you are welcome to modify and redistribute it under the GPL v2 license

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql>

 

到這裏root密碼就已經修改成功了,用Ctrl+C將第一個cmd窗口結束,那樣就會停止mysql服務,如:

=============================================================================== C:\Program Files\MySQL\MySQL Server 5.1\bin>mysqld --defaults-file="C:\Program F

iles\MySQL\MySQL Server 5.1\my.ini" --console --skip-grant-tables

110301 10:29:47 [Warning] '--default-character-set' is deprecated and will be re

moved in a future release. Please use '--character-set-server' instead.

110301 10:29:47 [Note] Plugin 'FEDERATED' is disabled.

110301 10:29:47  InnoDB: Initializing buffer pool, size = 46.0M

110301 10:29:47  InnoDB: Completed initialization of buffer pool

110301 10:29:48  InnoDB: Started; log sequence number 0 44233

110301 10:29:48 [Note] mysqld: ready for connections.

Version: '5.1.55-community'  socket: ''  port: 3306  MySQL Community Server (GPL)

(此處輸入ctrl+c)

110301 10:30:58 [Note] mysqld: Normal shutdown

 

110301 10:30:58  InnoDB: Starting shutdown...

110301 10:31:03  InnoDB: Shutdown completed; log sequence number 0 44233

110301 10:31:03 [Note] mysqld: Shutdown complete

Mysql服務停止了,輸入命令:

net start mysql,即可重新啓動mysql服務,如:

===============================================================================

C:\Program Files\MySQL\MySQL Server 5.1\bin>net start mysql

MySQL 服務正在啓動 .

MySQL 服務已經啓動成功。

 

C:\Program Files\MySQL\MySQL Server 5.1\bin>

===============================================================================

 

 

全部流程的操作信息,如下:

停止mysql服務方法1:

停止mysql服務方法2:

停止mysql服務方法3:

 

第一個CMD窗口:

===============================================================================

C:\Program Files\MySQL\MySQL Server 5.1\bin>mysqld --defaults-file="C:\Program Files\MySQL\MySQL Server 5.1\my.ini" --console --skip-grant-tables

110301  9:20:07 [Warning] '--default-character-set' is deprecated and will be re

moved in a future release. Please use '--character-set-server' instead.

110301  9:20:07 [Note] Plugin 'FEDERATED' is disabled.

110301  9:20:07  InnoDB: Initializing buffer pool, size = 46.0M

110301  9:20:07  InnoDB: Completed initialization of buffer pool

110301  9:20:07  InnoDB: Started; log sequence number 0 44233

110301  9:20:07 [Note] mysqld: ready for connections.

Version: '5.1.55-community'  socket: ''  port: 3306  MySQL Community Server (GPL)

===============================================================================

第二個CMD窗口:

===============================================================================

Microsoft Windows XP [版本 5.1.2600]

(C) 版權所有 1985-2001 Microsoft Corp.

 

C:\Documents and Settings\Administrator>mysql -uroot -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.1.55-community MySQL Community Server (GPL)

 

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

This software comes with ABSOLUTELY NO WARRANTY. This is free software,

and you are welcome to modify and redistribute it under the GPL v2 license

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| test               |

+--------------------+

3 rows in set (0.00 sec)

 

mysql> use mysql;

Database changed

mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';

Query OK, 2 rows affected (0.03 sec)

Rows matched: 2  Changed: 2  Warnings: 0

 

mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

 

mysql> quit

Bye

 

C:\Documents and Settings\Administrator>mysql -uroot -p

Enter password: ***********

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 9

Server version: 5.1.55-community MySQL Community Server (GPL)

 

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

This software comes with ABSOLUTELY NO WARRANTY. This is free software,

and you are welcome to modify and redistribute it under the GPL v2 license

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql>

===============================================================================

結束第一個窗口,輸入ctrl+c命令停止mysql服務:

C:\Program Files\MySQL\MySQL Server 5.1\bin>mysqld --defaults-file="C:\Program Files\MySQL\MySQL Server 5.1\my.ini" --console --skip-grant-tables

110301 10:29:47 [Warning] '--default-character-set' is deprecated and will be re

moved in a future release. Please use '--character-set-server' instead.

110301 10:29:47 [Note] Plugin 'FEDERATED' is disabled.

110301 10:29:47  InnoDB: Initializing buffer pool, size = 46.0M

110301 10:29:47  InnoDB: Completed initialization of buffer pool

110301 10:29:48  InnoDB: Started; log sequence number 0 44233

110301 10:29:48 [Note] mysqld: ready for connections.

Version: '5.1.55-community'  socket: ''  port: 3306  MySQL Community Server (GPL)

(此處輸入ctrl+c)

110301 10:30:58 [Note] mysqld: Normal shutdown

 

110301 10:30:58  InnoDB: Starting shutdown...

110301 10:31:03  InnoDB: Shutdown completed; log sequence number 0 44233

110301 10:31:03 [Note] mysqld: Shutdown complete

 

重新啓動mysql服務:

C:\Program Files\MySQL\MySQL Server 5.1\bin>net start mysql

MySQL 服務正在啓動 .

MySQL 服務已經啓動成功。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章