很多时候,我们会忘记mysql密码,需要重置mysql密码。

其实mysql密码重置非常简单,下面是记录了linux下操作的过程。

[root@CactiEZ ~]# mysql -u root -p        #尝试登录mysql
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)        #因密码不正确登录失败
[root@CactiEZ ~]# service mysqld stop        #停止mysqld
Stopping mysqld:                                           [  OK  ]
You have new mail in /var/spool/mail/root
[root@CactiEZ ~]# mysqld_safe --skip-grant-tables&    #跳过权限表启动
[1] 20462
130326 14:01:10 mysqld_safe Logging to '/var/log/mysqld.log'.
130326 14:01:10 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
 
[root@CactiEZ ~]# mysql -u root    #以root身份登录mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.47 Source distribution
 
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> UPDATE
    -> mysql.user
    -> SET
    -> Password = password('yourpasswd')
    -> WHERE
    -> User = 'root';                                #更改root用户的密码
Query OK, 3 rows affected (0.02 sec)
Rows matched: 3  Changed: 3  Warnings: 0
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql> exit
Bye
[root@CactiEZ ~]# service mysqld restart        #将mysqld重新启动
130326 14:03:51 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[1]+  Done                    mysqld_safe --skip-grant-tables
[root@CactiEZ ~]# service mysqld start
Starting mysqld:                                           [  OK  ]
[root@CactiEZ ~]# mysql -u root -p                #使用新设置的密码登录
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.47 Source distribution
 
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>                                                 #登录成功