故障现象:

>>> import MySQLdb
>>> conn = MySQLdb.connect(host="localhost",user="root",passwd="tiange1003",db="tian",port=,charset="utf8")
Traceback (most recent call last):
File "<stdin>", line , in <module>
File "/usr/lib64/python2.7/site-packages/MySQLdb/__init__.py", line , in Connect
return Connection(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/MySQLdb/connections.py", line , in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")

原因分析:

这里主要是因为我们连接mysql的时候,host用的是localhost, 实际用的是UNIX Domain Socket来进行通信的。我们知道,UNIX Domain Socket的地址是一个socket类型的文件在文件系统中的路径,如果这个路径不存在的话,连接的时候就会失败。上面提示的错误原因是”Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)”,从字面意思上来看,是说无法通过’/var/lib/mysql/mysql.sock’这个socket来连接本地的mysql sever,这时候问题基本就比较明显了,应该是mysql配置的本地连接的socket不是’/var/lib/mysql/mysql.sock’这个路径的原因。接下来我们来验证我们的想法,打开mysql的配置文件(/etc/my.cnf),我们看到如下的内容:这里主要是因为我们连接mysql的时候,host用的是localhost, 实际用的是UNIX Domain Socket来进行通信的。我们知道,UNIX Domain Socket的地址是一个socket类型的文件在文件系统中的路径,如果这个路径不存在的话,连接的时候就会失败。上面提示的错误原因是”Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)”,从字面意思上来看,是说无法通过’/var/lib/mysql/mysql.sock’这个socket来连接本地的mysql sever,这时候问题基本就比较明显了,应该是mysql配置的本地连接的socket不是’/var/lib/mysql/mysql.sock’这个路径的原因。接下来我们来验证我们的想法,打开mysql的配置文件(/etc/my.cnf),我们看到如下的内容:

解决方式:

1、查看/etc/my.cnf配置文件

[mysqld]
datadir = /usr/local/mysql/data
log-error = /usr/local/mysql/data/error.log
pid-file = /usr/local/mysql/data/mysql.pid
user = mysql
tmpdir = /tmp

在这个配置文件里面添加

socket  = /tmp/mysql.sock

2、解决办法:
>>> import MySQLdb
>>> conn = MySQLdb.connect(host="localhost",user="root",passwd="tiange1003",db="tian",port=,charset="utf8",unix_socket='/tmp/mysql.sock')

python MySQLdb连接mysql时报错的更多相关文章

  1. 【转载】在使用JDBC连接MySql时报错:You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support

    在使用JDBC连接MySql时报错:You must configure either the server or JDBC driver (via the serverTimezone config ...

  2. 解决使用DBeaver连接MySQL时报错-The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone.

    解决使用DBeaver连接MySQL时报错,其实提示很明显. The server time zone value '�й���׼ʱ��' is unrecognized or represents ...

  3. python MySQLdb连接mysql失败(转载)

    最近了解了一下django,数据库选用了mysql, 在连接数据库的过程中,遇到一点小问题,在这里记录一下,希望能够对遇到同样的问题的朋友有所帮助,少走一些弯路.关于django,想在这里也额外说一句 ...

  4. python -- MySQLdb连接mysql数据库

    1. python安装mysql $ pip install mysql-python 2. 数据库连接程序: import MySQLdb # 打开数据库连接db = MySQLdb.connect ...

  5. 使用navicat连接mysql时报错:2059 - authentication plugin 'caching_sha2_password'

    首先从本地登录mysql数据库,进入mysql控制台,输入如下命令: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_passwo ...

  6. Ubuntu下使用mysqli-connect连接mysql时报错:ERROR 1698 (28000): Access denied for user 'root'@'localhost'

    LNMP安装好后,写了个index.php文件,里面的内容很简单,就是想测试php与mysql的通信是否正常,代码如下: <?php $host = 'localhost'; $user = ' ...

  7. Navicat Premium 12连接MySQL时报错2059和1045的解决办法

    参考连接:https://www.jianshu.com/p/15876ad165f5 https://jingyan.baidu.com/article/c275f6ba479ca9e33d7567 ...

  8. Nodejs 连接 mysql时报错 Error: Cannot enqueue Query after fatal error

    解决办法,参考:https://github.com/chill117/express-mysql-session/issues/18 我们只需在实例化SessionStore的时候,配置useCon ...

  9. 【small项目】MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:

    MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link ...

随机推荐

  1. java 项目坑记录

    1. spring项目引入了lombok jar包,且已在类上面添加@Data注释,还是关联不到 get() 和 set() 方法 需要安装扩展包 如果在线安装失败,提示错误readtime out, ...

  2. 移动端rem移动适配

    https://www.cnblogs.com/jingwhale/p/5741567.html

  3. tensorflow模型的保存与恢复,以及ckpt到pb的转化

    转自 https://www.cnblogs.com/zerotoinfinity/p/10242849.html 一.模型的保存 使用tensorflow训练模型的过程中,需要适时对模型进行保存,以 ...

  4. okhttp连接池:put,get方法&connection回收

    OkHttp连接池put和get方法: 在上一次[https://www.cnblogs.com/webor2006/p/9281429.html]咱们分析了连接拦截器,如下: 不管是Http1.0还 ...

  5. 基于IAP的STM32程序更新技术

    引言 嵌入式系统的开发最终需要将编译好的代码下载到具体的微控制器芯片上,而不同厂家的微控制器芯片有不同的下载方式.随着技术的发展和应用需求的更新,用户程序加载趋向于在线编程的方式,越来越多的芯片公司提 ...

  6. Python3-BaseType

    counter = 100 # 整型变量 miles = 1000.0 # 浮点型变量 name = "runoob" # 字符串 print(counter) print(mil ...

  7. [Javascript] Customize Behavior when Accessing Properties with Proxy Handlers

    A Proxy allows you to trap what happens when you try to get a property value off of an object and do ...

  8. PHP mysqli_multi_query() 函数

    实例 执行多个针对数据库的查询: <?php $con=mysqli_connect("localhost","my_user","my_pas ...

  9. Mac出现程序闪退的解决方案

    重置PRAM 1. 关机 2.按下电源键后,立即按下command + option + P +R 3.等到电脑出现4次重启的声音后,放开按键,重置成功 4.正常使用

  10. learning armbian steps(6) ----- armbian 源码分析(一)

    为了深入学习armbian,前面已经学习了如何手动构建arm ubuntu rootfs. 由于armbian官方的文档比较的匮乏,所以最终还是决定通过其编译的过程来深入地学习. 为了快速度深入地学习 ...