why does my node app works for a few moments on and crashes( heroku)? - mysql

i have deployed a nodejs app on heroku with ClearDb mysql, the app works for a few moments then crashes. i checked logs and got this errors
2019-09-20T19:03:41.536452+00:00 app[web.1]: events.js:174
2019-09-20T19:03:41.536474+00:00 app[web.1]: throw er; // Unhandled 'error' event
2019-09-20T19:03:41.536476+00:00 app[web.1]: ^
2019-09-20T19:03:41.536478+00:00 app[web.1]:
2019-09-20T19:03:41.536481+00:00 app[web.1]: Error: Connection lost: The server closed the connection.
2019-09-20T19:03:41.536483+00:00 app[web.1]: at Protocol.end (/app/node_modules/mysql/lib/protocol/Protocol.js:112:13)
2019-09-20T19:03:41.536485+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:97:28)
2019-09-20T19:03:41.536488+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:525:10)
2019-09-20T19:03:41.53649+00:00 app[web.1]: at Socket.emit (events.js:203:15)
2019-09-20T19:03:41.536492+00:00 app[web.1]: at endReadableNT (_stream_readable.js:1145:12)
2019-09-20T19:03:41.536495+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:63:19)
2019-09-20T19:03:41.536497+00:00 app[web.1]: Emitted 'error' event at:
2019-09-20T19:03:41.536503+00:00 app[web.1]: at Connection._handleProtocolError (/app/node_modules/mysql/lib/Connection.js:426:8)
2019-09-20T19:03:41.536506+00:00 app[web.1]: at Protocol.emit (events.js:198:13)
2019-09-20T19:03:41.536509+00:00 app[web.1]: at Protocol._delegateError (/app/node_modules/mysql/lib/protocol/Protocol.js:398:10)
2019-09-20T19:03:41.536511+00:00 app[web.1]: at Protocol.end (/app/node_modules/mysql/lib/protocol/Protocol.js:116:8)
2019-09-20T19:03:41.536513+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:97:28)
2019-09-20T19:03:41.536515+00:00 app[web.1]: [... lines matching original stack trace ...]
2019-09-20T19:03:41.536517+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:63:19)
2019-09-20T19:03:41.599727+00:00 heroku[web.1]: Process exited with status 1
2019-09-20T19:03:41.653368+00:00 heroku[web.1]: State changed from up to crashed

This could be the problem
You can use the following code to handle the server disconnect
var db_config = {
host: 'localhost',
user: 'root',
password: '',
database: 'example'
};
var connection;
function handleDisconnect() {
connection = mysql.createConnection(db_config); // Recreate the connection, since
// the old one cannot be reused.
connection.connect(function(err) { // The server is either down
if(err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
connection.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
}
handleDisconnect();

Related

NodeJS - MySQL server not working after some time

Basically I have my Node.js server working along with MySQL. When I work on my localhost everything's fine. The connection to my local DB (I'm using XAMPPP) is great and nothing breaks.
The problem comes along when the server is hosted by a provider. The one I hired uses cPanel and everithing's great until some time passes, because I get this error:
events.js:377
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:209:20)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (/home/adminis6/Artesofa/node_modules/mysql/lib/Connection.js:423:8)
at Protocol.emit (events.js:400:28)
at Protocol._delegateError (/home/adminis6/Artesofa/node_modules/mysql/lib/protocol/Protocol.js:398:10)
at Protocol.handleNetworkError (/home/adminis6/Artesofa/node_modules/mysql/lib/protocol/Protocol.js:371:10)
at Connection._handleNetworkError (/home/adminis6/Artesofa/node_modules/mysql/lib/Connection.js:418:18)
at Socket.emit (events.js:400:28)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
errno: -104,
code: 'ECONNRESET',
syscall: 'read',
fatal: true
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! amor-muebles#1.0.0 start: `node app.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the amor-muebles#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/adminis6/.npm/_logs/2021-11-30T19_15_37_322Z-debug.log
I've been researching how to solve this problem and the only useful answer I got basically said that the DB connection was timming out, so all I had to do was to make a request on an interval and hope it won't break. So I wrote the following code in my app.js file:
const fetch = require("node-fetch");
setInterval(() => {
fetch('sample-endpoint');
}, 30000);
Although this seemed to have solved my problem, it appeared over and over again (note that the server did last longer being up).
Later on, some people taught me about CRONS so I made the following CRON:
PATH=$PATH:$HOME/bin; export PATH; /usr/bin/pgrep "node" >/dev/null || (cd /home/adminis6/Artesofa/; pkill node; pkill npm; nohup npm start &)
And it does work, because it gets the server up, but it instantly crashes (literally right after the server initiates, even after the server connects to the DB successfully), and it logs the following:
> amor-muebles#1.0.0 start /home/adminis6/Artesofa
> node app.js
Server running on port 3100
mysql connected
events.js:377
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:209:20)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (/home/adminis6/Artesofa/node_modules/mysql/lib/Connection.js:423:8)
at Protocol.emit (events.js:400:28)
at Protocol._delegateError (/home/adminis6/Artesofa/node_modules/mysql/lib/protocol/Protocol.js:398:10)
at Protocol.handleNetworkError (/home/adminis6/Artesofa/node_modules/mysql/lib/protocol/Protocol.js:371:10)
at Connection._handleNetworkError (/home/adminis6/Artesofa/node_modules/mysql/lib/Connection.js:418:18)
at Socket.emit (events.js:400:28)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
errno: -104,
code: 'ECONNRESET',
syscall: 'read',
fatal: true
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! amor-muebles#1.0.0 start: `node app.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the amor-muebles#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/adminis6/.npm/_logs/2021-11-30T20_14_02_182Z-debug.log
I don't know what else to try nor I have much more time, please help!
If you need it, here is my app.js:
/* ----------- Server initialization ----------- */
//Here are all the modudule's require
const app = express();
// Connect to mySQL DB
const db = require('./db/connectDB');
// Set server to listen on specified port
app.listen(process.env.PORT || '4000', () => {
console.log(`Server running on port ${process.env.PORT} AAAAA`);
})
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.set('views', [
path.join(__dirname, 'views/adminSite/')
]);
/* ----------- Middleware ----------- */
app.use(express.urlencoded({ extended: true }));
app.use(helmet());
app.use(cookieParser());
app.use(morgan('tiny'));
/* ----------- Routes ----------- */
app.use('/api', apiRoutes);
setInterval(() => {
fetch('https://administracionartesofa.com/api/sucursales');
}, 30000);
And, finally, here is my connectDB file:
const mysql = require('mysql');
const dotenv = require('dotenv').config();
const settings = process.env.ENV === 'dev' ? require('./devSettings.json') : require('./prodSettings.json');
let db;
const connectDatabase = () => {
if (!db) {
db = mysql.createConnection(settings);
db.connect((err) => {
if (err) {
console.log('Database error');
console.log(err);
connectDatabase();
} else {
console.log('mysql connected');
}
})
}
return db;
}
module.exports = connectDatabase();
Use a mysql connection pool in your nodejs program. Your hosting provider's cheap and nasty shared MySql server has an aggressively short idle connection time limit. If you hold open a connection for too long the server slams it shut, so you get ECONNRESET.
Why? Cybercreeps trying to break in to random servers on the internet for fun and profit. This slows them down a bit, hopefully.
Connection pools cope with this behind the scenes if you
set up a pool at app startup, and
grab a connection from the pool when you need one, use it, and then return it to the pool.
Or, you can skip the pooling and just close your connection when you're done using it, then open a new one when you need it again. That will work fine for a low-volume app, but it might cause some inefficient connection thrashing if your volume goes up. Pools are better.

'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;
```

node server in expressjs stops working after a while on its own

after running npm start, some time passes and then i get this error in the terminal. I am new to express js and node paired with mysql, so I can't really understand the errors below, hopefully that's enough context for this. The project is a simple table display of the database's table content, and it works fine, I just have to run npm start every 10-40 seconds.
events.js:291
throw er; // Unhandled 'error' event
^
Error: Connection lost: The server closed the connection.
at Protocol.end (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:112:13)
at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:94:28)
at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (events.js:326:22)
at endReadableNT (_stream_readable.js:1226:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:423:8)
at Protocol.emit (events.js:314:20)
at Protocol._delegateError (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:398:10)
at Protocol.end (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:116:8)
at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:94:28)
[... lines matching original stack trace ...]
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
fatal: true,
code: 'PROTOCOL_CONNECTION_LOST'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! nodeapp#0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the nodeapp#0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/thodoristhomaidis/.npm/_logs/2021-01-22T03_09_42_711Z-debug.log
thodoristhomaidis#Theos-iMac nodeapp % npm i -g mysql
+ mysql#2.18.1
added 11 packages from 15 contributors in 0.589s
thodoristhomaidis#Theos-iMac nodeapp % npm start
> nodeapp#0.0.0 start /Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp
> node ./bin/www
Database is connected successfully !
GET /users/form 304 7.634 ms - -
GET /css/form-style.css 404 3.291 ms - 1532
events.js:291
throw er; // Unhandled 'error' event
^
Error: Connection lost: The server closed the connection.
at Protocol.end (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:112:13)
at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:94:28)
at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (events.js:326:22)
at endReadableNT (_stream_readable.js:1226:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:423:8)
at Protocol.emit (events.js:314:20)
at Protocol._delegateError (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:398:10)
at Protocol.end (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:116:8)
at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:94:28)
[... lines matching original stack trace ...]
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
fatal: true,
code: 'PROTOCOL_CONNECTION_LOST'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! nodeapp#0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the nodeapp#0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/thodoristhomaidis/.npm/_logs/2021-01-22T03_11_13_709Z-debug.log
Try to use this code to handle server disconnect:
var db_config = {
host: 'localhost',
user: 'root',
password: '',
database: 'example'
};
var connection;
function handleDisconnect() {
connection = mysql.createConnection(db_config); // Recreate the connection, since
// the old one cannot be reused.
connection.connect(function(err) { // The server is either down
if(err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
connection.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
}
handleDisconnect();
In your code i am missing the parts after connection = mysql.createConnection(db_config);
can you check this

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

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 :)

Express app getting disconnected right after the connection

I created a rest api with Express. on my server.js the i have my mysql connection.. though the connection is made at first. It's working on my local machine properly but when i deployed on AWS EC2 service then i am facing this problem..
server.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'myusername',
password: 'mypassword',
database: 'mydatabasename'
});
connection.connect(function (err) {
if (err) throw err;
console.log('connected!');
});
module.exports = connection;
i tried pm 2 handledisconnect() following other project function none worked.getting following error...
restapi#0.0.0 start /home/bcs_quiz_console/bcs-preparation-backend/restapi
npm start
connected!
events.js:180
throw er; // Unhandled 'error' event
^
Error: Connection lost: The server closed the connection.
at Protocol.end (/home/bcs_quiz_console/bcs-preparation-backend/restapi/node_modules/mysql/lib/protocol/Protocol.js:112:13)
at Socket. (/home/bcs_quiz_console/bcs-preparation-backend/restapi/node_modules/mysql/lib/Connection.js:97:28)
at Socket. (/home/bcs_quiz_console/bcs-preparation-backend/restapi/node_modules/mysql/lib/Connection.js:525:10)
at Socket.emit (events.js:208:15)
at endReadableNT (_stream_readable.js:1168:12)
at processTicksAndRejections (internal/process/task_queues.js:77:11)
Emitted 'error' event at:
at Connection._handleProtocolError (/home/bcs_quiz_console/bcs-preparation-backend/restapi/node_modules/mysql/lib/Connection.js:426:8)
at Protocol.emit (events.js:203:13)
at Protocol._delegateError (/home/bcs_quiz_console/bcs-preparation-backend/restapi/node_modules/mysql/lib/protocol/Protocol.js:398:10)
at Protocol.end (/home/bcs_quiz_console/bcs-preparation-backend/restapi/node_modules/mysql/lib/protocol/Protocol.js:116:8)
at Socket. (/home/bcs_quiz_console/bcs-preparation-backend/restapi/node_modules/mysql/lib/Connection.js:97:28)
[... lines matching original stack trace ...]
at processTicksAndRejections (internal/process/task_queues.js:77:11) {
fatal: true,
code: 'PROTOCOL_CONNECTION_LOST'
}
Thank anyone who can help in advance..