Hi,
I have an error like below. while i initiate my server it was working smoothly after the certain period of time it has been raised.for connectivity i used the file like below. for resolve this issue i googled and tried so many suggestions no one it works fine. when i restart my server then it was
fine for another certain period of time. for deployment here am using appache for reverse proxy and pm2 for load balancing.
nodemysql connectivity
const mysql = require("mysql");
const logger = require("../log4js");
const dbConfig = {
connectionLimit: 10,
host: 'localhost',
user: 'root',
password: 'test',
database: 'test'
};
let pool = mysql.createPool(dbConfig);
// Ping database to check for common exception errors.
pool.getConnection((err, connection) => {
if (err) {
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
console.error('Database connection was closed.')
}
if (err.code === 'ER_CON_COUNT_ERROR') {
console.error('Database has too many connections.')
}
if (err.code === 'ECONNREFUSED') {
console.error('Database connection was refused.')
}
}
if (connection){
connection.release();
return;
}
})
module.exports = pool
database.js
const pool = require('./dbconfig');
const logger = require('../log4js');
function executeQuery(sql, callback) {
pool.query(sql, function (error, results, fields) {
logger('INFO',sql);
if (error) {
logger('INFO',error);
return callback(error, null);
}else{
logger('INFO',results);
return callback(null, results);
}
});
}
function query(sql, callback) {
executeQuery(sql,function(err, data) {
if(err) {
return callback(err,null);
}else{
return callback(null, data);
}
});
}
module.exports = {
query: query
}
sample issue
[2019-09-13T11:14:02.078] [INFO] bussiness_logic - select id, name, username, password, allowedoperator, status from user where id = "1" and status = "ACTIVE"
[2019-09-13T11:14:02.078] [INFO] bussiness_logic - { Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
--------------------
at Protocol._enqueue (/var/www/fms/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/var/www/fms/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at PoolConnection.connect (/var/www/fms/node_modules/mysql/lib/Connection.js:119:18)
at Pool.getConnection (/var/www/fms/node_modules/mysql/lib/Pool.js:48:16)
at Pool.query (/var/www/fms/node_modules/mysql/lib/Pool.js:202:8)
at executeQuery (/var/www/fms/database/database.js:5:10)
at Object.query (/var/www/fms/database/database.js:18:5)
at UserProfile.FnGetUserProfile (/var/www/fms/classes/userprofile.js:13:25)
at Object.<anonymous> (/var/www/fms/cron/test.js:15:13)
at Module._compile (internal/modules/cjs/loader.js:778:30)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true }
The error ECONNREFUSED 127.0.0.1:3306 is being raised by Node.js itself, and not this module. This module passes the error through, though, from Node.js. Basically, this module is asking Node.js to create a TCP connection to 127.0.0.1, port 3306, but for whatever reason, Node.js cannot do this and raises the ECONNREFUSED error.
Unfortunately there is nothing this module can do to resolve this.
It could be an issue with Node.js (in which you can open on the Node.js issue tracker) or an issue with your MySQL server (did it restart or something?). If there is some specific things you can list to change in this module, we can re-open the issue, though.
Most helpful comment
The error
ECONNREFUSED 127.0.0.1:3306is being raised by Node.js itself, and not this module. This module passes the error through, though, from Node.js. Basically, this module is asking Node.js to create a TCP connection to127.0.0.1, port3306, but for whatever reason, Node.js cannot do this and raises theECONNREFUSEDerror.Unfortunately there is nothing this module can do to resolve this.
It could be an issue with Node.js (in which you can open on the Node.js issue tracker) or an issue with your MySQL server (did it restart or something?). If there is some specific things you can list to change in this module, we can re-open the issue, though.