M
M
MadHowradBot2021-12-14 17:22:21
MySQL
MadHowradBot, 2021-12-14 17:22:21

Why is mysql2 not working?

Why is mysql2 not working?

const TelegramBot = require('node-telegram-bot-api');
const mysql = require('mysql2');
const token = "";

const connection = mysql.createConnection({
    host: '65.21.149.173',
    user: 'admin_samp',
    password: 'Asn123As123',
    database: 'admin_samp'
});

const bot = new TelegramBot(token, {polling: true});

bot.on('message', (msg) => {
    const chatId = msg.from.id;
    const text = msg.text;
    
    connection.query("SELECT * FROM `users` WHERE chatId = ?",[chatId], function(err,results) {
        if(results == 0){
            connection.query("INSERT INTO `users` SET chatId = ?, username = ?, login = ?, lencm = 0", [chatId, msg.from.first_name + ' ' + msg.from.last_name, msg.from.username]);
            console.log("New user " + msg.from.first_name + ' ' + msg.from.last_name);
            connection.commit();
        } else{
            console.log("m:", text);
        }
    });
});

after some time it gives an error:
Warning: got packets out of order. Expected 5 but received 0
node:events:368
thrower; // Unhandled 'error' event
^

Error: The client was disconnected by the server because of inactivity. See wait_timeout and interactive_timeout for configuring this behavior.
at Connection.protocolError (C:\Users\Mad\Desktop\bot\node_modules\mysql2\lib\connection.js:394:17)
at Connection.handlePacket (C:\Users\Mad\Desktop\bot\node_modules\mysql2\ lib\connection.js:445:14)
at PacketParser.onPacket (C:\Users\Mad\Desktop\bot\node_modules\mysql2\lib\connection.js:85:12)
at PacketParser.executeStart (C:\Users\ Mad\Desktop\bot\node_modules\mysql2\lib\packet_parser.js:75:16)
at Socket. (C:\Users\Mad\Desktop\bot\node_modules\mysql2\lib\connection.js:92:25)
at Socket.emit (node:events:390:28)
at addChunk (node:internal/streams/readable: 315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:199 :23)
Emitted 'error' event on Connection instance at:
at Connection.protocolError (C:\Users\Mad\Desktop\bot\node_modules\mysql2\lib\connection.js:397:10)
at Connection.handlePacket (C: \Users\Mad\Desktop\bot\node_modules\mysql2\lib\connection.js:445:14)
[... lines matching original stack trace ...]
at TCP.onStreamRead (node:internal/stream_base_commons:199:23) {
fatal: true,
code: 4031
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Melkij, 2021-12-14
@melkij

The client was disconnected by the server because of inactivity. See wait_timeout and interactive_timeout for configuring this behavior.

Mmm, quite simple English. Google translate copes with a bang, I even specifically checked.
mysql got tired of waiting for commands from you and closed the connection, which, according to its settings, it considers no one needs.

S
SagePtr, 2021-12-14
@SagePtr

Instead of a separate connection, it is best to create a pool, and pull separate requests from it. Then the mysql2 module will pull a connection from the pool for reuse or create a new one if there are no free old ones, or they fell off by timeout. A very handy thing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question