Linux下忘记mysql密码解决全方案
作者:转载自:网络更新时间:2009-9-12

想登录mysql数库, 突然记不得mysql下root用户的密码了。
下面是处理过程
1:关闭mysql(一定不要用kill -9 pid或pkill mysql之类的命令, 用这种命令尽管会强制关闭数据库,但使得数据库有很多的碎片,容易使得数据crash(我图方便就用了两次,尽管我知道不能用pkill,数据库就crash了。最好用service mysql stop关闭它)
2:测试是否真正关闭
3:开启mysql用--skip-grant-tables 项,即可不提示输入密码
4:mysql -u root -p 通过空密码进入mysql,
5:选择mysql库下的user表,修改root用户对应当密码
6:flush privileges 刷新缓冲区
7:退出mysql,重新登陆测试(此时密码已经生效,不需要重启mysql)
以下是我的处理过程!

[root@localhost ~]# service mysql stop   ##究竟是mysql还是mysqld要看你在/etc/rc.d/init.d下到底是mysql还是mysqld
[root@localhost ~]# netstat -antl | grep 3306
[root@localhost ~]# /usr/local/mysql/bin/mysqld_safe --skip-grant-tables --user=mysql &
[1] 3701
[root@localhost ~]# Starting mysqld daemon with databases from /usr/local/mysql/data

[root@localhost ~]# netstat -antl | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.83 MySQL Community Server (GPL)

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

mysql> update user set password=password('root') where user='root';
ERROR 1046 (3D000): No database selected
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=password('root') where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.83 MySQL Community Server (GPL)

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