PostGres Timeout error in AWS EC2 Instance (Production) - node.js

I'm newbie to the backend below it's my configuration for Postgres
const pool = new Pool({
user: "username",
host: "hostname",
database: "dbname",
password: "postgres",
port: 5432,
max: 10,
min: 10,
statement_timeout: 10000,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000,
maxUses: 7500,
});
console.log("requesting db");
pool.connect((err, client, release) => {
console.error("Error acquiring client", err);
console.error("Error acquiring client", client);
console.error("Error acquiring client", release);
if (err) {
return console.error("Error acquiring client", err.stack);
}
});
pool.on("connect", () => {
console.log("connected to the db");
});
pool.on("error", function (err, client) {
console.log(client);
console.log(err);
});
module.exports = pool;
In production, I'm facing this below error but it works in local I tried connecting my prod DB in my local machine its working fine
Error acquiring client Error: Connection terminated due to connection timeout
at Connection.<anonymous> (/home/ubuntu/TapToCookBackEnd/node_modules/pg/lib/client.js:255:9)
at Object.onceWrapper (events.js:421:28)
at Connection.emit (events.js:315:20)
at Socket.<anonymous> (/home/ubuntu/TapToCookBackEnd/node_modules/pg/lib/connection.js:78:10)
at Socket.emit (events.js:315:20)
at emitCloseNT (net.js:1656:8)
at processTicksAndRejections (internal/process/task_queues.js:83:21)
at runNextTicks (internal/process/task_queues.js:66:3)
at listOnTimeout (internal/timers.js:518:9)
at processTimers (internal/timers.js:492:7)
Error acquiring client undefined
Error acquiring client [Function: NOOP]
Error acquiring client Error: Connection terminated due to connection timeout
at Connection.<anonymous> (/home/ubuntu/TapToCookBackEnd/node_modules/pg/lib/client.js:255:9)
at Object.onceWrapper (events.js:421:28)
at Connection.emit (events.js:315:20)
at Socket.<anonymous> (/home/ubuntu/TapToCookBackEnd/node_modules/pg/lib/connection.js:78:10)
at Socket.emit (events.js:315:20)
at emitCloseNT (net.js:1656:8)
at processTicksAndRejections (internal/process/task_queues.js:83:21)
at runNextTicks (internal/process/task_queues.js:66:3)
at listOnTimeout (internal/timers.js:518:9)
at processTimers (internal/timers.js:492:7)
Below route are working fine in production but connection is not established to postgres
app.get("/api/v1/test", function (req, res) {
res.send("Hello World test!");
});
My EC2 Configuration
Inbound rule
OutBound rule
My RDS Configuration
Inbound rule
OutBound rule

I suspect the RDS instance does not have a security group that allows connections from your EC2 instance. See this help article: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.RDSSecurityGroups.html
If your RDS instance has a public IP address, then the domain name will resolve to that. If both your EC2 instance and RDS instance are hosted in the same VPC, then I recommend disabling the public IP address. This may make it harder to connect to the database from your personal computer, but you can use an SSH tunnel to accomplish in this case.

When setting up RDS for public access, it is not enough to open the ports in the Security Group.
You also need to select yes for "Publicly accessible", which is hidden inside the "Additional connectivity configuration" section (which is closed by default, so easy to overlook).

Related

ioredis Error: connect ETIMEDOUT - Can't get connection to local redis server

Problem
On my node.js backend, I initialized a redis server:
const options = {
host: process.env.REDIS_HOST, // localhost
port: process.env.REDIS_PORT, // 6379 Redis standard port
db: 0,
// reconnect after
retryStrategy: times => Math.min(times * 50, 2000),
tls: {}
};
export const redis = new Redis(options);
Unfortunately, I always get this error message:
[ioredis] Unhandled error event: Error: connect ETIMEDOUT
at TLSSocket.<anonymous> (/home/pascal/vipfy/vipfy-backend/node_modules/ioredis/built/redis.js:298:31)
at Object.onceWrapper (events.js:273:13)
at TLSSocket.emit (events.js:182:13)
at TLSSocket.EventEmitter.emit (domain.js:442:20)
at TLSSocket.Socket._onTimeout (net.js:449:8)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
I installed redis locally and use the redis-cli to ping the local server, no password is set. It always gives a positive answer, but I can't seem to be able to reach it via ioredis. Anybody an idea?
Make sure your redis server is running. You just try without options params, so it will try to connect your localhost redis automatically by host as localhost and port as 6379.
redis = new Redis();
If you don't have any specific advantage try following, I am using following one and works well.
Package : "redis": "^2.8.0"
Code :
var redis = require('redis');
var redis_conn = redis.createClient();
redis_conn.set("key", "val");

Establish a connection between the raspberry and mongodb

I created a database on my pc and I wanted to create a javascript script using node, which would allow me to access all the database data installed on my pc. I tried to establish a connection between raspberry and my pc locally using the same router. Unfortunately, it always gives me connection errors.
Looking a bit on the internet I found many posts about "bindIp" of mongodb, but despite several tests found the same problem.
network interfaces
net:
   port: 27017
   #bindIp: 127.0.0.1,192.168.1.8
sorry for my English.
Code raspberry:
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://168.192.1.6:27017/"; //ip my pc and mongodb
var day = new Date();
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("qadrio");
dbo.collection("pin").findOne({}, function(err, result) {
if (err) throw err;
console.log("NAME DEVICE: "+ result.name);
console.log("ERROR: "+ err);
db.close();
});
});
I'm not an expert so I ask for help from you users. I would like to understand how it is done. I think the problem is mongodb itself that blocks external connections except the local ones of the pc.
Error Message
failed to connect to server [168.192.1.6:27017] on first connect [MongoNetworkError: connection 0 to 168.192.1.6:27017 timed out] at Pool. at Pool.emit (events.js:182:13) at Connection. at Object.onceWrapper (events.js:273:13) at Connection.emit (events.js:182:13) at Socket. at Object.onceWrapper (events.js:273:13) at Socket.emit (events.js:182:13) at Socket._onTimeout (net.js:449:8) at ontimeout (timers.js:425:11)

node express-myconnection mysql pool : connection closed by server

I have a node server that create a connection pool to mysql, if there has been no activity for a period of time the connection is closed by server and and further access throws an error. Not sure where to go with this, have researched a number of forums with little luck.
var mysql = require('mysql');
var connection = require('express-myconnection');
// DB connection
var dbOptions = {
host: 'localhost',
user: 'admin',
password: 'admin',
port: 3306,
database: 'data',
multipleStatements: true,
connectionLimit: 20
};
app.use(connection(mysql, dbOptions, pool));
Error
Whoops there is an uncaught error Error: Connection lost: The server closed the connection.
at Protocol.end (/opt/www/CMI_intern_2/node_modules/mysql/lib/protocol/Protocol.js:109:13)
at Socket.<anonymous> (/opt/www/CMI_intern_2/node_modules/mysql/lib/Connection.js:109:28)
at emitNone (events.js:72:20)
at Socket.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:921:12)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickCallback (node.js:356:17)
Connection pool works fine, there was a hidden database connection in some old code that was the root cause. Non issue.
actually, if someone experiences those errors while calling localhost adding database option
socketPath: '/var/run/mysqld/mysqld.sock'
solve this problem

NodeJS server closes connection after idling for a few hours

This question has been re-asked at past years, however solutions are either using deprecated methods or are not clear.
NodeJS server (running on CentOS) after being idle for some hours, usually at night, closes its connection and shuts down probably because it loses its connection with MySQL.
Error: Connection lost: The server closed the connection.
at Protocol.end (/home/s/prcrm/node_modules/mysql/lib/protocol/Protocol.js:109:13)
at Socket.<anonymous> (/home/s/prcrm/node_modules/mysql/lib/Connection.js:115:28)
at emitNone (events.js:91:20)
at Socket.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:975:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
DB Configuration
function DB( host, user, password, database ) {
this.host = host;
this.user = user;
this.password = password;
this.database = database;
this.connection = mysql.createConnection({
host: this.host,
user: this.user,
password: this.password,
database: this.database
});
this.connection.connect(function(err){
if(err){
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
Versions
Node.js: 6.4.0
Express: 4.13.4
MySQL: 10.0.29-MariaDB MariaDB Server
CentOS: 6.8 (Final)
Any suggestions or guidance of how to keep MySQL connection alive (without being quick&dirty) when server is idling, supposing that this causes the problem?

MongoError: connection 0 to localhost:27017 timed out

events.js:141
throw er; // Unhandled 'error' event
MongoError: connection 0 to localhost:27017 timed out
at Function.MongoError.create (/home/ubuntu/scripts/node_modules/mongodb-core/lib/error.js:29:11)
at Socket.<anonymous> (/home/ubuntu/scripts/node_modules/mongodb-core/lib/connection/connection.js:184:20)
at Socket.g (events.js:260:16)
at emitNone (events.js:67:13)
at Socket.emit (events.js:166:7)
at Socket._onTimeout (net.js:318:8)
at _runOnTimeout (timers.js:524:11)
at _makeTimerTimeout (timers.js:515:3)
at Timer.unrefTimeout (timers.js:584:5)
Well there is no error during connection, but when try to save some models/collections it runs for a while and then it throws this error.
BTW I also have another node process connected to the same mongodb server. Any help is highly appreciated.
const mongoose = require('mongoose');
const option = {
socketTimeoutMS: 30000,
keepAlive: true,
reconnectTries: 30000
};
const mongoURI = process.env.MONGODB_URI;
mongoose.connect(mongoURI, option).then(function(){
//connected successfully
}, function(err) {
//err handle
});
Your query is taking a long time. And mongo itself has a default time out set. So it times out, if the query takes longer than the timeout time.
you have to use this configs on your connection:
keepAlive: 300000, connectTimeoutMS: 30000
If you are sure that you exported port -p 27017:2017 and it still doesn't work.
Check your VPN if it is not blocking Local Network sharing.

Resources