Establish a connection between the raspberry and mongodb - node.js

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)

Related

PostGres Timeout error in AWS EC2 Instance (Production)

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

mongoose atlas connection fails while mongodb driver works

I need to connect to an Atlas replica set with mongoose. It's really weird. If I connect via the mongodb-js driver everything works fine. CRUD works like a charm.
Here's how I connect via MongoClient:
let dbURL = 'myAtlasConnectionURL';
let db = await MongoClient.connect(dbURL);
await db.collection('users').insert({name: 'username'});
let result = await db.collection('users').find({});
log.info('result', await result.toArray());
db.collection('users').remove({});
await db.close();
But if use the mongoose connection I keep getting:
MongoError: connection 5 to cluster0-shard-00-02-c4nst.mongodb.net:27017 closed
at Function.MongoError.create (/mypath/vendors/node_modules/mongodb-core/lib/error.js:29:11)
at Socket.<anonymous> (/mypath/vendors/node_modules/mongodb-core/lib/connection/connection.js:202:22)
at Object.onceWrapper (events.js:316:30)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at TCP._handle.close [as _onclose] (net.js:548:12)
Here's how I try to connect via mongoose, using the same dbURL as above:
mongoose.connect(dbURL, {
useMongoClient: true
});
I have no idea, what could possibly be wrong.

Connect multiple mongodbs, running on different aws servers

I am new to nodejs and I am facing an issue when I try to connect multiple mongodbs, running on different aws servers.
I have used tunnel-ssh but it shows failed to connect.
Can anyone provide me some information on how to connect multiple mongodbs, running on different aws servers within a single nodejs application?
const fs=require('fs')
var mongoose = require('mongoose');
var tunnel = require('tunnel-ssh');
var config = {
username:'ubuntu',
host:'my.ip.address',
agent : process.env.SSH_AUTH_SOCK,
privateKey:require('fs').readFileSync('C:\Users\\Ronit\\Downloads\\my.ppk'),
port:19735,
dstHost:'Database server Ip address',
dstPort:19735,
localHost:'127.0.0.1',
//password:'mypassword',
localPort: 19735,
keepAlive:true
};
var server = tunnel(config, function (error, server) {
if(error){
console.log("SSH connection error: " + error);
}
mongoose.connect('mongodb://localhost:19735/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function() {
// we're connected!
console.log("DB connection successful");
});
});
events.js:141
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED my_ip:19735
at Object.exports._errnoException (util.js:873:11)
at exports._exceptionWithHostPort (util.js:896:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1077:14)

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?

MongoDB mLab mongoose Node.js driver - connection timeout after a period of idle time?

I have a simple Node.js that uses mongoose to connect with Mongo database hosted on mLab.
Everything seems working just fine: adding new records, querying for existing stuff.
Only sometimes, after some period of inactivity, when I look at the console I see the following:
events.js:160
throw er; // Unhandled 'error' event
^
Error: connection timeout
at Db.<anonymous> (___PATH___/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:168:17)
at emitTwo (events.js:106:13)
at Db.emit (events.js:191:7)
at Server.listener (___PATH___/node_modules/mongodb/lib/db.js:1786:14)
at emitOne (events.js:96:13)
at Server.emit (events.js:188:7)
at Server.<anonymous> (___PATH___/node_modules/mongodb/lib/server.js:274:14)
at emitOne (events.js:96:13)
at Server.emit (events.js:188:7)
at Pool.<anonymous> (___PATH___/node_modules/mongodb-core/lib/topologies/server.js:334:12)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (___PATH___/node_modules/mongodb-core/lib/connection/pool.js:270:12)
at Connection.g (events.js:292:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
Right now it doesn't matter that much to me - I can always restart the app. I'm worried that in production it will cause a lot of headaches so I preemptively ask what's the issue here?
Note that initially everything is working fine, it after some time when I get Error: connection timeout
Sample Connection code for Mongo-Node using Mongoose
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
const options = {
useMongoClient: true,
reconnectTries: 5000,
reconnectInterval: 0,
socketTimeoutMS: 100000,
connectTimeoutMS: 100000
}
mongoose.connect(mongodb://........, options);
var db = mongoose.connection;
db.on('error', function (error) {
console.log('Error while connecting to mongodb database:', error);
});
db.once('open', function () {
console.log('Successfully connected to mongodb database');
});
db.on('disconnected', function () {
//Reconnect on timeout
mongoose.connect(mongodb://........, options);
db = mongoose.connection;
});
it seems that there is fluctuation in your internet connection. Maybe this is the reason of connection timeout.
you can handle it by setting the timeout.
As below :
var timeout = require('connect-timeout');
app.use(timeout('5s'));`
in your app.js file
Replace your mongoose connection setting by this code:
var mongoose = require('mongoose');
/*
* Mongoose by default sets the auto_reconnect option to true.
* We recommend setting socket options at both the server and replica set level.
* We recommend a 30 second connection timeout because it allows for
* plenty of time in most operating environments.
*/
var options = { server: { socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 } },
replset: { socketOptions: { keepAlive: 300000, connectTimeoutMS : 30000 } } };
var mongodbUri = 'mongodb://user:pass#host:port/db';
mongoose.connect(mongodbUri, options);
var conn = mongoose.connection;
conn.on('error', console.error.bind(console, 'connection error:'));
conn.once('open', function() {
// Wait for the database connection to establish, then start the app.
});

Resources