天天看点

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp

在安装好了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,如需转载请自行联系原作者