解决Linux MySQL报错ERROR 2002

在Linux安装MySQL有时候会出现[mysql]ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 这样的错误,具体解决办法如下:

[html]  view plain  copy
  1. [root@www ~]# rm -rf /var/lib/mysql/*  
  2. [root@www ~]# rm /var/lock/subsys/mysqld  
  3. rm: remove regular empty file `/var/lock/subsys/mysqld'? y  
  4. [root@www ~]# killall mysqld  
  5. [root@www ~]#  service mysqld start  
  6. [root@www ~]# /etc/rc.d/init.d/mysqld status  
  7. mysqld (pid 5457) is running...  

[html]  view plain  copy
  1. [root@r710-1 /]# mysql -u root  
  2. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)  
  3. [root@r710-1 /]# vi /etc/my.cnf  
  4. ##########添加如下内容:  
  5. [client]   
  6. socket=/var/lib/mysql/mysql.sock  

[html]  view plain  copy
  1. ##########保存退出后,  
  2. ##########重启mysql  
  3. [root@r710-1 /]# service mysqld restart  
  4. Shutting down MySQL.                                       [  OK  ]  
  5. Starting MySQL.                                            [  OK  ]  
  6.   
  7. [root@r710-1 /]# mysql -u root -p   
  8. Enter password:   
  9. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)  
  10. [root@r710-1 /]# mysql -u root  
  11. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  12. Your MySQL connection id is 2  
  13. Server version: 5.1.45 MySQL Community Server (GPL)  
  14.   
  15. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  16.   
  17. mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'rootpassword' WITH GRANT OPTION;  
  18.   
  19. Query OK, 0 rows affected (0.00 sec)  
  20.   
  21. mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'rootpassword' WITH GRANT OPTION;  
  22.   
  23. exit  
  24. Bye  
  25. [root@r710-1 /]# mysql -u root -p  
原出处:http://www.linuxidc.com/Linux/2010-06/26890.htm

猜你喜欢

转载自blog.csdn.net/wei349914638/article/details/80737165