MySQL connection error with NodeJS on Heroku (works in Dev) - mysql

My application works correctly in Localhost with following connection details:
const mysql = require("mysql");
var mysqlConnection = mysql.createConnection({
host: "gatorxxxx.hostgator.com",
user: "ermaulik_chdb",
password: "<password>",
database: "ermaulik_chdb",
port: <port>
})
mysqlConnection.connect((err) => {
if(!err) {
console.log("Connection")
} else {
console.log(err)
}
});
But, it doesn't work when I host this application on Heroku and throws following exception.
Just to give more context, my database in hosted in cpanel of hostgator.
2020-05-23T16:47:32.542824+00:00 app[web.1]: server is running on port: 54806
2020-05-23T16:47:32.812651+00:00 app[web.1]: Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'ermaulik_chdb'#'ec2-3-84-45-11.compute-1.amazonaws.com' (using password: YES)
2020-05-23T16:47:32.812658+00:00 app[web.1]: at Handshake.Sequence._packetToError (/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
2020-05-23T16:47:32.812659+00:00 app[web.1]: at Handshake.ErrorPacket (/app/node_modules/mysql/lib/protocol/sequences/Handshake.js:123:18)
2020-05-23T16:47:32.812659+00:00 app[web.1]: at Protocol._parsePacket (/app/node_modules/mysql/lib/protocol/Protocol.js:291:23)
2020-05-23T16:47:32.812660+00:00 app[web.1]: at Parser._parsePacket (/app/node_modules/mysql/lib/protocol/Parser.js:433:10)
2020-05-23T16:47:32.812661+00:00 app[web.1]: at Parser.write (/app/node_modules/mysql/lib/protocol/Parser.js:43:10)
2020-05-23T16:47:32.812661+00:00 app[web.1]: at Protocol.write (/app/node_modules/mysql/lib/protocol/Protocol.js:38:16)
2020-05-23T16:47:32.812662+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:88:28)
2020-05-23T16:47:32.812662+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:526:10)
2020-05-23T16:47:32.812662+00:00 app[web.1]: at Socket.emit (events.js:310:20)
2020-05-23T16:47:32.812663+00:00 app[web.1]: at addChunk (_stream_readable.js:286:12)
2020-05-23T16:47:32.812663+00:00 app[web.1]: --------------------
2020-05-23T16:47:32.812664+00:00 app[web.1]: at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
2020-05-23T16:47:32.812664+00:00 app[web.1]: at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
2020-05-23T16:47:32.812665+00:00 app[web.1]: at Connection.connect (/app/node_modules/mysql/lib/Connection.js:116:18)
2020-05-23T16:47:32.812665+00:00 app[web.1]: at Object.<anonymous> (/app/connection.js:11:17)
2020-05-23T16:47:32.812666+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1133:30)
2020-05-23T16:47:32.812666+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
2020-05-23T16:47:32.812666+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:977:32)
2020-05-23T16:47:32.812667+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:877:14)
2020-05-23T16:47:32.812667+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:1019:19)
2020-05-23T16:47:32.812667+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:77:18) {
2020-05-23T16:47:32.812668+00:00 app[web.1]: code: 'ER_ACCESS_DENIED_ERROR',
2020-05-23T16:47:32.812668+00:00 app[web.1]: errno: 1045,
2020-05-23T16:47:32.812669+00:00 app[web.1]: sqlMessage: "Access denied for user 'ermaulik_chdb'#'ec2-3-84-45-11.compute-1.amazonaws.com' (using password: YES)",
2020-05-23T16:47:32.812669+00:00 app[web.1]: sqlState: '28000',
2020-05-23T16:47:32.812669+00:00 app[web.1]: fatal: true
2020-05-23T16:47:32.812670+00:00 app[web.1]: }

Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'ermaulik_chdb'#'ec2-3-84-45-11.compute-1.amazonaws.com'
you got an Error. because your MySQL database connection is Not Established.
Firstly, please check the Installed LAMP, Nodejs Acc. to your Code version supported. in the Heroku server. properly.
In the second step, check the MYSQL DB GRANT PRIVILEGES.
SHOW GRANTS FOR 'root'#'localhost';
the Third one is to Make sure your Nodejs Code & MySql DB
in the same server. If not you need to allow permission a server to connect OutSide the server.
and make sure the host, username, password, HOST all things are correct. I hope these things help you resolve a problem. thanks :)

Related

Having issues with Node JS MySQL connection

I'm creating a discord bot that pulls from a pre-established database of patch note news I have, it's my first time dealing with NodeJS and MySQL. I've got the default connection code in the file right now to test the connection and it looks like this:
require('dotenv').config();
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
const mysql = require('mysql');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const con = mysql.createConnection({
host: process.env.HOST,
port: process.env.PORT,
user: process.env.MYSQLUSER,
db: process.env.DATABSE,
password: process.env.MYSQLPASSWORD,
});
con.connect((err) => {
if (err) throw err;
console.log("Connected");
})
I've tried hosting the application local on my machine with a remote connection to the SQL database and on the actual server that it lives on, so far this is all I'm getting:
/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Parser.js:437
throw err; // Rethrow non-MySQL errors
^
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'niemer5_discordbot'#'localhost' (using password: NO)
at Handshake.Sequence._packetToError (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
at Handshake.ErrorPacket (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/sequences/Handshake.js:123:18)
at Protocol._parsePacket (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Parser.js:433:10)
at Parser.write (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Parser.js:43:10)
at Protocol.write (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (/home/niemer5/news-bot/node_modules/mysql/lib/Connection.js:88:28)
at Socket.<anonymous> (/home/niemer5/news-bot/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (node:events:394:28)
at addChunk (node:internal/streams/readable:315:12)
--------------------
at Protocol._enqueue (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/home/niemer5/news-bot/node_modules/mysql/lib/Connection.js:116:18)
at Object.<anonymous> (/home/niemer5/news-bot/index.js:22:5)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47 {
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlMessage: "Access denied for user 'niemer5_discordbot'#'localhost' (using password: NO)",
sqlState: '28000',
fatal: true
}
The user exists, it has ALL privileges, I set everything up in cPanel and also tried granting privs in SQL CLI and so far no luck it just errors out with a 1045 access denied error and I have no clue what is happening. Looking for a little guidance here!

Remote MySQL: errno: -111, code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 3306

React beginner here. I just deployed an early-alpha of an app and currently getting this error -111 which from this site says that its a problem that occurs when mysqld only listens to the localhost interface. I'm using a free remote mysql for testing from this site. It says that I have to find my.cnf and modify it to allow connections aside from localhost.
Will it work if I edit my my.cnf or no because I'm using a free remote mysql that I have no control of?
Will it work if I try another free remote mysql service that may work differently?
2022-05-04T06:22:09.121413+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:3306
2022-05-04T06:22:09.121421+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16)
2022-05-04T06:22:09.121421+00:00 app[web.1]: --------------------
2022-05-04T06:22:09.121422+00:00 app[web.1]: at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
2022-05-04T06:22:09.121422+00:00 app[web.1]: at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
2022-05-04T06:22:09.121423+00:00 app[web.1]: at PoolConnection.connect (/app/node_modules/mysql/lib/Connection.js:116:18)
2022-05-04T06:22:09.121423+00:00 app[web.1]: at Pool.getConnection (/app/node_modules/mysql/lib/Pool.js:48:16)
2022-05-04T06:22:09.121424+00:00 app[web.1]: at Pool.query (/app/node_modules/mysql/lib/Pool.js:202:8)
2022-05-04T06:22:09.121424+00:00 app[web.1]: at /app/routes/operations/login.js:27:24
2022-05-04T06:22:09.121424+00:00 app[web.1]: at new Promise (<anonymous>)
2022-05-04T06:22:09.121425+00:00 app[web.1]: at getInput (/app/routes/operations/login.js:20:16)
2022-05-04T06:22:09.121425+00:00 app[web.1]: at /app/routes/operations/login.js:106:28
2022-05-04T06:22:09.121425+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5) {
2022-05-04T06:22:09.121426+00:00 app[web.1]: errno: -111,
2022-05-04T06:22:09.121426+00:00 app[web.1]: code: 'ECONNREFUSED',
2022-05-04T06:22:09.121427+00:00 app[web.1]: syscall: 'connect',
2022-05-04T06:22:09.121427+00:00 app[web.1]: address: '127.0.0.1',
2022-05-04T06:22:09.121427+00:00 app[web.1]: port: 3306,
2022-05-04T06:22:09.121427+00:00 app[web.1]: fatal: true
2022-05-04T06:22:09.121428+00:00 app[web.1]: }

'Node Api using mysql' deployed on heroku crashes after some time Interval

I have created a Node API using MySQL and deployed it on Heroku. But after every 20-25 minutes it shows 'Application error' and I have to do 'Heroku restart' again to fetch data from API. My API calls work fine when runs on localhost:port_no
Heroku Logs
>2021-06-26T04:15:47.965652+00:00 heroku[web.1]: State changed from starting to up
2021-06-26T04:20:47.759660+00:00 app[web.1]: events.js:353
2021-06-26T04:20:47.759669+00:00 app[web.1]: throw er; // Unhandled 'error' event
2021-06-26T04:20:47.759670+00:00 app[web.1]: ^
2021-06-26T04:20:47.759670+00:00 app[web.1]:
2021-06-26T04:20:47.759670+00:00 app[web.1]: Error: Connection lost: The server closed the connection.
2021-06-26T04:20:47.759671+00:00 app[web.1]: at Protocol.end (/app/node_modules/mysql/lib/protocol/Protocol.js:112:13)
2021-06-26T04:20:47.759672+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:94:28)
2021-06-26T04:20:47.759672+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:526:10)
2021-06-26T04:20:47.759673+00:00 app[web.1]: at Socket.emit (events.js:388:22)
2021-06-26T04:20:47.759673+00:00 app[web.1]: at endReadableNT (internal/streams/readable.js:1336:12)
2021-06-26T04:20:47.759674+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:82:21)
2021-06-26T04:20:47.759674+00:00 app[web.1]: Emitted 'error' event on Connection instance at:
2021-06-26T04:20:47.759676+00:00 app[web.1]: at Connection._handleProtocolError (/app/node_modules/mysql/lib/Connection.js:423:8)
2021-06-26T04:20:47.759677+00:00 app[web.1]: at Protocol.emit (events.js:376:20)
2021-06-26T04:20:47.759678+00:00 app[web.1]: at Protocol._delegateError (/app/node_modules/mysql/lib/protocol/Protocol.js:398:10)
2021-06-26T04:20:47.759678+00:00 app[web.1]: at Protocol.end (/app/node_modules/mysql/lib/protocol/Protocol.js:116:8)
2021-06-26T04:20:47.759679+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:94:28)
2021-06-26T04:20:47.759679+00:00 app[web.1]: [... lines matching original stack trace ...]
2021-06-26T04:20:47.759679+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:82:21) {
2021-06-26T04:20:47.759680+00:00 app[web.1]: fatal: true,
2021-06-26T04:20:47.759680+00:00 app[web.1]: code: 'PROTOCOL_CONNECTION_LOST'
2021-06-26T04:20:47.759681+00:00 app[web.1]: }
2021-06-26T04:20:47.817878+00:00 heroku[web.1]: Process exited with status 1
2021-06-26T04:20:47.876488+00:00 heroku[web.1]: State changed from up to crashed
My connection.js file
const mysql = require('mysql');
var sqlConnection = mysql.createConnection({
host: ##,
user:##,
password: ##,
database: ##,
multipleStatements: true
});
sqlConnection.connect((err) => {
if (!err) {
console.log("Connected");
} else {
console.log("Connection Failed");
}
})
module.exports = sqlConnection;
```

I cannot connect to mysql database using node.js

I am total beginner to database.
I am using localhost(127.0.0.1) with port 9090.
I successfully created mysql database, ran/used it in windows 10 command prompt and MySQL Workbench.
But when I try to run it with node.js, I fail to connect to server.
Below is index.js file I tried to run in windows command line.
var mysql = require('mysql');
var client = mysql.createConnection({
user:'root',
password:'password',
});
client.query('USE mydb');
client.query('SELECT * FROM PRODUCTS',function(error, result, fields){
if(error){
console.log(error);
} else{
console.log(result);
}
});
And it results in this error.
D:\Coding\VSCode\nodejs>node index.js
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
--------------------
at Protocol._enqueue (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:116:18)
at Connection._implyConnect (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:454:10)
at Connection.query (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:196:8)
at Object.<anonymous> (D:\Coding\VSCode\nodejs\index.js:10:8)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
I am at the stage not knowing what I am not knowing.
I tried to change port in createConnection() function like this
var client = mysql.createConnection({
user:'root',
password:'password',
port: '9090'
});
Then I get this error.
D:\Coding\VSCode\nodejs>node index.js
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by
server; consider upgrading MySQL client
at Handshake.Sequence._packetToError
(D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Handshake.ErrorPacket
(D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\sequences\Handshake.js:123:18)
at Protocol._parsePacket (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:88:28)
at Socket.<anonymous> (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:309:12)
--------------------
at Protocol._enqueue (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:116:18)
at Connection._implyConnect (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:454:10)
at Connection.query (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:196:8)
at Object.<anonymous> (D:\Coding\VSCode\nodejs\index.js:10:8)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14) {
code: 'ER_NOT_SUPPORTED_AUTH_MODE',
errno: 1251,
sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading
MySQL client',
sqlState: '08004',
fatal: true
}
1.Open your windows terminal and login :
mysql -uroot -p
( then type you password )
use mysql;
update user set authentication_string=password(''), plugin='mysql_native_password' where user='root';
4 Restart you mysql db : sudo service mysql restart
Doing this in MySQL Workbench solved the problem.
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password
BY 'YourRootPassword';
FLUSH PRIVILEGES;

I got this error after deploying my api on heroku "connect ECONNREFUSED 127.0.0.1:3306"

const Sequelize = require('sequelize');
const sequelize = new Sequelize(`${process.env.SQL_DB}`, 'root', `${process.env.SQL_Password}`, {
dialect: 'mysql',
host: 'localhost'
});
module.exports = sequelize;
although I stored my config var in heroku settings but it can not connect with my database I think
errors
Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306
2020-01-07T10:27:38.586244+00:00 app[web.1]: at /app/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:139:19
2020-01-07T10:27:38.586246+00:00 app[web.1]: at tryCatcher (/app/node_modules/bluebird/js/release/util.js:16:23)
2020-01-07T10:27:38.586248+00:00 app[web.1]: at Promise._settlePromiseFromHandler (/app/node_modules/bluebird/js/release/promise.js:547:31)
2020-01-07T10:27:38.586250+00:00 app[web.1]: at Promise._settlePromise (/app/node_modules/bluebird/js/release/promise.js:604:18)
2020-01-07T10:27:38.586252+00:00 app[web.1]: at Promise._settlePromise0 (/app/node_modules/bluebird/js/release/promise.js:649:10)
2020-01-07T10:27:38.586254+00:00 app[web.1]: at Promise._settlePromises (/app/node_modules/bluebird/js/release/promise.js:725:18)
2020-01-07T10:27:38.586256+00:00 app[web.1]: at _drainQueueStep (/app/node_modules/bluebird/js/release/async.js:93:12)
2020-01-07T10:27:38.586258+00:00 app[web.1]: at _drainQueue (/app/node_modules/bluebird/js/release/async.js:86:9)
2020-01-07T10:27:38.586260+00:00 app[web.1]: at Async._drainQueues (/app/node_modules/bluebird/js/release/async.js:102:5)
2020-01-07T10:27:38.586262+00:00 app[web.1]: at Immediate.Async.drainQueues [as _onImmediate] (/app/node_modules/bluebird/js/release/async.js:15:14)
2020-01-07T10:27:38.586264+00:00 app[web.1]: at processImmediate (internal/timers.js:439:21)