Nodejs Mariadb on PCF timeout not triggering mariadb events - javascript

I have a nodejs application on PCF which connects to a mariadb instance. The instance wait_timeout is 600 so after inactivity it will disconnect after 10 minutes.
On my local machine running this application and connecting to a local instance of mariadb on docker (once timeout hits or when i kill the connection) i will get a MySQL Server has gone away then my application will reconnect and give me my database result. It does not crash locally.
When running this on PCF the mariadb error will crash the application or if the exception is caught it will crash and send a Headers already sent error
I believe that once MariaDB kills the connection somehow the mariadb driver in node is not notified that the connection was terminated even though i have event handlers on the connection. So once my application attempts to run the query it fails because the event was not triggered and its trying to run it on a closed connection.
My connection module
// Loading local .env vars
var route_helper = require("./route_helper");
route_helper.loadLocalEnvVars();
var Client = require('mariasql');
var moment = require('moment');
var connection = null;
try{
connection = new Client({
host: process.env.DB_MARIA_HOST,
user: process.env.DB_MARIA_USER,
password: process.env.DB_MARIA_PASS,
db: process.env.DB_MARIA_SCHEMA,
port: parseInt(process.env.DB_MARIA_PORT),
connTimeout: 20,
pingInterval: 1,
compress: true
});
connection.connect(function(err){
if(!err) {
if(process.env.DEBUG == "true"){
console.log("Database is connected");
}
} else {
console.log("Error connecting database " + err.message);
console.log(err);
}
});
connection.on('ready',function(){
if(process.env.DEBUG == "true"){
console.log("Connection ready " + moment().format('MMMM Do YYYY, h:mm:ss a') )
}
});
connection.on('close',function(){
if(process.env.DEBUG == "true"){
console.log("Connection closed " + moment().format('MMMM Do YYYY, h:mm:ss a') )
}
});
connection.on('error',function(err){
if(process.env.DEBUG == "true"){
console.log("Connection timeout... Reconnecting")
console.log(err);
}
if(err.code == 2006 ){
this.connect();
}
//this.connect();
});
}catch(err){
console.log("Maria_db error")
console.log(err)
throw err;
}
module.exports = connection;
Loading Server routers
var routes = require("./routes/application.js").appRouter(server);
var server = server.listen(process.env.PORT, function () {
console.log("Listening on port %s...", server.address().port);
});
My Route
app.get("/applications", function (req, res) {
try{
var applications;
var dbConn = require("../util/db.js");
dbConn.query('SELECT some stuff’, function (error, results) {
if(!error) {
applications = {
"apps": results
}
return res.send(applications);
} else {
return res.send({"status": "error", "message": error.code});
}
});
}catch(err) {
console.log(err)
}
});
MySql has gone away error
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] OUT { Error: MySQL server has gone away
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] OUT at /home/vcap/app/node_modules/mariasql/lib/Client.js:223:12
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] ERR at Client.<anonymous> (/home/vcap/app/util/dbHealth.js:14:9)
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] ERR at emitOne (events.js:101:20)
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] ERR at Client.emit (events.js:191:7)
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] ERR at Client._onerror (/home/vcap/app/node_modules/mariasql/lib/Client.js:395:10)
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] ERR at Client._processQueue (/home/vcap/app/node_modules/mariasql/lib/Client.js:614:18)
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] ERR at /home/vcap/app/node_modules/mariasql/lib/Client.js:223:12
2017-12-20T09:03:55.29-0500 [APP/PROC/WEB/0] ERR at process._tickCallback (internal/process/next_tick.js:104:9)
2017-12-20T09:03:55.31-0500 [APP/PROC/WEB/0] ERR npm ERR! Exit status 1
2017-12-20T09:03:55.31-0500 [APP/PROC/WEB/0] ERR npm ERR! Failed at the myapplication#1.0.0 start script 'node server.js'.
2017-12-20T09:03:55.31-0500 [APP/PROC/WEB/0] ERR npm ERR! not with npm itself.
2017-12-20T09:03:55.31-0500 [APP/PROC/WEB/0] ERR npm ERR! You can get information on how to open an issue for this project with:
2017-12-20T09:03:55.31-0500 [APP/PROC/WEB/0] ERR npm ERR! npm bugs myapplication
2017-12-20T09:03:55.31-0500 [APP/PROC/WEB/0] ERR npm ERR! Or if that isn't available, you can get their info via:
2017-12-20T09:03:55.31-0500 [APP/PROC/WEB/0] ERR npm ERR! npm owner ls myapplication
2017-12-20T09:03:55.33-0500 [APP/PROC/WEB/0] ERR npm ERR! Please include the following file with any support request:
2017-12-20T09:03:55.33-0500 [APP/PROC/WEB/0] ERR npm ERR! /home/vcap/app/.npm/_logs/2017-12-20T14_03_55_316Z-debug.log
Headers already sent error. They also seem to be out of order which is weird. During this test i am sending 3 http requests to 3 different services and getting this error.
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at _combinedTickCallback (internal/process/next_tick.js:131:7)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at Client._processQueue (/home/vcap/app/node_modules/mariasql/lib/Client.js:614:18)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at process._tickCallback (internal/process/next_tick.js:180:9) code: 2006 }
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR _http_outgoing.js:494
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at validateHeader (_http_outgoing.js:494:11)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at ServerResponse.send (/home/vcap/app/node_modules/express/lib/response.js:158:21)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at Object.cb (/home/vcap/app/routes/application.js:474:29)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at Client._processQueue (/home/vcap/app/node_modules/mariasql/lib/Client.js:614:18)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at _combinedTickCallback (internal/process/next_tick.js:131:7)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at process._tickCallback (internal/process/next_tick.js:180:9) code: 2006 }
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR throw new Error('Can\'t set headers after they are sent.');
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR Error: Can't set headers after they are sent.
2017-12-27T11:07:46.41-0500 [APP/PROC/WEB/0] OUT Connection timeout... Reconnecting
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at /home/vcap/app/node_modules/mariasql/lib/Client.js:223:12
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT { Error: MySQL server has gone away
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT { Error: MySQL server has gone away
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at _combinedTickCallback (internal/process/next_tick.js:131:7)
2017-12-27T11:07:46.41-0500 [APP/PROC/WEB/0] OUT Route: /applications
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT { Error: MySQL server has gone away
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at Client._processQueue (/home/vcap/app/node_modules/mariasql/lib/Client.js:614:18)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at /home/vcap/app/node_modules/mariasql/lib/Client.js:223:12
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at ServerResponse.setHeader (_http_outgoing.js:501:3)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at ServerResponse.header (/home/vcap/app/node_modules/express/lib/response.js:767:10)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at process._tickCallback (internal/process/next_tick.js:180:9) code: 2006 }
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] OUT at /home/vcap/app/node_modules/mariasql/lib/Client.js:223:12
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR ^
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at ServerResponse.send (/home/vcap/app/node_modules/express/lib/response.js:170:12)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at ServerResponse.json (/home/vcap/app/node_modules/express/lib/response.js:267:15)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at cleanupReqs (/home/vcap/app/node_modules/mariasql/lib/Client.js:744:11)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at Client._onclose (/home/vcap/app/node_modules/mariasql/lib/Client.js:574:5)
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR at Client._onerror (/home/vcap/app/node_modules/mariasql/lib/Client.js:396:10)
2017-12-27T11:07:46.43-0500 [APP/PROC/WEB/0] ERR npm ERR! Failed at the myapplication#1.0.0 start script.
2017-12-27T11:07:46.42-0500 [APP/PROC/WEB/0] ERR npm ERR! code ELIFECYCLE
2017-12-27T11:07:46.43-0500 [APP/PROC/WEB/0] ERR npm ERR!
2017-12-27T11:07:46.43-0500 [APP/PROC/WEB/0] ERR npm ERR! myapplication#1.0.0 start: `node server.js`
2017-12-27T11:07:46.43-0500 [APP/PROC/WEB/0] ERR npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2017-12-27T11:07:46.43-0500 [APP/PROC/WEB/0] ERR npm ERR! Exit status 1
2017-12-27T11:07:46.43-0500 [APP/PROC/WEB/0] ERR npm ERR! errno 1
2017-12-27T11:07:46.43-0500 [APP/PROC/WEB/0] ERR npm ERR! A complete log of this run can be found in:
2017-12-27T11:07:46.43-0500 [APP/PROC/WEB/0] ERR npm ERR! /home/vcap/app/.npm/_logs/2017-12-27T16_07_46_432Z-debug.log
edit: This application structure was generated by swagger

I switch from only mariasql npm driver to knex driver. Even though knex dependancies had mariasql: '^0.2.3' it worked right out of the box with no problems.

Related

Can't connect to remote db (Hostinger)

I've been having this error for a long time and I can't solve it. I have made my project locally, connecting to my database locally. Once finished I wanted to migrate my database manually since it is small to my hosting provider.
I have created the database, I have given it permissions to connect remotely and I have also checked that the connection data is correct.
A curiosity is that through mysql workbench it has allowed me to connect, the problem is clearly with my code, I am working with NodeJS (express). Another curiosity is that it appears to me as if I have successfully connected and 30 seconds later it gives me the following error:
Node.js v18.12.0
[nodemon] app crashed - waiting for file changes before starting...
[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
Listen on port 3000
Connection success!
node:events:491
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:217:20)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError
(C:\Users\M\Desktop\Proyect\node_modules\mysql\lib\Connection.js:423:8)
at Protocol.emit (node:events:513:28)
at Protocol._delegateError
(C:\Users\M\Desktop\Proyect\node_modules\mysql\lib\protocol\Protocol.js:398:10)
at Protocol.handleNetworkError
(C:\Users\M\Desktop\Proyect\node_modules\mysql\lib\protocol\Protocol.js:371:10)
at Connection._handleNetworkError
(C:\Users\M\Desktop\Proyect\node_modules\mysql\lib\Connection.js:418:18)
at Socket.emit (node:events:513:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read',
fatal: true
}
Node.js v18.12.0
[nodemon] app crashed - waiting for file changes before starting...
This is my connection script:
const mysql = require('mysql')
const connection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE
});
connection.connect((error) => {
if(error){
console.log('The connection error is: ' + error)
return;
}
console.log('Connection success!')
})
module.exports = connection;
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
// Now use the connection to do stuff
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Result: " + result);
});
});
You don't show the rest of your code, but you're not doing any query in that callback function, just outputting success. You need to do stuff inside that callback, or change the way you're exporting the connection. Have a look at createPool, and getConnection as I'm guessing you're actually wanting a persistent connection to your database?

Server crashes when making an API call

app.post("/", (req, res) => {
const origin = req.body.origin;
const destination = req.body.destination;
const urlOri = "https://api.openweathermap.org/data/2.5/weather?q=" + origin + "&APPID={mykey}"
https.get(urlOri, function (response) {
console.log(response);
})
});
I have written this code and I am getting this error:
Server started on port 443
events.js:292
throw er; // Unhandled 'error' event
^
Error: connect ETIMEDOUT 178.128.25.248:443
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
Emitted 'error' event on ClientRequest instance at:
at TLSSocket.socketErrorListener (_http_client.js:469:9)
at TLSSocket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: -4039,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '178.128.25.248',
port: 443
}
This same code runs on one of my friend's pc same as expected, what to do?
I have tried to do the same via request and axios module as well and I got the same error.
Sorry guys I was using a proxy server.
I disabled it and now it's working fine

Heroku Node deployment "internal server error"

I have my app deployed to heroku. Once I go to the user signup page and enter my credentials the site immediately crashes, displaying internal server error in the browser, and giving me this in the heroku logs.
I'm not sure what might be the issue so I don't know which code to give? I don't think it has to do with MySQL but considering it's mentioned in the error report, it might be? Either way here is my connection.js:
const mysql = require('mysql');
let connection;
if (process.env.JAWSDB_URL) {
connection = mysql.createConnection(process.env.JAWSDB_URL);
} else {
connection = mysql.createConnection({
host: 'localhost',
port: 3306,
user: 'root',
password: '',
database: 'trythis_db'
});
};
connection.connect(function(err) {
if (err) {
console.log(err);
} else {
console.log('mysql is connected');
};
});
module.exports = connection;
This is my error code:
2017-12-04T04:58:51.486756+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:3306
2017-12-04T04:58:51.486150+00:00 app[web.1]: POST /signup 302 287.664 ms - 46
2017-12-04T04:58:51.486758+00:00 app[web.1]: at Object._errnoException (util.js:1024:11)
2017-12-04T04:58:51.486758+00:00 app[web.1]: at _exceptionWithHostPort (util.js:1046:20)
2017-12-04T04:58:51.486759+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
2017-12-04T04:58:51.486760+00:00 app[web.1]: --------------------
2017-12-04T04:58:51.486761+00:00 app[web.1]: at Protocol._enqueue (/app/node_modules/express-mysql-session/node_modules/mysql/lib/protocol/Protocol.js:145:48)
2017-12-04T04:58:51.486762+00:00 app[web.1]: at Protocol.handshake (/app/node_modules/express-mysql-session/node_modules/mysql/lib/protocol/Protocol.js:52:23)
2017-12-04T04:58:51.486763+00:00 app[web.1]: at PoolConnection.connect (/app/node_modules/express-mysql-session/node_modules/mysql/lib/Connection.js:130:18)
2017-12-04T04:58:51.486763+00:00 app[web.1]: at Pool.getConnection (/app/node_modules/express-mysql-session/node_modules/mysql/lib/Pool.js:48:16)
2017-12-04T04:58:51.486764+00:00 app[web.1]: at Pool.query (/app/node_modules/express-mysql-session/node_modules/mysql/lib/Pool.js:202:8)
2017-12-04T04:58:51.486765+00:00 app[web.1]: at MySQLStore.set (/app/node_modules/express-mysql-session/lib/index.js:192:19)
2017-12-04T04:58:51.486766+00:00 app[web.1]: at Session.save (/app/node_modules/express-session/session/session.js:72:25)
2017-12-04T04:58:51.486766+00:00 app[web.1]: at Session.save (/app/node_modules/express-session/index.js:381:15)
2017-12-04T04:58:51.486767+00:00 app[web.1]: at ServerResponse.end (/app/node_modules/express-session/index.js:330:21)
2017-12-04T04:58:51.486768+00:00 app[web.1]: at ServerResponse.redirect (/app/node_modules/express/lib/response.js:947:10)
2017-12-04T04:58:51.638706+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:3306
2017-12-04T04:58:51.638708+00:00 app[web.1]: at Object._errnoException (util.js:1024:11)
2017-12-04T04:58:51.638709+00:00 app[web.1]: at _exceptionWithHostPort (util.js:1046:20)
2017-12-04T04:58:51.638710+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
2017-12-04T04:58:51.638710+00:00 app[web.1]: --------------------
2017-12-04T04:58:51.638711+00:00 app[web.1]: at Protocol._enqueue (/app/node_modules/express-mysql-session/node_modules/mysql/lib/protocol/Protocol.js:145:48)
2017-12-04T04:58:51.638712+00:00 app[web.1]: at Protocol.handshake (/app/node_modules/express-mysql-session/node_modules/mysql/lib/protocol/Protocol.js:52:23)
2017-12-04T04:58:51.638713+00:00 app[web.1]: at PoolConnection.connect (/app/node_modules/express-mysql-session/node_modules/mysql/lib/Connection.js:130:18)
2017-12-04T04:58:51.638713+00:00 app[web.1]: at Pool.getConnection (/app/node_modules/express-mysql-session/node_modules/mysql/lib/Pool.js:48:16)
2017-12-04T04:58:51.638714+00:00 app[web.1]: at Pool.query (/app/node_modules/express-mysql-session/node_modules/mysql/lib/Pool.js:202:8)
2017-12-04T04:58:51.638802+00:00 app[web.1]: at session (/app/node_modules/express-session/index.js:460:11)
2017-12-04T04:58:51.638800+00:00 app[web.1]: at MySQLStore.get (/app/node_modules/express-mysql-session/lib/index.js:130:19)
2017-12-04T04:58:51.638803+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2017-12-04T04:58:51.638804+00:00 app[web.1]: at trim_prefix (/app/node_modules/express/lib/router/index.js:317:13)
2017-12-04T04:58:51.638804+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:284:7
The first line of the error (Error: connect ECONNREFUSED 127.0.0.1:3306) says that the app is trying to connect to localhost even when it is hosted online, where it should try to connect to the online database. You need to figure out what's wrong in the if-else statements, since, the else loop is being executed, which, I guess, is for your local environment.
Make sure you have added JAWSDB_URL under project config variables.
(Heroku project -> settings -> config variables -> reveal variables -> your var)
Issue was with another library related to storing sessions in MySQL...

Node.js MongoDB Creating Database giving error [duplicate]

This question already has answers here:
ECONNREFUSED error when connecting to mongodb from node.js
(21 answers)
Closed 5 years ago.
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err)
{
throw err;
}
console.log("Database created!");
db.close();
});
This is the error I am getting when executing this code in nodejs and trying to create a database, I was going through the tutorial in w3school, please help to solve this error--
C:\Users\n.patel\Desktop\Promotion\nodejs\application4success>node mymongodb.js
C:\Users\n.patel\Desktop\Promotion\nodejs\application4success\node_modules\mongodb\lib\mongo_client.js:415
throw err
^
MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
at null.<anonymous> (C:\Users\n.patel\Desktop\Promotion\nodejs\application4success\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:3
29:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null.<anonymous> (C:\Users\n.patel\Desktop\Promotion\nodejs\application4success\node_modules\mongodb\node_modules\mongodb-core\lib\connection\pool.js:280
:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (C:\Users\n.patel\Desktop\Promotion\nodejs\application4success\node_modules\mongodb\node_modules\mongodb-core\lib\connection\connection.js:187:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
Make sure that you have Mongo running on on port 27017
use this to verify
mongo localhost:27017

Site.js connection timeout

I'm trying to launch site.js script on my page(http://www.erzis.ct8.pl), but it keeps showing me this error.
[2017-01-04 16:42:34.936] [TRACE] [default] - Strange error
[2017-01-04 16:42:34.936] [DEBUG] [default] - { [Error: listen EPERM 0.0.0.0:8080]
code: 'EPERM',
errno: 'EPERM',
syscall: 'listen',
address: '0.0.0.0',
port: 8080 }
Error: listen EPERM 0.0.0.0:8080
at Object.exports._errnoException (util.js:873:11)
at exports._exceptionWithHostPort (util.js:896:20)
at Server._listen2 (net.js:1237:19)
at listen (net.js:1286:10)
at Server.listen (net.js:1382:5)
at Server.listen.Server.attach (/usr/home/erzis/domains/erzis.ct8.pl/public_html/Bot/BOT/node_modules/socket.io/lib/index.js:228:9)
at null._onTimeout (/usr/home/erzis/domains/erzis.ct8.pl/public_html/Bot/BOT/site.js:618:29)
at Timer.listOnTimeout (timers.js:92:15)

Categories

Resources