MySQL错误ERROR 2002 (HY000): Can't connect to local MySQL server through socket
在安装好了MySQL之后,使用了新的配置文件后,MySQL服务器可以成功启动,但在登陆的时候出现了ERROR2002(HY000):Can'tconnecttolocalMySQLserverthroughsocket,即无法通过socket连接到mysql服务器,同时提供了socket文件的位置。下面是这个问题的描述与解决办法。
1、故障现象
[root@SZDBmysqldata]#mysql-uroot-p123456 Warning:Usingapasswordonthecommandlineinterfacecanbeinsecure. ERROR2002(HY000):Can'tconnecttolocalMySQLserverthroughsocket'/data/mysqldata/mysql.sock'(111) #故障环境 [root@SZDBmysqldata]#more/etc/issue CentOSrelease5.9(Final) Kernel\ronan\m
2、故障分析
#查看mysql实例的状态 [root@SZDBmysqldata]#netstat-ntlp |grep3306 tcp 0 0:::3306 :::* LISTEN 13001/mysqld #查看my.cnf关于socket的配置 [root@SZDBmysqldata]#more/etc/my.cnf|grepsock socket=/tmp/mysql.sock #由上可知my.cnf中定义的为/tmp目录下,而错误提示为/data/mysqldata/目录下 #也就是说mysqld已经声称了正确的sock文件,但客户端连接还是从初始目录去找sock文件 #下面查看后台日志,有个ERROR,是关于满查询日志的,是由于目录不存在而产生的错误,与当前故障无关 [root@SZDBmysqldata]#moreSZDB.err ............ 2014-10-1113:17:2113001[Note]InnoDB:5.6.12started;logsequencenumber1625997 /app/soft/mysql/bin/mysqld:File'/log/mysql_logs/slowquery.log'notfound(Errcode:2-Nosuchfileordirectory) 2014-10-1113:17:2113001[ERROR]Couldnotuse/log/mysql_logs/slowquery.logforlogging(error2).Turningloggingoffforthewho ledurationoftheMySQLserverprocess.Toturnitonagain:fixthecause,shutdowntheMySQLserverandrestartit. 2014-10-1113:17:2113001[Note]Serverhostname(bind-address):'*';port:3306 2014-10-1113:17:2113001[Note]IPv6isavailable. 2014-10-1113:17:2113001[Note] -'::'resolvesto'::'; 2014-10-1113:17:2113001[Note]ServersocketcreatedonIP:'::'. 2014-10-1113:17:2113001[Note]EventScheduler:Loaded0events 2014-10-1113:17:2113001[Note]/app/soft/mysql/bin/mysqld:readyforconnections. Version:'5.6.12-log' socket:'/tmp/mysql.sock' port:3306 Sourcedistribution
3、解决故障
a、通过配置my.cnfmysql选项socket文件位置解决
#先停止mysql服务器 [root@SZDBmysqldata]#servicemysqldstop ShuttingdownMySQL.[ OK ] #修改my.cnf,如下 [root@SZDBmysqldata]#vi/etc/my.cnf [mysql] no-auto-rehash socket=/tmp/mysql.sock #添加该行 #重启mysql服务器 [root@SZDBmysqldata]#servicemysqldstart StartingMySQL..[ OK ] #再次连接正常 [root@SZDBmysqldata]#mysql-uroot-p Enterpassword: mysql>showvariableslike'version'; +---------------+------------+ |Variable_name|Value | +---------------+------------+ |version |5.6.12-log| +---------------+------------+
b、为socket文件建立链接方式
[root@SZDBmysqldata]#ln-s/tmp/mysql.sock/data/mysqldata/mysql.sock ln:creatingsymboliclink`/data/mysqldata/mysql.sock'to`/tmp/mysql.sock':Fileexists [root@SZDBmysqldata]#rmmysql.sock #上面提示文件存在,所以删除之前的mysql.sock文件 [root@SZDBmysqldata]#ln-s/tmp/mysql.sock/data/mysqldata/mysql.sock [root@SZDBmysqldata]#ls-hltrmysql.sock lrwxrwxrwx1rootroot15Oct1114:00mysql.sock->/tmp/mysql.sock [root@SZDBmysqldata]#mysql-uroot-p Enterpassword: mysql>showvariableslike'socket'; +---------------+-----------------+ |Variable_name|Value | +---------------+-----------------+ |socket |/tmp/mysql.sock| +---------------+-----------------+