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

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

问题描述

我在 ubuntu VPS 上托管了一个 discord.js 机器人,该机器人使用 MySQL 作为其数据库.我打开机器人,然后离开它,它工作正常,但大约 12 小时后(它往往会有所不同),机器人因为错误而离线,说 MySQL 服务器已断开连接.但是,如果我重新启动机器人,MySQL 会重新打开,并且需要数据库的机器人部分可以完美地使用数据库.我不确定这是否是 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 服务器不会离线,只是您与它的连接会.这意味着当您重新启动机器人时,它会重新连接并且一切正常.

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.

我遇到了两种解决手动重启机器人问题的方法.

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天全站免登陆