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(卡塔尔函数提醒“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(卡塔尔函数提醒“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);
}
?>

发表评论