socket的解决方法

mysql,mysqldump,Mysqladmin,php连接mysql服务常会提示上面错误:

正如所示,PHP连接MySQL报错:

mysql,Mysqldump,Mysqladmin,php连接mysql服务常会提醒下边错误:

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket
‘/var/lib/mysql/mysql.sock’ (2)

SQLSTATE[HY000] [2002] Can’t connect to local MySQL server through
socket ‘MySQL’ (2)

 

当使用host参数为“localhost”连接Mysql服务时,会预先利用“sock文件”举办一而再,并非接受“IP:端口”举行连接,而Mysql尝试接收“sock文件”实行三番五次时,却未有任何进展得到“sock文件”的职位。
 
要解决此错误,有三种减轻方法:

测验代码如下:

1
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)
  • 将接连参数“host”的值由“localhost”改成“127.0.0.1”;
  • 按上边方法,在“/etc/mysql.cnf”中内定“sock文件”地方。

<?php
try
{
 $dsn = ‘mysql:dbname=php-note;host=localhost;port=3306;charset=utf8’;
 $username = ‘root’;
 $password = ‘root’;
 new PDO( $dsn, $username, $password);
}
catch (PDOException $e)
{
 echo $e->getMessage();
}

 

 

【施工方案】

那是出于改进mysql服务的了socket文件mysql.sock地点,而形成敬谢不敏透过mysql
socket文件三番五次到mysql服务引起的,具体解决办法如下:
1、查看mysql服务的socket文件地点:
mysql socket文件的岗位是在/etc/my.cnf中安装的,cat /etc/my.cnf内容如下:

1、查看mysql服务的socket文件地方:
  mysql socket文件的职位是在/etc/my.cnf中装置的,cat
/etc/my.cnf内容如下:

把 host=localhost 改为 host=127.0.0.1 即可!
 
 
连续几天Mysql提示Can’t connect to local MySQL server through
socket的解决方法
 
mysql,Mysqldump,Mysqladmin,php连接mysql服务常会提醒上面错误:
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服务引起的,具体消释办法如下:

 

[mysqld]
datadir=/storage/db/mysql
socket=/storage/db/mysql/mysql.sock
user=mysql

1、查看mysql服务的socket文件地点:

1
2
3
4
[mysqld]
datadir=/storage/db/mysql
socket=/storage/db/mysql/mysql.sock
user=mysql

 

mysql socket文件的任务是在/etc/my.cnf中装置的,cat
/etc/my.cnf内容如下:
[mysqld]
datadir=/storage/db/mysql
socket=/storage/db/mysql/mysql.sock
user=mysql
里面socket等于的不二等秘书技正是socket文件的职位,我们就算校订my.cnf文件,告诉mysql,mysqldump,mysqladmin
mysql服务的socket地方在哪儿就可以。

里面socket等于的不二等秘书诀正是socket文件的任务,大家固然改正my.cnf文件,告诉mysql,mysqldump,mysqladmin mysql服务的socket地方在何地就足以。

内部socket等于的不二秘技正是socket文件的职责,大家若是校正my.cnf文件,告诉mysql,mysqldump,mysqladmin等mysql服务的socket地点在哪个地方就足以。

2、修改my.cnf文件:

2、修改my.cnf文件:
在/etc/my.cnf文件中增加如下内容,同等对待启mysqls服务,就能够解决mysql,mysqldump,mysqladmin的“Can’t
connect to local MySQL server through socket
‘/var/lib/mysql/mysql.sock’”问题:

 

在/etc/my.cnf文件中加多如下内容,人己一视启mysqls服务,就可以解决mysql,mysqldump,mysqladmin的“Can’t
connect to local MySQL server through socket
‘/var/lib/mysql/mysql.sock’”难点:
[mysqld]
datadir=/storage/db/mysql
socket=/storage/db/mysql/mysql.sock
[mysql]
socket=/storage/db/mysql/mysql.sock
[mysqldump]
socket=/storage/db/mysql/mysql.sock
[mysqladmin]
socket=/storage/db/mysql/mysql.sock

 

2、修改my.cnf文件:
在/etc/my.cnf文件中加多如下内容,同等对待启mysqls服务,就可以消释mysql,mysqldump,mysqladmin的“Can’t
connect to local MySQL server through socket
‘/var/lib/mysql/mysql.sock’”难点:

3、php连接mysql服务提醒”Can’t connect to local MySQL server through
socket…”的消除办法

1
2
3
4
5
6
7
8
9
10
11
12
[mysqld]
datadir=/storage/db/mysql
socket=/storage/db/mysql/mysql.sock
 
[mysql]
socket=/storage/db/mysql/mysql.sock
 
[mysqldump]
socket=/storage/db/mysql/mysql.sock
 
[mysqladmin]
socket=/storage/db/mysql/mysql.sock

[mysqld]
datadir=/storage/db/mysql
socket=/storage/db/mysql/mysql.sock

奇迹mysql服务正常运维,客户名密码也完全正确,使用php的mysql_connect函数却总是不了mysql,调用php的mysql_error(卡塔尔(英语:State of Qatar)函数提醒“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文件就能够,如:
[MySQL]
…省略n行…
mysql.default_socket = “/storage/db/mysql/mysql.sock”

 

[mysql]
socket=/storage/db/mysql/mysql.sock

4、python连接mysql提醒”Can’t connect to local MySQL server through
socket…”的化解办法:

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文件就能够,如:

[client]
socket=/storage/db/mysql/mysql.sock

在连接mysql数据库函数中钦定socket文件,如下:
#!/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()

 

[mysqldump]
socket=/storage/db/mysql/mysql.sock

5、php pdo连接mysql提醒”Can’t connect to local MySQL server through
socket…”的缓慢解决格局:

1
2
3
[MySQL]
…省略n行…
mysql.default_socket = "/storage/db/mysql/mysql.sock"

[mysqladmin]
socket=/storage/db/mysql/mysql.sock

相符在连接字符串加多mysql socket文件的岗位就能够,如下:
<?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);
}
?>

 

3、php连接mysql服务提示”Can’t connect to local MySQL server through
socket…”的缓慢解决措施
  有时候mysql服务平常运行,客户名密码也完全准确,使用php的mysql_connect函数却三番若干次不了mysql,调用php的mysql_error(卡塔尔(英语:State of Qatar)函数提醒“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文件就能够,如:

4、python连接mysql提醒”Can’t connect to local MySQL server through
socket…”的缓和措施:
在接二连三mysql数据库函数中钦赐socket文件,如下:

[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()

#!/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()

 

  1. php pdo连接mysql提醒”Can’t connect to local MySQL server through
    socket…”的减轻格局:
      同样在接连字符串加多mysql socket文件的岗位就可以,如下:
  1. php pdo连接mysql提示”Can’t connect to local MySQL server through
    socket…”的撤废办法:
    风华正茂致在连接字符串增加mysql socket文件的职位即可,如下:

<?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);
}
?>

 

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);
}
?>