简体   繁体   中英

Error: read ECONNRESET when connected to a mysql server with Node.js

I'm trying to establish a simple connection to my database with the mysql npm package. At first glance, everything works fine and I can get the information I need, however, if I leave the server running for some time I get the following error:
Error: read ECONNRESET at TCP.onStreamRead

const express = require('express');
const app = express();`

const mysql = require('mysql');

 const db = mysql.createConnection({
  host: 'XXXX.mysql.database.azure.com',
  user: 'XXXXX',
 password: 'XXXXX',
 database: 'XXXXX'

})
    db.connect((err)=>{
    if(err){
       console.log(err.message);
    } else {
        console.log('Connected to the database');
    }
})

As far as I understand the problem stems from the database connection being in idle mode. Do I need to configure the Azure server or is there something else I need to do?

Couple of things to try:

  • You can try creating connection pool instead of **createConnection**

    mysql.createPool({});

  • Modify your package.json like below:

 "dependencies": { "mysql": "git://github.com/mysqljs/mysql#e3e123e9af7c0829a6c1417d911572a75b4a5f95" },

It is described in detail here:

Bad handshake or ECONNRESET Azure Mysql Nodejs

https://social.msdn.microsoft.com/Forums/en-US/c8fedbcc-909d-41ce-8c72-0374f76fdf82/cannot-connect-from-nodejs?forum=AzureDatabaseforMySQL

Hope it helps.

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