简体   繁体   中英

Node Js mysql(and mysql2) ECONNRESET

i am currently trying to connect to a MySQL server on the internet using Node.Js with the mysql or the mysql2 NPM dependencies to use queries and other related stuff.

the code is simple...

//i import my dependency
const mysql = require('mysql2') //either 'mysql' or 'mysql2'

//i create my pool to create connections as needed
var conn = mysql.createPool({
    host: 'some_database_i_have_access_to.mysql.uhserver.com',
    user: 'valid_user',
    password: 'valid_password',
    database: 'some_existing_database'
})

//i try to connect (this is the part where it fails)
conn.getConnection((err,conn) => {
    if (err) throw err //<- the error is thrown here

    //i do query stuff
    conn.query("SELECT * FROM atable",(err,res,firlds) => {
        if(err) throw err
        console.log(JSON.stringify(res))
    })
    //i close the connection
    conn.end()
})

yet i always get an Error like this:

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
    --------------------
    at Protocol._enqueue (C:\Users\Aluno\Desktop\my-project\node_modules\mysql\lib\protocol\Protocol.js:144:48)
    at Protocol.handshake (C:\Users\Aluno\Desktop\my-project\node_modules\mysql\lib\protocol\Protocol.js:51:23)
    at Connection.connect (C:\Users\Aluno\Desktop\my-project\node_modules\mysql\lib\Connection.js:118:18)
    at Object.<anonymous> (C:\Users\Aluno\Desktop\my-project\private\dtp-mysql.js:13:6)
    at Module._compile (internal/modules/cjs/loader.js:707:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
    at Module.load (internal/modules/cjs/loader.js:605:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
    at Function.Module._load (internal/modules/cjs/loader.js:536:3)
    at Module.require (internal/modules/cjs/loader.js:643:17)

all i know about the error is that the connection abruptly closes in one of the sides as stated in this question (Node js ECONNRESET ), but nothing more, and creating singular connections does not solve this issue for me either.

any fixes to that?

Hi I know this was asked some time ago, but is it possibly because you're using:

conn.end()

Since you're using a Pooled connection, I think you can release the connection using

conn.release()

Or

conn.destroy()

you can also ref below url. error while inserting LARGE volume data in mysql by using node.js (error code: 'ECONNRESET')

I have fixed this issue. It is caused by the default definition max_allowed_packet. Find max_allowed_packet in my.ini (C:\\ProgramData\\MySQL\\MySQL Server 5.7). Update to 'max_allowed_packet=64M'. Restart mysql. Done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM