ERROR 2002 (HY000): Cant connect to local MySQL server through socket的解决方法

连接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

mysqlmysqldumpmysqldump,php连接mysql服务常会提示下面错误:

1

ERROR 2002 (HY000): Can'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 '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, 'root', '');

$rs = $db->query("SELECT * FROM qrtest");

while($row = $rs->fetch()){

    print_r($row);

}

?>




  

 

 

 
                                                            

            

  

   

   

 

  

 

  

 
>

     

      

 

 

    

>

 
>

          

       



    

 
  

        

           

 

   

   

   

 

 

 

   

 



>

时间: 2024-10-02 17:09:14

ERROR 2002 (HY000): Cant connect to local MySQL server through socket的解决方法的相关文章

Can’t connect to local MySQL server through socket

mysql,Mysqldump,Mysqladmin,php连接mysql服务常会提示下面错误: 1 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 这是由于修改mysql服务的了socket文件mysql.sock位置,而导致无法通过mysql socket文件连接到mysql服务引起的,具体解决办法如下:1.查看mysql服务的sock

connection to dat Can&amp;#39;t connect to local MySQL server through socket &amp;#39;/var/lib/mysql/mysql.sock&amp;#39; (2)

[root@t1 ~]# tail /tmp/zabbix_server.log  23392:20160303:131022.598 [Z3001] connection to database 'zabbix' failed: [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)  23392:20160303:131022.598 Database is down.

ERROR 2002 (HY000): Can&#039;t connect to local MySQL server through socket &#039;/var/lib/mysql/mysql.sock&#039; (2)

从供应商手中接手一个云平台(Windwos Azure)上的MySQL数据库,登录数据库时遇到错误: $mysql -uroot -p Enter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 检查配置文件/etc/my.cnf发现供应商修改了mysql数据库的数据存储目录,另外mysql.sock文件位置也变更为了/

ERROR 2002 (HY000): Can&amp;#39;t connect to local MySQL server through socket &amp;#39;/var/lib/mysql/mysql.sock&amp;#39; (

mysql启动成功 service mysqld start  mysql is OK mysql登录错误 : [root@localhost ~]# mysql -uroot -p123456ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 解决: vim /etc/my.cnf 找到 socket路径 scoket=/tmp/mysql.

MYSQL错误解决:ERROR 2002 (HY000): Can&#039;t connect to local MySQL server through socket &#039;/var/lib/mysql/mysql.sock&#039; (2)

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 据说这个问题的原因很多,当然,我自己这个问题我是清楚的,就是mysql.sock的位置所致,一个是PHP要求的,一个是MYSQLD定义的,一个是MY.CONF指定的...SO.... 暂时不能统一的话,用软链接LN -S命令解决吧..   方案5      摘要:解决不能通过mysql .so

MYSQL错误解决:ERROR 2002 (HY000): Can&amp;#39;t connect to local MySQL server through socket &amp;#39;/var/lib/mysql/mysql.sock&amp;#39; (2)

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 据说这个问题的原因很多,当然,我自己这个问题我是清楚的,就是mysql.sock的位置所致,一个是PHP要求的,一个是MYSQLD定义的,一个是MY.CONF指定的...SO.... 暂时不能统一的话,用软链接LN -S命令解决吧..   方案5      摘要:解决不能通过mysql .so

ERROR 2002 (HY000): Can&#039;t connect to local MySQL server through socket

    在安装好了MySQL之后,使用了新的配置文件后,MySQL服务器可以成功启动,但在登陆的时候出现了ERROR 2002 (HY000): Can't connect to local MySQL server through socket,即无法通过socket连接到mysql服务器,同时提供了socket文件的位置.下面是这个问题的描述与解决办法.   1.故障现象[root@SZDB mysqldata]# mysql -uroot -p123456Warning: Using a

error: &amp;#39;Can&amp;#39;t connect to local MySQL server through socket &amp;#39;/var/lib/mysql/mysql.sock&amp;#39; (2)&amp;#39;

 [root@luozhonghua ~]#   /usr/bin/mysqladmin -u root password 'aaaaaa' /usr/bin/mysqladmin: connect to server at 'localhost' failed error: 'Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)' Check that mysqld is runni

XtraBackup出现 Can&#039;t connect to local MySQL server through socket &#039;/tmp/mysql.sock&#039;

Xtrabackup做备份时遇到下面错误信息MySQL server: Can't connect to local MySQL server through socket '/tmp/mysql.so'(2)   [root@MySQL-DB ~]# innobackupex --defaults-file=/usr/my.cnf --user=root --password=password  /mnt/resource/mysql_backup 160322 22:28:43 innoba