MongoNetworkError: connect ETIMEDOUT - database

SOMETIMES when our team connect the mongodb database at localhost by these codes:
const mongoose = require('mongoose');
const CONN = 'mongodb://name:password#www.wecard.io:27017/myproject'
const option = {
socketTimeoutMS: 30000,
keepAlive: true,
reconnectTries: 30000
};
mongoose.connect(CONN, option).then(
() => { console.log('It is good: ' + CONN); },
(err) => {
console.error('Error: ' + err);
console.log(err);
}
);
ONE of our teammates get this error:
MongoNetworkError: failed to connect to server [www.website.com:27017] on first connect [MongoNetworkError: connect ETIMEDOUT 46.101.98.56:27017]
at Pool.<anonymous> (/Users/liubaobao/Desktop/WeCard/models/node_modules/mongodb-core/lib/topologies/server.js:505:11)
at emitOne (events.js:116:13)
at Pool.emit (events.js:211:7)
at Connection.<anonymous> (/Users/liubaobao/Desktop/WeCard/models/node_modules/mongodb-core/lib/connection/pool.js:329:12)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7)
at Socket.<anonymous> (/Users/liubaobao/Desktop/WeCard/models/node_modules/mongodb-core/lib/connection/connection.js:245:50)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'MongoNetworkError',
message: 'failed to connect to server [www.website.com:27017] on first connect [MongoNetworkError: connect ETIMEDOUT 46.101.98.56:27017]'
And at the same time if we connect it on cloud server, it has been always good so far. So anyone knows how to fix it? Thanks.

Related

Not Install Package On Atom Ide and not install themes too

C:\Users\ali aminaee\AppData\Local\atom\app-1.63.1\resources\app\apm\lib\featured.js:60
packages = body.filter(function(pack) {
^
TypeError: body.filter is not a function
at C:\Users\ali aminaee\AppData\Local\atom\app-1.63.1\resources\app\apm\lib\featured.js:60:27
at Request._callback (C:\Users\ali aminaee\AppData\Local\atom\app-1.63.1\resources\app\apm\lib\request.js:57:22)
at Request.self.callback (C:\Users\ali aminaee\AppData\Local\atom\app-1.63.1\resources\app\apm\node_modules\request\request.js:185:22)
at Request.emit (events.js:223:5)
at Request.<anonymous> (C:\Users\ali aminaee\AppData\Local\atom\app-1.63.1\resources\app\apm\node_modules\request\request.js:1154:10)
at Request.emit (events.js:223:5)
at IncomingMessage.<anonymous> (C:\Users\ali aminaee\AppData\Local\atom\app-1.63.1\resources\app\apm\node_modules\request\request.js:1076:12)
at Object.onceWrapper (events.js:312:28)
at IncomingMessage.emit (events.js:228:7)
at endReadableNT (_stream_readable.js:1185:12)
this massage for me when i want package install
thank you for help me
C:\Users\ali aminaee\AppData\Local\atom\app-1.63.1\resources\app\apm\lib\featured.js:60
packages = body.filter(function(pack) {
^
TypeError: body.filter is not a function
at C:\Users\ali aminaee\AppData\Local\atom\app-1.63.1\resources\app\apm\lib\featured.js:60:27
at Request._callback (C:\Users\ali aminaee\AppData\Local\atom\app-1.63.1\resources\app\apm\lib\request.js:57:22)
at Request.self.callback (C:\Users\ali aminaee\AppData\Local\atom\app-1.63.1\resources\app\apm\node_modules\request\request.js:185:22)
at Request.emit (events.js:223:5)
at Request.<anonymous> (C:\Users\ali aminaee\AppData\Local\atom\app-1.63.1\resources\app\apm\node_modules\request\request.js:1154:10)
at Request.emit (events.js:223:5)
at IncomingMessage.<anonymous> (C:\Users\ali aminaee\AppData\Local\atom\app-1.63.1\resources\app\apm\node_modules\request\request.js:1076:12)
at Object.onceWrapper (events.js:312:28)
at IncomingMessage.emit (events.js:228:7)
at endReadableNT (_stream_readable.js:1185:12)

Getting a timeout when i try to add data to mongodb (via mongoose)

I am new to MERN development and I have been trying to add the username, email and password of a user to database but i get this error(when i try to add user after connecting to the database from thunderclient in json format in vscode):
[nodemon] 2.0.19
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node ./index.js`
Example app listening on port 3000
connected to mongodb successfully
{ name: 'kashish', email: 'mail#mail.com', password: '1234' }
/home/alien/codes/webDev/react/inotebook/backend/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:151
const err = new MongooseError(message);
^
MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms
at Timeout.<anonymous> (/home/alien/codes/webDev/react/inotebook/backend/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:151:23)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7)
Node.js v18.7.0
[nodemon] app crashed - waiting for file changes before starting...
The code connects to the database successfully, but when i try to add the user (using thunder client before calling the api) it just shows me the given error here is the code which connects to the database and the one which saves data in database respectively:
const mongoose=require("mongoose")
const mongoUri="mongodb://localhost:27017"
const connectMongo=() => {
mongoose.connect(mongoUri,() => {
console.log("connected to mongodb successfully");
}).catch(error => handleError(error))
}
module.exports=connectMongo;
the code that adds user:
const express=require('express');
const User=require('../models/User');
const router=express.Router();
router.get('/',(req,res) => {
console.log(req.body);
const user=User(req.body);
user.save()
res.send(req.body);
})
module.exports=router;
I tried going through similar issues from the forum but i don't understand any of them, what am i missing?
another edit:
When i tried using async function and added await before user.save(), the response didn't occur, not sure if that is obvious but the user data shows up in console but there happens to be an error during saving data, is it possible that the timeout happens before the data can be saved? i am hosting mongodb on localhost so this is not an internet issue
here is the updated code for auth.js:
const express=require('express');
const User=require('../models/User');
const router=express.Router();
router.get('/',async (req,res) => {
console.log(req.body);
const user=User(req.body);
await user.save()
res.send(req.body);
})
module.exports=router;
updated code of db.js (connecting to database) with error handling:
const mongoose=require("mongoose")
const mongoUri="mongodb://localhost:27017"
const connectMongo=() => {
mongoose.connect(mongoUri).then(
() => console.log('connected')
).catch(e => console.log('error',e))
}
module.exports=connectMongo;
after the above update in code this shows up in a few mins:
Example app listening on port 3000
error MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at Connection.openUri (/home/alien/codes/webDev/react/inotebook/backend/node_modules/mongoose/lib/connection.js:824:32)
at /home/alien/codes/webDev/react/inotebook/backend/node_modules/mongoose/lib/index.js:380:10
at /home/alien/codes/webDev/react/inotebook/backend/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
at new Promise (<anonymous>)
at promiseOrCallback (/home/alien/codes/webDev/react/inotebook/backend/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
at Mongoose._promiseOrCallback (/home/alien/codes/webDev/react/inotebook/backend/node_modules/mongoose/lib/index.js:1225:10)
at Mongoose.connect (/home/alien/codes/webDev/react/inotebook/backend/node_modules/mongoose/lib/index.js:379:20)
at connectMongo (/home/alien/codes/webDev/react/inotebook/backend/db.js:5:14)
at Object.<anonymous> (/home/alien/codes/webDev/react/inotebook/backend/index.js:3:1)
at Module._compile (node:internal/modules/cjs/loader:1120:14) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
},
code: undefined
}
and before you ask, i already ran sudo systemctl start mongodb before starting the app. here is the output of sudo systemctl status mongodb:
● mongodb.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongodb.service; disabled; preset: disabled)
Active: active (running) since Mon 2022-08-22 14:59:01 IST; 24s ago
Docs: https://docs.mongodb.org/manual
Main PID: 5840 (mongod)
Memory: 163.0M
CPU: 857ms
CGroup: /system.slice/mongodb.service
└─5840 /usr/bin/mongod --config /etc/mongodb.conf
error MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
This issue can occur when the MongoDB client is trying to connect to the MongoDB server over IPv6, but the server isn't actually listening on IPv6 (for whatever reason; I think that older versions of the server had to be told to listen on IPv6 but more recent versions should do so automatically, unless it's explicitly turned off).
To force the client to connect over IPv4, use the IPv4 localhost address:
const mongoUri="mongodb://127.0.0.1:27017"

ConnectionError [SequelizeConnectionError]: Failed to connect to localhost:1433 - Could not connect (sequence) Node.js, Sequelize and Heroku

I am trying to deploy my application to Heroku, the problem is when my application is trying to connect to the local SQL Server database doesn't work.
When i test my application on development environment everything works good, the connection to the localhost:1433 works correctly.
This is my DB config
const { Sequelize } = require('sequelize');
// Connection to SQL Server
const db = new Sequelize('Cafeteria', process.env.SQL_NAME, process.env.SQL_PASS, {
host: 'localhost',
dialect: 'mssql',
// logging: false,
});
module.exports = db;
Connection in server.js
// Connection to DB
async dbConnection() {
try {
await db.authenticate();
console.log('Database online');
} catch (error) {
throw new Error( error );
}
}
And this is the error on console (Heroku logs)
(node:21) UnhandledPromiseRejectionWarning: Error:
SequelizeConnectionError: Failed to connect to localhost:1433 - Could
not connect (sequence) at Server.dbConnection
(/app/models/server.js:43:19) at processTicksAndRejections
(internal/process/task_queues.js:93:5) at processTicksAndRejections
(internal/process/task_queues.js:80:21) { code: 'ESOCKET' }
Yes the TCP/IP is Enabled, the TCP Port is 1433, TCP Dynamic Ports are a blank space.
The SQL Server Browser is running.
I hope anyone can help me!

PM2 and nextjs stop responding request after a few uptime

My current setup is nginx -> reverse proxy -> nextjs. I"m using pm2 with cluster mode for nextjs. The problem is after nextjs is up for like 10 mins, it's stop responding without a trace of error but this issue is resolved when I restart nextjs app with pm2 restart all. It looks like my nextjs app only live for 10 mins and after that I have to restart. So I have to run a cron job to do this.
pm2 log output
2020-01-14T09:40:02: PM2 log: App [npm:0] starting in -cluster mode-
2020-01-14T09:40:02: PM2 log: App [npm:0] online
2020-01-14T09:40:02: PM2 log: App [npm:1] starting in -cluster mode-
2020-01-14T09:40:02: PM2 log: App [npm:1] online
2020-01-14T09:40:02: PM2 log: App [npm:2] starting in -cluster mode-
2020-01-14T09:40:03: PM2 log: App [npm:2] online
2020-01-14T09:40:03: PM2 log: App [npm:3] starting in -cluster mode-
2020-01-14T09:40:03: PM2 log: App [npm:3] online
2020-01-14T09:40:03: PM2 log: App [npm:4] starting in -cluster mode-
2020-01-14T09:40:03: PM2 log: App [npm:4] online
2020-01-14T09:40:03: PM2 log: App [npm:5] starting in -cluster mode-
2020-01-14T09:40:03: PM2 log: App [npm:5] online
2020-01-14T09:40:03: PM2 log: App [npm:6] starting in -cluster mode-
2020-01-14T09:40:03: PM2 log: App [npm:6] online
2020-01-14T09:40:03: PM2 log: App [npm:7] starting in -cluster mode-
2020-01-14T09:40:03: PM2 log: App [npm:7] online
> Server started at http://localhost:3000 with development ? false
events.js:187
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1306:14)
at listenInCluster (net.js:1354:12)
at Server.listen (net.js:1442:7)
at /Users/username/Data/Projects/reactjs/ssr/server.js:90:8
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1333:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -48,
syscall: 'listen',
address: '::',
port: 3000
}
events.js:187
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1306:14)
at listenInCluster (net.js:1354:12)
at Server.listen (net.js:1442:7)
at /Users/username/Data/Projects/reactjs/ssr/server.js:90:8
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1333:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -48,
syscall: 'listen',
address: '::',
port: 3000
}
events.js:187
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1306:14)
at listenInCluster (net.js:1354:12)
at Server.listen (net.js:1442:7)
at /Users/username/Data/Projects/reactjs/ssr/server.js:90:8
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1333:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -48,
syscall: 'listen',
address: '::',
port: 3000
}
events.js:187
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1306:14)
at listenInCluster (net.js:1354:12)
at Server.listen (net.js:1442:7)
at /Users/username/Data/Projects/reactjs/ssr/server.js:90:8
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1333:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -48,
syscall: 'listen',
address: '::',
port: 3000
}
events.js:187
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1306:14)
at listenInCluster (net.js:1354:12)
at Server.listen (net.js:1442:7)
at /Users/username/Data/Projects/reactjs/ssr/server.js:90:8
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1333:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -48,
syscall: 'listen',
address: '::',
port: 3000
}
events.js:187
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1306:14)
at listenInCluster (net.js:1354:12)
at Server.listen (net.js:1442:7)
at /Users/username/Data/Projects/reactjs/ssr/server.js:90:8
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1333:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -48,
syscall: 'listen',
address: '::',
port: 3000
}
Though you did not mention using npm, I fixed a similar issue by removing usage of npm from the ecosystem.config.json file.
Before (not working):
{
"apps": [
{
...
"script": "/usr/bin/npm",
"args" : "start",
...
}
]
}
After (working):
{
"apps": [
{
...
"script": "./node_modules/.bin/next",
"args" : "start -p 3000",
...
}
]
}
I did not take time to dig into the issue with npm.
I found the solution here: express server port configuration issue with pm2 cluster mode

NodeJS connecting to sql server using node-mssql does not work

I use a named instance of SQL Server Express 2012
If I try to connect to it using SSMS it works, using these parameters:
Server name: mit-007\SQLEXPRESS2012
Authentication: SQL Server Authentication
Login: sa
Password: mit
Using node-mssql:
var sql = require('mssql');
var config = {
user: 'sa',
password: 'mit',
server: 'mit-007',
driver: 'tedious',
database: 'Delvi',
options: {
instanceName: 'SQLEXPRESS2012'
}
};
sql.connect(config).then(function(){ // and so on
It logs this error
{ [ConnectionError: Failed to connect to mit-007:undefined in 15000ms]
name: 'ConnectionError',
message: 'Failed to connect to mit-007:undefined in 15000ms',
code: 'ETIMEOUT' }
After browsing around I solved the problem, here's what I did
Open SQL Server Configuration Manager
Click SQL Server Network Configuration => Protocols for SQLEXPRESS2012
Double click TCP/IP
Change Enabled to Yes
Click IP Addresses
IPAll => Clear TCP Dynamic Ports, set TCP Port 1433
Open services.msc
Start SQL Server Browser Service
Restart SQL Server
I'm not sure that every single one of the steps above are necessary, but they worked for me
I think mit-007 is not your network adress.
https://msdn.microsoft.com/pl-pl/library/ms189921(v=sql.110).aspx
There is a flaw in the tedious driver logic that indicates that the port is "undefined" when running on a mac platform.
The program (testtedious.js) runs fine when using the following configuration settings on a MS Windows platform:
var connectionConfig = {
userName: 'xxx',
password: 'yyy',
database: 'zzz',
server: '127.0.0.1',
port: 1443,
debug: true,
driver: 'tedious',
options: {
encrypt: false,
instanceName: 'SQLEXPRESS',
database: 'www',
useColumnNames: false,
debug: {
packet: true,
data: true,
payload: true,
token: true,
log: true
}
}
};
However, when running on a mac I get the following error:
$ node testtedious.js
Tedious-Connection-Pool: filling pool with 2
Tedious-Connection-Pool: creating connection: 1
Tedious-Connection-Pool: creating connection: 2
Tedious-Connection-Pool: connection connected: 1
Tedious-Connection-Pool: connection closing because of error
{ ConnectionError: Failed to connect to 127.0.0.1:undefined in 15000ms
at ConnectionError (/project-dir/node_modules/tedious/lib/errors.js:12:12)
at Connection.connectTimeout (/project-dir/node_modules/tedious/lib/connection.js:467:28)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5)
message: 'Failed to connect to 127.0.0.1:undefined in 15000ms',
code: 'ETIMEOUT' }
Tedious-Connection-Pool: connection connected: 2
Tedious-Connection-Pool: connection closing because of error
{ ConnectionError: Failed to connect to 127.0.0.1:undefined in 15000ms
at ConnectionError (/project-dir/node_modules/tedious/lib/errors.js:12:12)
at Connection.connectTimeout (/project-dir/node_modules/tedious/lib/connection.js:467:28)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5)
message: 'Failed to connect to 127.0.0.1:undefined in 15000ms',
code: 'ETIMEOUT' }
Tedious-Connection-Pool: creating connection: 3
Tedious-Connection-Pool: creating connection: 4
Here's the "fix":
var connectionConfig = {
userName: 'xxx',
password: 'yyy',
database: 'zzz',
server: '127.0.0.1',
port: 1443,
debug: true,
driver: 'tedious',
options: {
port: 1443,
encrypt: false,
database: 'www',
useColumnNames: false,
debug: {
packet: true,
data: true,
payload: true,
token: true,
log: true
}
}
};
Note the movement of the port setting into options and the removal of the instanceName: 'SQLEXPRESS' option setting.

Resources