1. 程式人生 > >Linux 連線mysql報錯Access denied for user 'root'@'localhost'

Linux 連線mysql報錯Access denied for user 'root'@'localhost'

忘記mysql密碼,使用不對的伺服器密碼,在連線伺服器的時候,常常會提示 Access denied 錯誤,如下圖所示:

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

遇到上面情況怎麼辦,請按如下步驟操作:

1、停止mysql服務

[[email protected] bin]# chkconfig --list | grep -i mysql
mysql           0:關閉  1:關閉  2:啟用  3:啟用  4:啟用  5:啟用  6:關閉
[
[email protected]
bin]# service mysql stop Shutting down MySQL [確定]
2、用mysqld_safe重啟服務
[[email protected] bin]# ./mysqld_safe --user=root --skip-grant-tables --skip-networking &
[1] 3818
[[email protected] 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、重啟服務

[[email protected] bin]#service mysql stop


[[email protected]
bin]# service mysql start

5、再次連線資料庫

[[email protected] 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.