Introduction
This article has a relation with several other previous article. But the main point is that this article context is focusing on the MySQL database connection. The process for connecting to the MySQL database from the NodeJS application actually fail. Failing on to connect to MySQL database exist on the following short description :
UnhandledPromiseRejectionWarning: SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306
Furthermore, the following is the full output of the error message upon the NodeJS application execution :
[admin@10 nodejs]$ node app.js Test NodeJS ! (node:2759) UnhandledPromiseRejectionWarning: SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306 at ConnectionManager.connect (/home/admin/nodejs/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:116:17) at processTicksAndRejections (internal/process/task_queues.js:93:5) at async ConnectionManager._connect (/home/admin/nodejs/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:318:24) at async /home/admin/nodejs/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:250:32 at async ConnectionManager.getConnection (/home/admin/nodejs/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:280:7) at async /home/admin/nodejs/node_modules/sequelize/lib/sequelize.js:613:26 at async Sequelize.authenticate (/home/admin/nodejs/node_modules/sequelize/lib/sequelize.js:867:5) at async Sequelize.sync (/home/admin/nodejs/node_modules/sequelize/lib/sequelize.js:791:7) (Use `node --trace-warnings ...` to show where the warning was created) (node:2759) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:2759) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Before going further to the solution, the following is the structure of the root folder of the NodeJS application :
[admin@10 nodejs]$ ls -al total 148 drwxrwxr-x 4 admin admin 99 Mar 4 21:36 . drwx------. 6 admin admin 199 Mar 4 21:37 .. -rw-rw-r-- 1 admin admin 268 Mar 4 17:58 app.js drwxrwxr-x 2 admin admin 19 Mar 4 21:37 config drwxrwxr-x 180 admin admin 8192 Mar 4 18:00 node_modules -rw-rw-r-- 1 admin admin 129 Mar 4 18:00 package.json -rw-rw-r-- 1 admin admin 128532 Mar 4 18:00 package-lock.json [admin@10 nodejs]$
Actually, the following is the content of the app.js file :
const express = require("express"); const app = express(); const db = require("./config/db"); app.get("/",(req,res) => res.send("Hello World !")); app.listen(3001); console.debug("Test NodeJS !");
Furthermore, the following is the content of the file with the name ‘db.js’ in the folder with the name of ‘config’ :
const sequelize = require("sequelize") const db = new sequelize("db_master","db_user","password", { dialect: "mysql" }); db.sync({}); module.exports = db;
Solution to the Problem
After searching and digging up through several googling attempt, the culprit of the problem soon appear. In this context, the database server is not in the same machine. In other words, the MySQL Database Server address is not localhost. So, how can the above MySQL database configuration by default is pointing to localhost address. Well, it seems that if there is no further definition of the host address, it will connect to the localhost address by default. Read the article for further reference about sequelize syntax pattern in this link.
So, in order to solve the problem, just modify the file with the right configuration database. The following is the correction of the configuration database :
const sequelize = require("sequelize") const db = new sequelize("db_master","db_user","password", { host : "10.0.2.2", dialect: "mysql" }); db.sync({}); module.exports = db;
Actually, the NodeJS application is running in a virtual server. It is a guest machine run in a VirtualBox application. On the other hand, MySQL Database server exist outside the guest machine. It is available in the host machine where the VirtualBox application is running. The host machine IP address is 10.0.2.2. So, in order to connect to MySQL Database Server in the host machine, the IP address of the host is 10.0.2.2.