Linux 连接mysql报错Access denied for user 'root'@'localhost'

忘记mysql密码,使用不对的服务器密码,在连接服务器的时候,常常会提示 Access denied 错误,如下图所示:

[root@localhost bin]# ./mysql -uroot -p12345
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

遇到上面情况怎么办,请按如下步骤操作:

1、停止mysql服务

[root@localhost bin]# chkconfig --list | grep -i mysql
mysql           0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭
[root@localhost bin]# service mysql stop
Shutting down MySQL                                        [确定]
2、用mysqld_safe重启服务

[root@localhost bin]# ./mysqld_safe --user=root --skip-grant-tables --skip-networking &
[1] 3818
[root@localhost bin]# 111105 07:30:32 mysqld_safe Logging to '/usr/local/mysql/var/localhost.localdomain.err'.
111105 07:30:32 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var
./mysql -uroot mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.48-log 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.

3、重设密码

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




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




mysql> quit
Bye

4、重启服务

[root@localhost bin]#service mysql stop


[root@localhost bin]# service mysql start

5、再次连接数据库

[root@localhost bin]# ./mysql -uroot -p12345
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.48-log 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.


你可能感兴趣的:(SQL)