Python使用MySQLdb时的一些问题解决

下午在搞Python连接MySQL的时候,出现一些问题,整理一下:

  • 安装的时候用了pip命令:
    pip install mysql-python时出现错误,错误没有细看,最后只能选择下载exe安装了:mysql-python
  • 连接mysql的时候,
>>>import MySQLdb
>>>conn =MySQLdb.connect(host="localhost",user="root",passwd="12345",db="test")

爆出了以下错误:
Traceback (most recent call last):
File "", line 1, in
File "D:\Python2.7\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "D:\Python2.7\lib\site-packages\MySQLdb\connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")

找了好久才发现,原来是因为hosts文件里这一行:
# 127.0.0.1 localhost
被屏蔽了……解决办法就是把#去掉,或者干脆,写成这样:

>>>import MySQLdb
>>>conn =MySQLdb.connect(host="127.0.0.1",user="root",passwd="12345",db="test")

你可能感兴趣的:(Python)