1

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?

5
  • Did you check in the azure panel if the server is running? Commented Nov 3, 2019 at 22:12
  • @nbk yes, the server is running Commented Nov 3, 2019 at 22:22
  • Try it with mysql workbench and see if it can get a connection. If not see if there is an error learn.microsoft.com/en-us/azure/azure-monitor/app/… maybe a restart helps. Commented Nov 3, 2019 at 22:36
  • Tried it with the workbench, tried restarting the database, no luck so far.. Commented Nov 3, 2019 at 22:57
  • contact the support.I am wondering why nothing shows up in the logs. Commented Nov 3, 2019 at 23:11

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It seems the createPool solved the problem. Thank you for your help and sorry for the late reply!
Glad to know it helped!!
@MohitVerma can you explain how this works? and why that error happens?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.