python MySQLdb连接mysql时报错
故障现象:
>>> 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时报错的更多相关文章
- 【转载】在使用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 ...
- 解决使用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 ...
- python MySQLdb连接mysql失败(转载)
最近了解了一下django,数据库选用了mysql, 在连接数据库的过程中,遇到一点小问题,在这里记录一下,希望能够对遇到同样的问题的朋友有所帮助,少走一些弯路.关于django,想在这里也额外说一句 ...
- python -- MySQLdb连接mysql数据库
1. python安装mysql $ pip install mysql-python 2. 数据库连接程序: import MySQLdb # 打开数据库连接db = MySQLdb.connect ...
- 使用navicat连接mysql时报错:2059 - authentication plugin 'caching_sha2_password'
首先从本地登录mysql数据库,进入mysql控制台,输入如下命令: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_passwo ...
- Ubuntu下使用mysqli-connect连接mysql时报错:ERROR 1698 (28000): Access denied for user 'root'@'localhost'
LNMP安装好后,写了个index.php文件,里面的内容很简单,就是想测试php与mysql的通信是否正常,代码如下: <?php $host = 'localhost'; $user = ' ...
- Navicat Premium 12连接MySQL时报错2059和1045的解决办法
参考连接:https://www.jianshu.com/p/15876ad165f5 https://jingyan.baidu.com/article/c275f6ba479ca9e33d7567 ...
- Nodejs 连接 mysql时报错 Error: Cannot enqueue Query after fatal error
解决办法,参考:https://github.com/chill117/express-mysql-session/issues/18 我们只需在实例化SessionStore的时候,配置useCon ...
- 【small项目】MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link ...
随机推荐
- ELK 错误: retrying failed action with response code: 403, kibana无log显示
今天10点时候同事报出kibana突然不显示log了,开始紧急排查 1. 从数据源头查起,先看被filebeat监视的log文件是否在更新(一般只要log对应服务在正常运行,log文件中就会有数据持续 ...
- Computer Vision_1_Active Appearance Models:Active Appearance Models——2001
此为计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面. 1. Active Appearance Models 活动表观模型和活动轮廓模型基本思想来源 Snake,现在 ...
- 【异常】 Ensure that config phoenix.schema.isNamespaceMappingEnabled is consistent on client and server.
1 详细异常 ror: ERROR 726 (43M10): Inconsistent namespace mapping properties. Ensure that config phoenix ...
- namespace" 或The content of element type "mapper" must match "EMPTY"
必须为元素类型 "mapper" 声明属性 "namespace" 或The content of element type "mapper" ...
- 【实用linux命令记录】
1.显示完整的进程命令 cat /proc/29049/cmdline 如上面显示不完整可用当前的 cat /proc/进程号/cmdline显示 vtysh下显示完整的配置
- CentOS7 基于 subversion 配置 SVN server
由于 Window Server 环境下,VisualSVN Server Community 版本只支持 15 个同时在线用户,所以彻底放弃 Windows Server,在 Linux Serve ...
- 单元测试框架之unittest(一)
一.单元测试的含义 unittest单元测试框架的设计灵感来源于Junit(Java语言的单元测试框架),它与其他语言的单元测试框架风格相类似,支持自动化测试.为测试共享setUp和shutDown. ...
- bind9+dlz+mysql连接断开问题
前言 关于bind-dlz介绍:http://bind-dlz.sourceforge.net/ DLZ(Dynamically Loadable Zones)与传统的BIND9不同,BIND的不足之 ...
- FFmpeg学习笔记之安装
本随笔原文出自:一叶知秋0830链接:https://www.jianshu.com/p/ab469a2ffd28 1.下载FFmpeg 先进入要存放下载文件的目录,比如要放在/Users/qinji ...
- CSS3 -- 边框圆角
文章后有彩蛋哦 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...