I have read through many similar questions such as node.js mysql error: ECONNREFUSED, but none of their solutions have solved my problem. For example, I ran mysql -hlocalhost -P 3306 -p***
on the command line and it properly connected to my database. It seems that the problem is this specific script. I have another script with the exact same dbconnection.js
file and it is connecting just fine, like the command line is.
dbconnection.js
var mysql = require('mysql');
var connection = mysql.createPool ({
host: '127.0.0.1',
user: 'root',
password: ****,
database: 'test_db',
dateStrings: true,
port: 3306
});
module.exports = connection;
And this works in one program, but not another. The problem area of the problem script uses the following code:
Notifications.js
var db = require('../dbconnection');
var express = require('express');
var router = express.Router();
router.post('/', function(req,res,next) {
handleNotification(req.body.value[0].resource);
res.status(202).end();
});
async function handleNotification(resource) {
db.query('SELECT of.name, po.PurchaseOrderID FROM order_files AS of \
INNER JOIN purchase_orders AS po ON po.PurchaseOrderID = of.purchaseOrderID \
INNER JOIN quotes ON po.PurchaseOrderID = quotes.PurchaseOrderID \
WHERE quotes.QuoteID=10000', function(err, rows) {
if (err) console.log(err);
else console.log(rows);
});
}
Yet I can run from bash, mysql -hlocalhost -P 3306 -p****
and then run the exact query shown and get the results. I can also run this query from another script.
The error received is as follows:
Error: connect ECONNREFUSED 127.0.0.1:3306
Is there a reason why this specific script would not be able to connect to the database?