在安裝好了MySQL之後,使用了新的配置檔案後,MySQL伺服器可以成功啟動,但在登陸的時候出現了ERROR 2002 (HY000): Can't connect to local MySQL server through socket,即無法通過socket連接配接到mysql伺服器,同時提供了socket檔案的位置。下面是這個問題的描述與解決辦法。
一、故障現象
在登入mysql資料庫時報錯為如下所示:
~]# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/application/mysql5.1.72/tmp/mysql.sock' (2)
二、故障分析
#檢視mysql執行個體的狀态
~]# netstat -tunp | grep 3306
tcp 0 0 :::3306 :::* LISTEN 13001/mysqld
#檢視my.cnf關于socket的配置
~]# more /etc/my.cnf |grep sock
~]# more /etc/my.cnf |grep sock
socket = /application/mysql5.1.72/tmp/mysql.sock
#由上可知my.cnf中定義的為/tmp目錄下,而錯誤提示為/application/mysql5.1.72/tmp/mysql.sock目錄下
#也就是說mysqld已經聲稱了正确的sock檔案,但用戶端連接配接還是從初始目錄去找sock檔案
三、解決方法
先停止mysql伺服器
~]# /etc/init.d/mysqld stop
Shutting down MySQL.[ OK ]
#修改my.cnf,如下
~]# vi /etc/my.cnf
[mysql]
no-auto-rehash
socket = /application/mysql5.1.72/tmp/mysql.sock #添加該行
#重新開機mysql伺服器
~]# service mysqld start
Starting MySQL..[ OK ]
#再次連接配接正常
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.72 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
本文轉自chengong1013 51CTO部落格,原文連結:http://blog.51cto.com/purify/1792482,如需轉載請自行聯系原作者