【问题标题】:Python MySQLdb connecting errorPython MySQLdb连接错误
【发布时间】:2012-11-04 10:36:55
【问题描述】:

我想将我的数据从 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)")

db=MySQLdb.connect(passwd="rat123",db="thangs",host="localhost",port=1028,user="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")

【问题讨论】:

  • 你的mysql数据库是在同一台电脑的10061端口上运行的吗?
  • 没有,但是也安装了mysql php admin

标签: python mysql-python


【解决方案1】:

您需要指定server and point

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

【讨论】:

  • OperationalError: (2005, "Unknown MySQL server host 'hostname' (0)") 它给出以下错误
  • 嗯,你必须改变它以匹配你想使用的服务器...获取你的数据库的信息。我不能告诉你那是什么......
  • 我如何获取我的数据库信息?
【解决方案2】:

我有同样的问题,对我来说,问题在于 mysql 监听端口 3305 的 IP 地址。 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,问题就解决了!

【讨论】:

    猜你喜欢
    • 2014-01-01
    • 2016-06-08
    • 2018-05-23
    • 2011-03-11
    • 1970-01-01
    • 2015-12-27
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多