Introduction
This is an article where the main focus is about how to solve an error message. Actually, the error message exist in the title of the article. Furthermore, this article has a relation with the previous article with the title of ‘How to Solve Error Message UnhandledPromiseRejectionWarning: SequelizeConnectionRefusedError: connect ECONNREFUSED in NodeJS Application’ in this link. The error message exist briefly as follows :
UnhandledPromiseRejectionWarning: SequelizeConnectionError: Access denied for user 'db_user'@'%' to database 'db_name'
The error appear upon executing a NodeJS application. The following is the execution of the NodeJS application along with the output describing the error message :
[admin@10 nodejs]$ node app.js Test NodeJS ! (node:2844) UnhandledPromiseRejectionWarning: SequelizeConnectionError: Access denied for user 'db_user'@'%' to database 'db_master' at ConnectionManager.connect (/home/admin/nodejs/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:126: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:2844) 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:2844) [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.
The following is the actual 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 !");
On the other hand, the file with the name of db has the following content :
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;
Solution for the Problem
Basically, the error message is giving a clear hint for solving the problem. The error message is giving information that the user with the name ‘db_user’ cannot connect to the database with the name of ‘db_master’. It cannot connect because MySQL Database Server denied the access. So, the solution is quite simple. Just give an access for the specific user name of ‘db_user’ to the database with the name of ‘db_master’. The following is the step to perform it :
-
- Access MySQL Database Server exist in the host machine. Just execute the following command :
C:\Users\Personal>mysql -uroot -p Enter password: ********* Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.19 MySQL Community Server - GPL Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | db_master | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 15 rows in set (0.03 sec) mysql>
- After successfully logging in to MySQL Database Server, just execute the following command to grant access :
mysql> grant all on db_master.* to 'db_user'@'%'; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 10 Current database: db_master Query OK, 0 rows affected (0.66 sec) mysql> flush privileges; Query OK, 0 rows affected (0.06 sec) mysql>
- Finally, access back the guest machine and execute the NodeJS application again as follows :
[admin@10 nodejs]$ node app.js Test NodeJS ! Executing (default): SELECT 1+1 AS result
- Access MySQL Database Server exist in the host machine. Just execute the following command :
As in the last step it shows that the error does not appear anymore. It perform a default query of ‘SELECT 1+1 AS result’ and it is actually a success without having error messages appear anymore.