PyMySQL:- OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] C

Posted

技术标签:

【中文标题】PyMySQL:- OperationalError: (2003, "Can\'t connect to MySQL server on \'localhost\' ([Errno 111] Connection denied)")【英文标题】:PyMySQL:- OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")PyMySQL:- OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection denied)") 【发布时间】:2021-12-16 22:37:45 【问题描述】:

我正在尝试运行一个爬虫蜘蛛,每当蜘蛛打开/爬取/关闭时,我都会将数据保存到 pymysql 数据库中。当我在 RDS 上运行时,它没有给出任何类型的错误,但是当我切换到我的本地主机时,它给了我一个“连接被拒绝错误”。

这是我的 runner.py 文件的代码

import pymysql
connection = pymysql.connect(
    host='127.0.0.1',
    port='3306',
    user='root',
    password='lifeisgood',
    db='scrapy_dev'
)
# rest of code

这是错误的完整堆栈跟踪:

Traceback (most recent call last):
  File "/home/technophile/.local/lib/python3.8/site-packages/pymysql/connections.py", line 581, in connect
    sock = socket.create_connection(
  File "/usr/lib/python3.8/socket.py", line 808, in create_connection
    raise err
  File "/usr/lib/python3.8/socket.py", line 796, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "runner.py", line 23, in <module>
    connection = pymysql.connect(
  File "/home/technophile/.local/lib/python3.8/site-packages/pymysql/__init__.py", line 94, in Connect
    return Connection(*args, **kwargs)
  File "/home/technophile/.local/lib/python3.8/site-packages/pymysql/connections.py", line 325, in __init__
    self.connect()
  File "/home/technophile/.local/lib/python3.8/site-packages/pymysql/connections.py", line 630, in connect
    raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")

我已经通过查看其他 *** 答案尝试了一些方法,但到目前为止它们都没有帮助。 请帮帮我。

【问题讨论】:

【参考方案1】:

仔细查看配置。应该是配置问题。请检查远程代码是否同步!

【讨论】:

非常感谢 Arun 的及时回复,但是在我的 settings.py 和 runner.py 中一切似乎都很好。你说的除了这些还有哪些配置?

以上是关于PyMySQL:- OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] C的主要内容,如果未能解决你的问题,请参考以下文章

pymysql 模块简单使用

python安装PyMySQL

PyMySQL

运行 tkinter+pymysql 脚本时出现 pymysql.err.ProgrammingError

PyMySQL的基本操作

PyMysql