连接MySQL提示ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)的解决方法
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
原文:http://www.aiezu.com/db/mysql_cant_connect_through_socket.html
mysql,mysqldump,mysqldump,php连接mysql服务常会提示下面错误:
1 | ERROR 2002 ( HY000 ) : Can & #039;t connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) |
产生此问题的原因一般有两个:
1、mysql服务未正常运行:
由于mysql的socket文件是由mysqld服务启动时创建的,如果mysqld服务未正常启动,socket文件自然也不会被创建,当然会找不到socket文件了。对于判断mysql服务是否启动,我们可以使用下面命令:
1 2 3 4 5 6 7 8 | # 1、 端口是否打开 [ root @ aiezu . com ~ ] # lsof -i:3306 COMMAND PID USER FD TYPE DEVICE SIZE / OFF NODE NAME mysqld 12207 mysql 14u IPv4 52350 0t0 TCP * : mysql ( LISTEN )
# 2、mysqld服务是否正在运行 [ root @ aiezu . com ~ ] # service mysqld status mysqld ( pid 4717 ) is running . . . |
2、socket文件路径在配置文件中设置不完整:
这一般是由于我们修改了mysql配置“/etc/my.cnf”引起的。比如我们修改了配置文件中“[mysql]”选项下的“socket”参数,而未指定“[client]”、“[mysql]”选项的“socket”参数,导致mysql使用默认的socket文件位置去寻找socket文件,从而导致未找到socket文件而引发此错误。
解决办法:
下面是解决方法,更详细的介绍可以参考:http://aiezu.com/article/mysql_cant_connect_through_socket.html
1、mysql服务未正常运行:
如果是服务未启动,我们运行“service mysqld start”启动服务即可。如果服务启动不了,就去查看mysql服务日志,寻找原因并解决再启动
1 2 3 4 5 6 7 | [ root @ aiezu . com ~ ] # service mysqld start Starting mysqld : [ OK ] [ root @ aiezu . com ~ ] # lsof -i:3306 COMMAND PID USER FD TYPE DEVICE SIZE / OFF NODE NAME mysqld 14109 mysql 10u IPv4 247183583 0t0 TCP * : mysql ( LISTEN ) [ root @ aiezu . com ~ ] # service mysqld status mysqld ( pid 14109 ) is running . . . |
2、完善mysql配置文件:
如果确认mysql服务正常运行,还提示文章标题的此错误,那就是“/etc/my.cnf”配置文件的问题了。解决办法是修改“/etc/my.cnf”配置文件,在配置文件中添加“[client]”选项和“[mysql]”选项,并使用这两个选项下的“socket”参数值,与“[mysqld]”选项下的“socket”参数值,指向的socket文件路径完全一致。如下:
1 2 3 4 5 6 7 8 9 10 11 12 | [ mysqld ] datadir = / storage / db / mysql socket = / storage / db / mysql / mysql . sock . . . 省略 n 行(爱 E 族) . . .
[ client ] default - character - set = utf8 socket = / storage / db / mysql / mysql . sock
[ mysql ] default - character - set = utf8 socket = / storage / db / mysql / mysql . sock |
其中socket等于的路径就是socket文件的位置,我们只要修改my.cnf文件,告诉mysql,mysqldump,mysqladmin等命令,mysql服务的socket文件位置在哪里,然后重启mysqld服务即可。
3、php连接mysql服务提示"Can't connect to local MySQL server through socket..."的解决方法
有时候mysql服务正常运行,用户名密码也完全正确,使用php的mysql_connect函数却连接不了mysql,调用php的mysql_error()函数提示“Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'”,这是我们需要修改/etc/php.ini文件。
在/etc/php.ini文件中"[MySQL]"项下找到"mysql.default_socket",并设置其值指向正确的mysql服务socket文件即可,如:
1 2 3 | [ MySQL ] . . . 省略 n 行 . . . mysql . default_socket = "/storage/db/mysql/mysql.sock" |
4、python连接mysql提示"Can't connect to local MySQL server through socket..."的解决方法:
在连接mysql数据库函数中指定socket文件,如下:
1 2 3 4 5 6 7 8 | #!/usr/bin/python from MySQLdb import connect conn = connect ( db = "pzy" , user = "root" , host = "localhost" , unix_socket = "/storage/db/mysql/mysql.sock" ) cur = conn . cursor ( ) count = cur . execute ( "show databases" ) print & #039;there has %s dbs' % count conn . commit ( ) conn . close ( ) |
5. php pdo连接mysql提示"Can't connect to local MySQL server through socket..."的解决方法:
同样在连接字符串添加mysql socket文件的位置即可,如下:
1 2 3 4 5 6 7 8 | <!--?php $dsn = "mysql:host=localhost;dbname=pzy;unix_socket=/storage/db/mysql/mysql.sock" ; $db = new PDO ( $dsn , & #039;root', ''); $rs = $db -> query ( "SELECT * FROM qrtest" ) ; while ( $row = $rs -> fetch ( ) ) { print_r ( $row ) ; } ?> |
在安装好了MySQL之后,使用了新的配置文件后,mysql服务器可以成功启动,但在登陆的时候出现了ERROR 2002 (HY000): Can't connect to local MySQL server through socket,即无法通过socket连接到mysql服务器,同时提供了socket文件的位置。下面是这个问题的描述与解决办法。
1、故障现象
[root@SZDB mysqldata]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/data/mysqldata/mysql.sock' (111)
#故障环境
[root@SZDB mysqldata]# more /etc/issue
CentOS release 5.9 (Final)
Kernel \r on an \m
2、故障分析
#查看mysql实例的状态
[root@SZDB mysqldata]# netstat -ntlp | grep 3306
tcp 0 0 :::3306 :::* LISTEN 13001/mysqld
#查看my.cnf关于socket的配置
[root@SZDB mysqldata]# more /etc/my.cnf |grep sock
socket = /tmp/mysql.sock
#由上可知my.cnf中定义的为/tmp目录下,而错误提示为/data/mysqldata/目录下
#也就是说mysqld已经声称了正确的sock文件,但客户端连接还是从初始目录去找sock文件
#下面查看后台日志,有个ERROR,是关于满查询日志的,是由于目录不存在而产生的错误,与当前故障无关
[root@SZDB mysqldata]# more SZDB.err
............
2014-10-11 13:17:21 13001 [Note] InnoDB: 5.6.12 started; log sequence number 1625997
/app/soft/mysql/bin/mysqld: File '/log/mysql_logs/slowquery.log' not found (Errcode: 2 - No such file or directory)
2014-10-11 13:17:21 13001 [ERROR] Could not use /log/mysql_logs/slowquery.log for logging (error 2). Turning logging off for the who
le duration of the MySQL server process. To turn it on again: fix the cause, shutdown the MySQL server and restart it.
2014-10-11 13:17:21 13001 [Note] Server hostname (bind-address): '*'; port: 3306
2014-10-11 13:17:21 13001 [Note] IPv6 is available.
2014-10-11 13:17:21 13001 [Note] - '::' resolves to '::';
2014-10-11 13:17:21 13001 [Note] Server socket created on IP: '::'.
2014-10-11 13:17:21 13001 [Note] Event Scheduler: Loaded 0 events
2014-10-11 13:17:21 13001 [Note] /app/soft/mysql/bin/mysqld: ready for connections.
Version: '5.6.12-log' socket: '/tmp/mysql.sock' port: 3306 Source distribution
#Author :Leshami
#Blog : http://blog.csdn.net/leshami
3、解决故障
a、通过配置my.cnf mysql选项socket文件位置解决
#先停止mysql服务器
[root@SZDB mysqldata]# service mysqld stop
Shutting down MySQL.[ OK ]
#修改my.cnf,如下
[root@SZDB mysqldata]# vi /etc/my.cnf
[mysql]
no-auto-rehash
socket = /tmp/mysql.sock #添加该行
#重启mysql服务器
[root@SZDB mysqldata]# service mysqld start
Starting MySQL..[ OK ]
#再次连接正常
[root@SZDB mysqldata]# mysql -uroot -p
Enter password:
mysql> show variables like 'version';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| version | 5.6.12-log |
+---------------+------------+
b、为socket文件建立链接方式
[root@SZDB mysqldata]# ln -s /tmp/mysql.sock /data/mysqldata/mysql.sock
ln: creating symbolic link `/data/mysqldata/mysql.sock' to `/tmp/mysql.sock': File exists
[root@SZDB mysqldata]# rm mysql.sock #上面提示文件存在,所以删除之前的mysql.sock文件
[root@SZDB mysqldata]# ln -s /tmp/mysql.sock /data/mysqldata/mysql.sock
[root@SZDB mysqldata]# ls -hltr mysql.sock
lrwxrwxrwx 1 root root 15 Oct 11 14:00 mysql.sock -> /tmp/mysql.sock
[root@SZDB mysqldata]# mysql -uroot -p
Enter password:
mysql> show variables like 'socket';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| socket | /tmp/mysql.sock |
+---------------+-----------------+
有时候,当我们使用“mysql”、“mysqladmin”、“mysqldump”等命令管理数据库时,服务器抛出类似如下错误:
1 | ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) |
这个错误是由于什么原因导致的呢?请看后面爱E族(aiezu.com)为您提供的分析。
下面我们通过三种方式来连接,然后观察提示的错误信息:
123456 | [root@aiezu.com ~]# mysqlERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)[root@aiezu.com ~]# mysql -h localhostERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)[root@aiezu.com ~]# mysql -h 127.0.0.1ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) |
通过上面实验可以看出,前面两种方式都能产生标题中的错误,而第三种方式连接是不会产生标题中的错误的(第三种方式这里产生的是由于密码问题拒绝访问的错误信息)(爱E族)。
这是由于我们连接数据库使用的主机名参数为“localhost”,或者未使用主机名参数、服务器默认使用“localhost”做为主机名(爱E族)。 使用主机名参数为“localhost”连接mysql服务端时,mysql客户端会认为是连接本机,所以会尝试以socket文件方式进行连接(socket文件连接方式,比“ip:端口”方式效率更高),这时根据配置文件“/etc/my.cnf”的路径,未找到相应的socket文件,就会引发此错误。
由于“socket”文件是由mysql服务运行时创建的,如果提示“ERROR 2002 (HY000): Can't connect to local MySQL server through socket '***' (2)”,找不到“socket”文件,我们首先要确认的是mysql服务是否正在运行。
123456789101112131415 | # 1、 端口是否打开[root@aiezu.com ~]# lsof -i:3306COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEmysqld 12207 mysql 14u IPv4 52350 0t0 TCP *:mysql (LISTEN)# 2、mysqld服务是否正在运行[root@aiezu.com ~]# service mysqld statusmysqld (pid 4717) is running...# 3、如果mariadb,同样方法查服务是否正在运行:[root@aiezu.com ~]# service mariadb statusRedirecting to /bin/systemctl status mariadb.service● mariadb.service - MariaDB database server Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled) Active: active (running) since 四 2016-11-03 13:47:37 CST; 23min ago |
确定mysql服务正常运行后,产生此错误的原因只剩下“socket”文件路径不正确了,我们可以使用“find”命令或者“lsof”命令来确定socket文件的正确路径:
12345 | [root@aiezu.com ~]# lsof -c mysqld|grep sock$mysqld 4717 mysql 12u unix 0xffff88010a655b80 0t0 77474827 /storage/db/mysql/mysql.sock[root@aiezu.com ~]# find / -name '*.sock'/storage/db/mysql/mysql.sock |
修改“/etc/my.cnf”配置文件,在配置文件中添加“[client]”选项和“[mysql]”选项,并使用这两个选项下的“socket”参数值,与“[mysqld]”选项下的“socket”参数值,指向的socket文件路径完全一致。如下:
123456789101112 | [mysqld]datadir=/storage/db/mysqlsocket=/storage/db/mysql/mysql.sock...省略n行(爱E族)...[client]default-character-set=utf8socket=/storage/db/mysql/mysql.sock[mysql]default-character-set=utf8socket=/storage/db/mysql/mysql.sock |
修改完后,重启mysqld服务,即可解决此问题。
使用“ln -s /storage/db/mysql/mysql.sock /var/lib/mysql/mysql.sock”命令,将正确的socket文件位置,软链接到提示错误的socket文件路径位置,即可解决此问题:
12345 | [root@aiezu.com ~]# ls /var/lib/mysql/mysql.sockls: cannot access /var/lib/mysql/mysql.sock: No such file or directory[root@aiezu.com ~]# ln -s /storage/db/mysql/mysql.sock /var/lib/mysql/mysql.sock[root@aiezu.com ~]# ls /var/lib/mysql/mysql.sock/var/lib/mysql/mysql.sock |
这篇文章只讲述了mysql提供的“mysql”、“mysqldump”、“mysqladmin”等命令提示“ERROR 2002 (HY000): Can't connect to local MySQL server through socket '***' (2)”的解决方法,如果需要解决“php”、“perl”、“python”等脚本语言提示此错误的问题,请参考爱E族的另外一篇文章:http://aiezu.com/article/mysql_php_connect_through_socket.html