我的 discord.py 机器人总是失去与 Heroku 上 MySQL 数据库的连接
Posted
技术标签:
【中文标题】我的 discord.py 机器人总是失去与 Heroku 上 MySQL 数据库的连接【英文标题】:My discord.py bot always loses connection to my MySQL database on Heroku 【发布时间】:2021-01-24 01:00:17 【问题描述】:所以,我在 Heroku 上托管的机器人有问题。事实上,我的机器人管理着一个我用 mysql 制作的关卡系统。我的系统似乎运行良好,但仅在启动机器人后几分钟。在此期间,所有命令都在工作。至少 5 分钟后,我的机器人已经启动,与数据库相关的命令不再起作用。写入命令时,日志会这样写:
2020-10-08T21:51:00.344273+00:00 app[worker.1]: Ignoring exception in on_message
2020-10-08T21:51:00.346163+00:00 app[worker.1]: Traceback (most recent call last):
2020-10-08T21:51:00.346212+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/client.py", line 333, in _run_event
2020-10-08T21:51:00.346213+00:00 app[worker.1]: await coro(*args, **kwargs)
2020-10-08T21:51:00.346215+00:00 app[worker.1]: File "pabBot.py", line 96, in on_message
2020-10-08T21:51:00.346217+00:00 app[worker.1]: await upgrade_level(message.author, message)
2020-10-08T21:51:00.346236+00:00 app[worker.1]: File "pabBot.py", line 19, in upgrade_level
2020-10-08T21:51:00.346238+00:00 app[worker.1]: cursor.execute("SELECT exp, lvl, exp_time FROM level_users WHERE id=%s;", (user.id, ))
2020-10-08T21:51:00.346241+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/mysql/connector/cursor.py", line 551, in execute
2020-10-08T21:51:00.346241+00:00 app[worker.1]: self._handle_result(self._connection.cmd_query(stmt))
2020-10-08T21:51:00.346262+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/mysql/connector/connection.py", line 490, in cmd_query
2020-10-08T21:51:00.346263+00:00 app[worker.1]: result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
2020-10-08T21:51:00.346281+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/mysql/connector/connection.py", line 267, in _send_cmd
2020-10-08T21:51:00.346281+00:00 app[worker.1]: return self._socket.recv()
2020-10-08T21:51:00.346300+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/mysql/connector/network.py", line 243, in recv_plain
2020-10-08T21:51:00.346300+00:00 app[worker.1]: raise errors.InterfaceError(errno=2013)
2020-10-08T21:51:00.346325+00:00 app[worker.1]: mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query
2020-10-08T21:51:00.346399+00:00 app[worker.1]: Ignoring exception in command level:
2020-10-08T21:51:00.347478+00:00 app[worker.1]: Traceback (most recent call last):
2020-10-08T21:51:00.347541+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 85, in wrapped
2020-10-08T21:51:00.347541+00:00 app[worker.1]: ret = await coro(*args, **kwargs)
2020-10-08T21:51:00.347557+00:00 app[worker.1]: File "pabBot.py", line 168, in level
2020-10-08T21:51:00.347557+00:00 app[worker.1]: cursor.execute("SELECT exp, lvl FROM level_users WHERE id=%s", (user.id, ))
2020-10-08T21:51:00.347572+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/mysql/connector/cursor.py", line 551, in execute
2020-10-08T21:51:00.347572+00:00 app[worker.1]: self._handle_result(self._connection.cmd_query(stmt))
2020-10-08T21:51:00.347574+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/mysql/connector/connection.py", line 490, in cmd_query
2020-10-08T21:51:00.347574+00:00 app[worker.1]: result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
2020-10-08T21:51:00.347591+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/mysql/connector/connection.py", line 267, in _send_cmd
2020-10-08T21:51:00.347591+00:00 app[worker.1]: return self._socket.recv()
2020-10-08T21:51:00.347594+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/mysql/connector/network.py", line 243, in recv_plain
2020-10-08T21:51:00.347595+00:00 app[worker.1]: raise errors.InterfaceError(errno=2013)
2020-10-08T21:51:00.347622+00:00 app[worker.1]: mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query
2020-10-08T21:51:00.347625+00:00 app[worker.1]:
2020-10-08T21:51:00.347625+00:00 app[worker.1]: The above exception was the direct cause of the following exception:
2020-10-08T21:51:00.347625+00:00 app[worker.1]:
2020-10-08T21:51:00.347641+00:00 app[worker.1]: Traceback (most recent call last):
2020-10-08T21:51:00.347669+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 903, in invoke
2020-10-08T21:51:00.347669+00:00 app[worker.1]: await ctx.command.invoke(ctx)
2020-10-08T21:51:00.347688+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 859, in invoke
2020-10-08T21:51:00.347688+00:00 app[worker.1]: await injected(*ctx.args, **ctx.kwargs)
2020-10-08T21:51:00.347709+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 94, in wrapped
2020-10-08T21:51:00.347709+00:00 app[worker.1]: raise CommandInvokeError(exc) from exc
2020-10-08T21:51:00.347734+00:00 app[worker.1]: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: InterfaceError: 2013: Lost connection to MySQL server during query
这是我的requirements.txt:
discord.py
dnspython==1.16.0
mysql-connector
当然,我注意到错误是:
mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query
我一直在寻找有关此错误的信息,但我并没有真正得到它。 我仍然不知道错误是服务器端错误还是客户端错误,或者是因为我的代码。有关信息,我的数据库托管在 remotemysql.com 上。 另外,我尝试在本地重现该错误,它也有效。所以,我不认为错误来自 Heroku。
我的代码非常大,所以我不认为将其完整发送是一个好主意,但我有一些功能可以让我检查我的等级、其他人的等级并制作排行榜。
如果你们知道我的机器人为什么不能工作以及错误是什么,我会很乐意接受你的信息!另外,如果您需要更多详细信息,我会发送给他们。
(对不起我的英语不好,我是法国人^^')
【问题讨论】:
【参考方案1】:又是我.. 结果我发现我的错误来自哪里,我做了很多测试,才意识到我愚蠢地忘记在命令结束时关闭连接,并在出现错误时重新连接它新的写了...
所以我想为这个完全不必要的问题道歉,并感谢那些看到它并可能寻求帮助我的人。
(如果您有任何其他建议,我很乐意接受!)
再见!
【讨论】:
以上是关于我的 discord.py 机器人总是失去与 Heroku 上 MySQL 数据库的连接的主要内容,如果未能解决你的问题,请参考以下文章