Python MySQLdb 连接错误

标签 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)")

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")

最佳答案

您需要指定 server and point .

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

关于Python MySQLdb 连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13404119/

相关文章:

python - 属性错误 : module 'MySQLdb.constants.FIELD_TYPE' has no attribute 'JSON' while migrating in Django

python - easyprocess.EasyProcessCheckInstalledError : cmd= ['Xvfb' , '-help' ] OSError=[Errno 2] 没有这样的文件或目录

python - 如何使用python语句检查sql表是否存在?

python - Flask mySQLDB 执行成功还是失败

python - 如何用python自动化shell输入?

python - 字符串插值导致 MysqlDB 错误

python - 如何在 Tornado 中为具有高度可扩展基础架构(可进行大量数据库查询)的应用程序进行 MySQL 数据库调用?

python - python 中的正交距离回归 : meaning of returned values

python - 如何在 VS Code 中调试第三方 Python 包

python - 扩展在范围外声明的 python 数组的问题