MySQL连接每天稳定掉线一次。

huangapple go评论58阅读模式
英文:

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.&lt;anonymous&gt; (/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(&#39;mysql2&#39;)

const pool = mysql.createPool({
    host: &quot;IP&quot;,
    user: &quot;userName&quot;,
    port: &quot;3306&quot;,
    database: &quot;dataBase&quot;,
    password: &quot;password&quot;,
    waitForConnections: true,
    connectionLimit: 0,
    queueLimit: 0
})


module.exports = {
    pool
}

For example message.js

     const {
  pool
} = require(&quot;../mysql.js&quot;)
pool.getConnection(function (err, connection) {
  if (err) {
    console.log(err)
  }
  let sqlChangeCoin = `UPDATE members SET coin = coin + ${coinResult} WHERE id = &#39;${message.author.id}&#39; AND guildId = &#39;${message.guildId}&#39;`
  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(&#39;SELECT 1&#39;); 
}, 5000);

It will keep your connection alive. Feel free to change the Timeout (5000 milliseconds) to some other value if needed.

huangapple
  • 本文由 发表于 2023年6月5日 16:23:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定