英文:
MySQL connection drops stable once a day
问题
我有一个基于Node.js和MySQL的Discord机器人。连接每天稳定地掉线并显示错误。在此之后的任何连接都会出现类似以下内容的错误:
(/root/testbot/node_modules/mysql2/lib/connection.js:153:11)
at PoolConnection.query (/root/testbot/node_modules/mysql2/lib/connection.js:546:17)
at Client.<anonymous> (/root/testbot/events/message.js:21:16)
at Client.emit (node:events:513:28)
at MessageCreateAction.handle (/root/testbot/node_modules/discord.js/src/client/actions/MessageCreate.js:28:14)
at Object.module.exports [as MESSAGE_CREATE] (/root/testbot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/root/testbot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:352:31)
at WebSocketShard.onPacket (/root/testbot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:494:22)
at WebSocketShard.onMessage (/root/testbot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:328:10)
在JavaScript文件中的MySQL连接如下:
const mysql = require('mysql2')
const pool = mysql.createPool({
host: "IP",
user: "userName",
port: "3306",
database: "dataBase",
password: "password",
waitForConnections: true,
connectionLimit: 0,
queueLimit: 0
})
module.exports = {
pool
}
例如,message.js中的代码如下:
const { pool } = require("../mysql.js")
pool.getConnection(function (err, connection) {
if (err) {
console.log(err)
}
let sqlChangeCoin = `UPDATE members SET coin = coin + ${coinResult} WHERE id = '${message.author.id}' AND guildId = '${message.guildId}'`
connection.query(sqlChangeCoin, [], async function (err) {
connection.release()
// 我的代码
if (err) {
console.log(err)
}
});
if (err) {
console.log(err)
}
});
可能的错误原因和MySQL请求失败是什么?
英文:
I have a discord bot on node js and MySQL. Сonnection drops once a day stably with errors. On any connection after that I get something like this
(/root/testbot/node_modules/mysql2/lib/connection.js:153:11)
at PoolConnection.query (/root/testbot/node_modules/mysql2/lib/connection.js:546:17)
at Client.<anonymous> (/root/testbot/events/message.js:21:16)
at Client.emit (node:events:513:28)
at MessageCreateAction.handle (/root/testbot/node_modules/discord.js/src/client/actions/MessageCreate.js:28:14)
at Object.module.exports [as MESSAGE_CREATE] (/root/testbot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/root/testbot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:352:31)
at WebSocketShard.onPacket (/root/testbot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:494:22)
at WebSocketShard.onMessage (/root/testbot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:328:10)
MySQL connection in js file
const mysql = require('mysql2')
const pool = mysql.createPool({
host: "IP",
user: "userName",
port: "3306",
database: "dataBase",
password: "password",
waitForConnections: true,
connectionLimit: 0,
queueLimit: 0
})
module.exports = {
pool
}
For example message.js
const {
pool
} = require("../mysql.js")
pool.getConnection(function (err, connection) {
if (err) {
console.log(err)
}
let sqlChangeCoin = `UPDATE members SET coin = coin + ${coinResult} WHERE id = '${message.author.id}' AND guildId = '${message.guildId}'`
connection.query(sqlChangeCoin, [], async function (err) {
connection.release()
//my code
if (err) {
console.log(err)
}
});
if (err) {
console.log(err)
}
});
what could be the error and fall of MySQL enter code here requests?
答案1
得分: 1
你可以将这段代码添加到你的主 Node.js 文件中:
setInterval(function () {
connection.query('SELECT 1');
}, 5000);
它会保持你的连接活跃。如果需要,可以随意更改超时时间(5000毫秒)为其他值。
英文:
You can add this into your main nodejs file:
setInterval(function () {
connection.query('SELECT 1');
}, 5000);
It will keep your connection alive. Feel free to change the Timeout (5000 milliseconds) to some other value if needed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论