Discord bot开启约12小时后,MySQL会自行关闭 [英] MySQL turns itself off after discord bot left on for about 12 hours

查看:39
本文介绍了Discord bot开启约12小时后,MySQL会自行关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ubuntu VPS上托管了一个discord.js机器人,该机器人使用MySQL作为其数据库.我打开机器人,然后离开它,它仍然可以正常工作,但是大约12个小时后(它可能会有所变化),该机器人由于出现错误而脱机,并说MySQL服务器已断开连接.但是,如果我重新启动bot,则MySQL会重新打开,并且需要数据库的bot部分完全可以使用数据库.我不确定这是否是MySQL,Node.JS或Ubuntu的错误,但我不知道如何解决.谢谢!

I'm hosting a discord.js bot on an ubuntu VPS, and the bot uses MySQL as its database. I turn on the bot, and leave it, and it works fine, but after about 12 hours (it tends to vary), the bot goes offline, because of an error, saying MySQL server disconnected. But, if I restart the bot, MySQL turns back on, and bits of the bot that require the database use the database perfectly fine. I'm not sure if this is an error with MySQL, Node.JS or Ubuntu, but I don't know how to fix it. Thanks!

推荐答案

当我开始使用MySQL时,我遇到了类似的问题.问题是您的机器人在遇到连接超时时崩溃.MySQL服务器不会脱机,只是您与它的连接会脱机.这意味着当您重新启动bot时,它会重新连接并且一切正常.

I had a similar issue when I started using MySQL. The problem is that your bot crashes when it encounters a connection timeout. The MySQL server doesn't go offline, just your connection to it does. Meaning that when you restart your bot it reconnects and everything works.

我遇到了两种方法来解决必须手动重新启动bot的问题.

I encountered two ways of getting around the issue of having to restart the bot manually.

  • 设置您的漫游器,使其在遇到严重错误时自动重新启动.(这并不是最好的选择,但是如果您希望那样做的话,它会起作用)

  • Set up your bot so that it restarts automatically when it encounters a critical error. (Not really the best option but if you want to do it that way it works)

创建连接池.

到目前为止,这是更好的选择.它将创建一个准备就绪的连接池.当您需要连接时,它将为您提供连接,并在完成后将其释放.这样,您的连接就不会超时.

This is by far the better option. It will create a pool of connections ready to go. When you need a connection it will provide you with one and release it after you are done. That way your connection won't time out.

它是这样的:

const mysql = require('mysql');

var db_config = {
 user: 'root',
 password: 'Your password',
 database: 'Your database',
 // etc
};

var con = mysql.createPool(db_config);

con.getConnection(function(err, con) {
 if (err) {
  connection.release();
  console.log(' Error getting mysql_pool connection: ' + err);
  throw err;
 }
 console.log('Connected to Database');
});

这篇关于Discord bot开启约12小时后,MySQL会自行关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆