连接Mysql提示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服务的socket文件位置:
mysql socket文件的位置是在/etc/my.cnf中设置的,cat /etc/my.cnf内容如下:

1

2

3

4

[mysqld]

datadir=/storage/db/mysql

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

user=mysql

其中socket等于的路径就是socket文件的位置,我们只要修改my.cnf文件,告诉mysql,mysqldump,mysqladmin mysql服务的socket位置在哪里就可以。

2、修改my.cnf文件:
在/etc/my.cnf文件中添加如下内容,并重启mysqls服务,即可解决mysql,mysqldump,mysqladmin的“Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘”问题:

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

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-29 19:08:28

连接Mysql提示Can’t connect to local MySQL server through socket的解决方法的相关文章

zabbix连接Mysql提示Can’t connect to local MySQL server through socket的解决方法

要解决此错误,有两种解决方法: 1.将连接参数“host”的值由“localhost”改成“127.0.0.1”: 2.按下面方法,在“/etc/mysql.cnf”中指定“sock文件”位置: #vi  /etc/my.cnf [mysqld] datadir=/storage/db/mysql socket=/storage/db/mysql/mysql.sock [client] socket=/storage/db/mysql/mysql.sock 补充: zabbix到安装界面报错"

启动Mysql服务提示Can’t connect to local MySQL server through socket的解决方法

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

MAC中Django中runserver提示Can&#39;t connect to local MySQL server through socket &#39;/tmp/mysql.sock错误

好像不止遇到一次,直接Google就可以了,在stackoverflow中就有答案,答案就是你没有开MySQL - -. stackoverflow链接见 http://stackoverflow.com/questions/16325607/cant-connect-to-local-mysql-server-through-socket-tmp-mysql-sock 开启MySQL的命令如下: mysql.server start MAC中Django中runserver提示Can't co

MYSQL错误Can&#39;t connect to local MySQL server through socket &#39;/var/lib/mysql/mysq

1.创建/修改文件 /etc/my.cnf,至少增加/修改一行 [mysql] [client] socket = /tmp/mysql.sock #在这里写上你的mysql.sock的正确位置,通常不是在 /tmp/ 下就是在 /var/lib/mysql/ 下2.指定IP地址,使用tcp方式连接mysql,而不使用本地sock方式 #mysql -h127.0.0.1 -uuser -ppassword 3.为 mysql.sock 加个连接,比如说实际的mysql.sock在 /tmp/

Mac OS X下MySQL出现Can&#39;t connect to local MySQL server through socket &#39;/tmp/mysql.sock&#39; (2)的问题

网上类似的很多,大都是说修改my.cnf之类的,在我这种情况下不凑效,另外,好多都是Linux下的,试了好久,最后的原因竟然相当简单:MySQL服务没起来. 因为对mysql.sock文件不是很熟,刚开始很大的时间都浪费在了这上面,重装后都不行,后来进入/usr/local/mysql/bin/下使用mysqld启动,出现下面的问题 好吧,我用root启动,结果又出现 Please read "Security" section of the manual to find out ho

oracle Can&#39;t connect to X11 window server using &#39;:0.0&#39; 解决方法

Can't connect to X11 window server using ':0.0' 解决方法 1. 以oracle 用户登陆X window 或者 2. root 身份执行 # xhost + oracle Can't connect to X11 window server using ':0.0' 解决方法

Ubuntu14.04 安装MySQL 及Can‘t connect to local MYSQL server through socket ’/var/run/mysqld/mysqld.sock‘ (2)

今天安装Mysql 按着这个①http://www.cnblogs.com/zhuyp1015/p/3561470.html来安装,却出现了这个问题 卸载又从安装还是有问题, 搜了好久在stackoverflow找到个链接 http://stackoverflow.com/questions/11990708/error-cant-connect-to-local-mysql-server-through-socket-var-run-mysqld-mysq ②http://www.cyberc

PHP连接MySQL报错:SQLSTATE[HY000] [2002] Can&#39;t connect to local MySQL server through socket &#39;MySQL&#39; (2)

如下所示,PHP连接MySQL报错: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket 'MySQL' (2) 测试代码如下: <?php try { $dsn = 'mysql:dbname=php-note;host=localhost;port=3306;charset=utf8'; $username = 'root'; $password = 'root'; new PDO( $dsn,

Can‘t connect to local MySQL server through socket

连接Mysql提示Can't connect to local MySQL server through socket的解决方法 2013年9月6日 aiezu 发表评论 阅读评论 mysql,Mysqldump,Mysqladmin,php连接mysql服务常会提示下面错误: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/<span class='wp_keywordlink_a