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

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

当使用host参数为“localhost”连接Mysql服务时,会优先使用“sock文件”进行连接,而不是使用“IP:端口”进行连接,而Mysql尝试使用“sock文件”进行连接时,却无法获取“sock文件”的位置。
 
要解决此错误,有两种解决方法:

  • 将连接参数“host”的值由“localhost”改成“127.0.0.1”;
  • 按下面方法,在“/etc/mysql.cnf”中指定“sock文件”位置。

1、查看mysql服务的socket文件位置:
  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位置在哪里就可以。

2、修改my.cnf文件:
在/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

[client]
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文件即可,如:

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

4、python连接mysql提示"Can't connect to local MySQL server through socket..."的解决方法:
  在连接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()

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

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

  1. 启动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/ ...

  2. Can’t connect to local MySQL server through socket的解决方法

    http://www.aiezu.com/db/mysql_cant_connect_through_socket.html mysql,mysqldump,php连接mysql服务常会提示下面错误: ...

  3. 【转】pam_mysql - MySQL error (Can't connect to local MySQL server through socket

    转自:http://350201.blog.51cto.com/340201/1034672 参照 http://wjw7702.blog.51cto.com/5210820/936244博 主做的p ...

  4. Mac下安装Mysql出现 Can’t connect to local MySQL server through socket '/tmp/mysql.sock'

    在Mac下安装mysql出现 Can't connect to local MySQL server through socket '/tmp/mysql.sock' 错误,解决如下: $ unset ...

  5. MAC中Django中runserver提示Can't connect to local MySQL server through socket '/tmp/mysql.sock错误

    好像不止遇到一次,直接Google就可以了,在stackoverflow中就有答案,答案就是你没有开MySQL - -. stackoverflow链接见 http://stackoverflow.c ...

  6. 关于使用deepin在linux下安装mysql出现Can't connect to local MySQL server through socket '/tmp/mysql/mysql.sock' (2)的解决方法

    根据目录/etc/mysql打开文件debain.cnf 此时文本里的内容为 # Automatically generated for Debian scripts. DO NOT TOUCH![c ...

  7. 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来安装,却出现了这个问题 卸载又从安装还是有问题, 搜了好久在stackov ...

  8. PHP连接MySQL报错:SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket 'MySQL' (2)

    如下所示,PHP连接MySQL报错: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket 'MySQL' ...

  9. mysql Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’

    mysql Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ 今天在linux中安装了mys ...

随机推荐

  1. Apache Shiro 学习记录3

    晚上看了教程的第三章....感觉Shiro字符串权限很好用....但是教程举的例子太少了.....而且有些地方讲的不是很清楚....所以我也自己测试了一下....记录一下测试的结果.... (1) * ...

  2. CSS常用技术总结!~~

    //放大屏幕,背景图不变 background: url(x.png) no-repeat 0 0; background-image: -webkit-image-set(url(logo_db.p ...

  3. codevs1257 打砖块

    题目描述 Description 在一个凹槽中放置了n层砖块,最上面的一层有n块砖,第二层有n-1块,--最下面一层仅有一块砖.第i层的砖块从左至右编号为1,2,--i,第i层的第j块砖有一个价值a[ ...

  4. sql注入时易被忽略的语法技巧以及二次注入

    那些容易被忽略.容易被弄错的地方 sql注入时的技巧 ========================================================================= ...

  5. java基础 super 子类调用父类

    如果希望在子类中,去调用父类的构造方法,要求在子类的构造函数调用 example如下: package test; /* * 如果希望在子类中,去调用父类的构造方法,要求在子类的构造函数调用 * */ ...

  6. 微信小程序开发工具的数据,配置,日志等目录在哪儿? 怎么找?

    原文地址:http://www.wxapp-union.com/portal.php?mod=view&aid=359 本文由本站halfyawn原创:感谢原创者:如有疑问,请在评论内回复   ...

  7. (转)android 蓝牙通信编程

    转自:http://blog.csdn.net/pwei007/article/details/6015907 Android平台支持蓝牙网络协议栈,实现蓝牙设备之间数据的无线传输. 本文档描述了怎样 ...

  8. ng-repeat 嵌套访问父作用域里的属性

    在一个项目中,需要嵌套循环输出一个二维表的里的数据 数据结构 [ { id:1, list:[ { id:1, name:'li' } ] }, { id:2, list:[ { id:1, name ...

  9. Qtp常见问题

    (1)Qtp不能识别对象 插件加载错误 IE内未加载BHOManager加载项

  10. VS2013全攻略

    http://blog.csdn.net/cpp12341234/article/details/45371269 挺好的,喜欢