Heroku - ClearDB:连接中止 - 对等方重置连接
Posted
技术标签:
【中文标题】Heroku - ClearDB:连接中止 - 对等方重置连接【英文标题】:Heroku - ClearDB: Connection aborted - Connection reset by peer 【发布时间】:2021-09-01 01:28:51 【问题描述】:我正在使用带有 ClearDB 的 mysql 数据库在 Heroku 上托管 Python 脚本。 我正在使用 MySQL 连接器连接到我的数据库。 我没有任何问题,但有时(大约每天一次,不定期)我的应用程序崩溃并显示以下错误消息:
('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
完整日志:
2021-06-17T22:37:31.736206+00:00 app[worker.1]: Traceback (most recent call last):
2021-06-17T22:37:31.736221+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/urllib3/connectionpool.py", line 699, in urlopen
2021-06-17T22:37:31.736501+00:00 app[worker.1]: httplib_response = self._make_request(
2021-06-17T22:37:31.736504+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/urllib3/connectionpool.py", line 445, in _make_request
2021-06-17T22:37:31.736730+00:00 app[worker.1]: six.raise_from(e, None)
2021-06-17T22:37:31.736730+00:00 app[worker.1]: File "<string>", line 3, in raise_from
2021-06-17T22:37:31.736843+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/urllib3/connectionpool.py", line 440, in _make_request
2021-06-17T22:37:31.737014+00:00 app[worker.1]: httplib_response = conn.getresponse()
2021-06-17T22:37:31.737034+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/http/client.py", line 1345, in getresponse
2021-06-17T22:37:31.737481+00:00 app[worker.1]: response.begin()
2021-06-17T22:37:31.737482+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/http/client.py", line 307, in begin
2021-06-17T22:37:31.737703+00:00 app[worker.1]: version, status, reason = self._read_status()
2021-06-17T22:37:31.737704+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/http/client.py", line 268, in _read_status
2021-06-17T22:37:31.737918+00:00 app[worker.1]: line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
2021-06-17T22:37:31.737919+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/socket.py", line 704, in readinto
2021-06-17T22:37:31.738221+00:00 app[worker.1]: return self._sock.recv_into(b)
2021-06-17T22:37:31.738222+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/ssl.py", line 1241, in recv_into
2021-06-17T22:37:31.738644+00:00 app[worker.1]: return self.read(nbytes, buffer)
2021-06-17T22:37:31.738645+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/ssl.py", line 1099, in read
2021-06-17T22:37:31.739025+00:00 app[worker.1]: return self._sslobj.read(len, buffer)
2021-06-17T22:37:31.739049+00:00 app[worker.1]: ConnectionResetError: [Errno 104] Connection reset by peer
2021-06-17T22:37:31.739050+00:00 app[worker.1]:
2021-06-17T22:37:31.739050+00:00 app[worker.1]: During handling of the above exception, another exception occurred:
2021-06-17T22:37:31.739050+00:00 app[worker.1]:
2021-06-17T22:37:31.739053+00:00 app[worker.1]: Traceback (most recent call last):
2021-06-17T22:37:31.739073+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/requests/adapters.py", line 439, in send
2021-06-17T22:37:31.739291+00:00 app[worker.1]: resp = conn.urlopen(
2021-06-17T22:37:31.739292+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/urllib3/connectionpool.py", line 755, in urlopen
2021-06-17T22:37:31.739602+00:00 app[worker.1]: retries = retries.increment(
2021-06-17T22:37:31.739603+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/urllib3/util/retry.py", line 532, in increment
2021-06-17T22:37:31.739842+00:00 app[worker.1]: raise six.reraise(type(error), error, _stacktrace)
2021-06-17T22:37:31.739846+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/urllib3/packages/six.py", line 734, in reraise
2021-06-17T22:37:31.740119+00:00 app[worker.1]: raise value.with_traceback(tb)
2021-06-17T22:37:31.740120+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/urllib3/connectionpool.py", line 699, in urlopen
2021-06-17T22:37:31.740385+00:00 app[worker.1]: httplib_response = self._make_request(
2021-06-17T22:37:31.740386+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/urllib3/connectionpool.py", line 445, in _make_request
2021-06-17T22:37:31.740589+00:00 app[worker.1]: six.raise_from(e, None)
2021-06-17T22:37:31.740593+00:00 app[worker.1]: File "<string>", line 3, in raise_from
2021-06-17T22:37:31.740708+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/urllib3/connectionpool.py", line 440, in _make_request
2021-06-17T22:37:31.740899+00:00 app[worker.1]: httplib_response = conn.getresponse()
2021-06-17T22:37:31.740903+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/http/client.py", line 1345, in getresponse
2021-06-17T22:37:31.741343+00:00 app[worker.1]: response.begin()
2021-06-17T22:37:31.741343+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/http/client.py", line 307, in begin
2021-06-17T22:37:31.741560+00:00 app[worker.1]: version, status, reason = self._read_status()
2021-06-17T22:37:31.741561+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/http/client.py", line 268, in _read_status
2021-06-17T22:37:31.741774+00:00 app[worker.1]: line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
2021-06-17T22:37:31.741778+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/socket.py", line 704, in readinto
2021-06-17T22:37:31.742070+00:00 app[worker.1]: return self._sock.recv_into(b)
2021-06-17T22:37:31.742070+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/ssl.py", line 1241, in recv_into
2021-06-17T22:37:31.742482+00:00 app[worker.1]: return self.read(nbytes, buffer)
2021-06-17T22:37:31.742483+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/ssl.py", line 1099, in read
2021-06-17T22:37:31.742863+00:00 app[worker.1]: return self._sslobj.read(len, buffer)
2021-06-17T22:37:31.742913+00:00 app[worker.1]: urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
据我所知,这不是我的问题。与许多其他数据库服务一样,ClearDB 会出于某种原因关闭连接(例如非活动连接)。
我已经阅读了其他人关于此错误的解决方案,其中大多数人告诉他们检测断开连接事件并使用连接池而不是简单连接重新创建连接。我将如何使用 MySQL 连接器在 Python 中执行此操作?
【问题讨论】:
分享完整的日志。 我不得不等到错误再次发生才能共享完整日志,我现在添加了它们 你使用哪个库来连接? mysql.connector 【参考方案1】:该错误是由于 CPU 使用过多造成的。似乎该问题可能会导致 100% 的 CPU 使用率,因此当网站上的流量非常高时只会偶尔失败一次。
【讨论】:
以上是关于Heroku - ClearDB:连接中止 - 对等方重置连接的主要内容,如果未能解决你的问题,请参考以下文章
无法在 heroku 上使用 ClearDB 运行 Play Framework 1.2.4 应用程序