Python MySQLdb连接错误

时间:2012-11-15 18:47:36

标签: python mysql-python

我想将我的数据从json传输到mysql,因为我安装了python库MySQLdb。它导入成功但连接时出错

>>> import MySQLdb
>>> db=MySQLdb.connect(passwd="king123",db="thangs")

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    db=MySQLdb.connect(passwd="king123",db="thangs")
  File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")

>>> import _mysql
>>> db=_mysql.connect()

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    db=_mysql.connect()
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")
  
    
      

分贝= MySQLdb.connect(passwd中= “rat123”,分贝= “thangs”,主机= “本地主机”,端口= 1028,用户= “mroot”)

    
  
Traceback (most recent call last):
  File "<pyshell#41>", line 1, in <module>
db=MySQLdb.connect(passwd="king123",db="thangs",host="localhost",port=1028,user="munieb")


File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2013, "Lost connection to MySQL server at 'waiting for initial communication packet', system error: 2")

2 个答案:

答案 0 :(得分:1)

您需要指定server and point

db=MySQLdb.connect(passwd="king123",db="thangs",host="hostname",port=3251,user="user")

答案 1 :(得分:1)

我有同样的问题,对我来说,问题是使用ip地址,mysql监听端口3305。到netstat -aon我看到了:

Proto  Local Address          Foreign Address        State           PID
...
TCP    0.0.0.0:3306            0.0.0.0:0              LISTENING       884
...

所以我在my.ini配置文件中取消注释下面的行:

#bind-address="127.0.0.1" 

然后重新启动mysql并且问题消失了!