-2

i have created express app with mysql database. when i call mysql query it works fine and then i call my view. loading view take few minute(2-3 min) and app crashed with bellow error.

events.js:287 throw er; // Unhandled 'error' event ^

Error: read ECONNRESET at TCP.onStreamRead (internal/stream_base_commons.js:205:27) Emitted 'error' event on Connection instance at: at Connection._handleProtocolError (C:\Users\AMW\Desktop\dishmize\dishmize\node_modules\mysql\lib\Connection.js:423:8) at Protocol.emit (events.js:310:20) at Protocol.EventEmitter.emit (domain.js:482:12) at Protocol._delegateError (C:\Users\AMW\Desktop\dishmize\dishmize\node_modules\mysql\lib\protocol\Protocol.js:398:10) at Protocol.handleNetworkError (C:\Users\AMW\Desktop\dishmize\dishmize\node_modules\mysql\lib\protocol\Protocol.js:371:10) at Connection._handleNetworkError (C:\Users\AMW\Desktop\dishmize\dishmize\node_modules\mysql\lib\Connection.js:418:18) at Socket.emit (events.js:310:20) at Socket.EventEmitter.emit (domain.js:482:12) at emitErrorNT (internal/streams/destroy.js:92:8) at emitErrorAndCloseNT (internal/streams/destroy.js:60:3) { errno: 'ECONNRESET', code: 'ECONNRESET', syscall: 'read', fatal: true } [nodemon] app crashed - waiting for file changes before starting...

i already spend 8- 10 hours. please help me to resolve this issue. thanks

Update:

const options = { 
    user: config.get('MYSQL_USER'), 
    password: config.get('MYSQL_PASSWORD'), 
    database:config.get('DATABASE'), 
    host: config.get('HOST'), 
    port: 3306 
} 

const connection = mysql.createConnection(options); 
connection.connect( function (err) { 
    if (err) { 
        console.log("!!! Cannot connect !!! Error:"); throw err; 
    } else { 
        console.log("Connection established."); 
    } 
});
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • create a connection pool instead of a single connection. – Satish Gupta May 04 '20 at 09:15
  • and please share your MySQL connection code snippet. – Satish Gupta May 04 '20 at 09:16
  • const options = { user:config.get('MYSQL_USER'), password: config.get('MYSQL_PASSWORD'), database:config.get('DATABASE'), host: config.get('HOST'), port: 3306 } const connection = mysql.createConnection(options); connection.connect( function (err) { if (err) { console.log("!!! Cannot connect !!! Error:"); throw err; } else { console.log("Connection established."); } }); – Jagdish Pal May 04 '20 at 09:23

1 Answers1

1

use below

const options = { connectionLimit :10, user:config.get('MYSQL_USER'), password: config.get('MYSQL_PASSWORD'), database:config.get('DATABASE'), host: config.get('HOST'), port: 3306 } 
const connection_pool = mysql.createPool(options); 

connection_pool.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
  if (error) throw error;
  console.log('The solution is: ', results[0].solution);
});

provide connectionLimit according to you use this is pool size of connection

Satish Gupta
  • 333
  • 1
  • 12