Sails-mongo automatic failover - mongodb

I'm using sails-mongo in my nodejs application, and my config looks like this:
mongoDb: {
adapter: 'sails-mongo',
url: 'mongodb://prod_user:prod_password#router-1-incloud:16888,router-2-incloud:16888/db_name',
auto_reconnect: true,
poolSize: parseInt(process.env.MONGO_POOL_LIMIT) || 10,
w: parseInt(process.env.MONGO_WRITE_CONCERT_INT) || 'majority',
reconnectInterval: 200,
wtimeout: parseInt(process.env.MONGO_WRITE_TIMEOUT_MS) || 2000,
retryMiliSeconds: 200,
numberOfRetries: 3,
readPreference: process.env.MONGO_READ_PREFERENCE || 'primaryPreferred',
socketOptions: {
noDelay: true,
keepAlive: 0,
connectTimeoutMS: parseInt(process.env.MONGO_CONNECT_TIMEOUT_MS) || 2000,
socketTimeoutMS: parseInt(process.env.MONGO_SOCKET_TIMEOUT_MS) || 2000
}
So when router-1-incloud:16888 goes down driver doesn't switch to router-2-incloud:16888 even when I restart my application, it always needs both routers to be available or it won't establish connection to mongo. Is there a config value I need to adjust so it automatically switches to any available host?

Related

what is mongoose connection validity time?

I have custom circuit breaker logic for 3rd party services. I checked mongoose connection status like below,
mongoose.connection.on("connected", function() {
console.log("Mongoose connected");
});
mongoose.connection.on("disconnected", function() {
console.log("Mongoose disconnected");
});
while running on server logs like below,
Mongoose disconnected //Happened 12.30PM
// after sometime
Mongoose connected //Happened 12.35PM
Mongoose disconnected //Happened 12.35PM
Mongoose connected //Happened 12.35PM
My connection string,
mongoose.connect(URL, {
keepAlive: true,
keepAliveInitialDelay: 300000,
socketTimeoutMS: 300000,
poolSize: 10,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
})
I dont know while connect why system act like above in same second of time interval ?
Why mongo connection goes to disconneted mode even i apply "keepAlive" ?
Is there any validity for mongoose connection alive status?

Stuck with mongodb connection in DigitalOcean app platform

I'm stuck in connecting my Nestjs server to the MongoDB database both are on the same app platform in the digital ocean. The main problem is in providing the ca.certificate.crt file to the Nestjs server. I'm using MongoDB with typeorm and nestjs.
const mongodb_config: ConnectionOptions = {
type: "mongodb",
url: `${getValue("MONGO_URL")}/${MONGODB_DATABASE}`,
name: DB_CONNECTION.MONGO_CONNECTION,
entities: [LivePriceEntity],
logging: ENVIRONMENT === ENVIRONMENTS.LOCAL,
useUnifiedTopology: true,
useNewUrlParser: true,
ssl: ENVIRONMENT === ENVIRONMENTS.LOCAL ? false : true,
sslValidate: ENVIRONMENT === ENVIRONMENTS.LOCAL ? false : true,
sslCert: ENVIRONMENT === ENVIRONMENTS.LOCAL ? "" : SSL_CA_CERT,
cli: {
entitiesDir: "src/app/**/entities/*.entity{.ts,.js}",
}
};
This is my typeorm connection config. Please help me out.

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 in mac [duplicate]

I've been trying for over 2 hours now trying to figure out what's wrong with this database. I've tried everything. From reinstalling the server, restarting the processes, rebooting and so much more. It keeps giving me this error when trying to connect:
const serverSelectionError = new ServerSelectionError();
^
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at NativeConnection.Connection.openUri (D:\TheShed\MX_\node_modules\mongoose\lib\connection.js:797:32)
at D:\TheShed\MX_\node_modules\mongoose\lib\index.js:330:10
at D:\TheShed\MX_\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (D:\TheShed\MX_\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (D:\TheShed\MX_\node_modules\mongoose\lib\index.js:1151:10)
at Mongoose.connect (D:\TheShed\MX_\node_modules\mongoose\lib\index.js:329:20)
at module.exports (D:\TheShed\MX_\other\DB\mong.js:4:20)
at D:\TheShed\MX_\app.js:195:37
at Object.<anonymous> (D:\TheShed\MX_\app.js:197:3) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'localhost:27017' => ServerDescription {
_hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 },
address: 'localhost:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 1421094,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (D:\TheShed\MX_\node_modules\mongodb\lib\cmap\connect.js:293:20)
at Socket.<anonymous> (D:\TheShed\MX_\node_modules\mongodb\lib\cmap\connect.js:267:22)
at Object.onceWrapper (node:events:510:26)
at Socket.emit (node:events:390:28)
at emitErrorNT (node:internal/streams/destroy:164:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
}
}
This error wont resolve no matter what I do. The MongoDB server is running, I've checked by doing >show dbs and it lists them perfectly fine. Also, C:/data/db exists and its fine too.
What do I do?
Here's my connection code:
(async () => {
await require('./other/DB/mong')();
console.log("Connected to Database.");
})();
and here's ./other/DB/mong:
var mongoose = require("mongoose");
module.exports = async () => {
await mongoose.connect('mongodb://localhost:27017/MX', {
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true
});
return mongoose;
}
I just found a solution in the internet ,
If you are using latest nodejs (v17.x) , then try updating mongodb url from localhost to 127.0.0.1
This worked for me :slightly_smiling_face:
Updated my mongodb url from 'mongodb://localhost:27017/student' to 'mongodb://127.0.0.1:27017/student' and it worked fine for me
MongoDB does not bind to localhost on ipv6 by default.
If you want it to listen on ::1 you will need to enable net.ipv6 and either enable net.bindIpAll or explicitly list the loopback address in net.bindIp
update from
'mongodb://localhost:27017/myapp' to
'mongodb://127.0.0.1:27017:27017/myapp'
then check to be sure it connects.
mongoose
.connect(process.env.DATABASE, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('DB Connected'));
const options = {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 5000,
autoIndex: false, // Don't build indexes
maxPoolSize: 10, // Maintain up to 10 socket connections
serverSelectionTimeoutMS: 5000, // Keep trying to send operations for 5 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
family: 4 // Use IPv4, skip trying IPv6
}
use mongoose.connect("mongodb://127.0.0.1:27017/databasename"); instead of localhost
just change this, mongoose.connect('mongodb://localhost:27017/Database-name');
---> mongoose.connect("mongodb://127.0.0.1:27017/Database-name");
If you are using latest nodejs. If connecting fails on your machine, try using 127.0.0.1 instead of localhost like
mongoose.connect('mongodb://127.0.0.1:27017/myapp');
you can also use mongodb://0.0.0.0:27017/database_name
step 1. check MongoDB service is running -> if OK.
try next:
step 2. replace 127.0.0.1 from localhost -> this should work 100%

Moongose ready state and aws lambda - cache connection

I am trying to implement a connect to database function that uses mongoose (MongoDB), and AWS lambda functions. Best practice, as I understand it, is to cache the database so that the connection can be reused. This, I have working. However, the problem is that the mongoose.readyState variable uses some fixed amount of time to determine if the connection is valid or not.
I was wondering if there is an alternative to .readyState to check if the connection is valid. I know that I can try to run a random query against the database every time I want to reuse the connection. Thus, I could determine if an exception happens or not. However, this feels a little hackish.
Here is my connect to database function:
let cacheDb = null
let options = {
useMongoClient: true,
autoIndex: true,
autoReconnect: true,
keepAlive: true,
socketOptions: {
keepAlive: true,
autoReconnect: true,
connectTimeoutMS: 30000
},
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 200,
poolSize: 1,
bufferMaxEntries: 0,
};
mongoose.Promise = global.Promise
module.exports = {
connectToDatabase: function connectToDatabase(context) {
context.callbackWaitsForEmptyEventLoop = false
if (cacheDb && mongoose.connection.readyState == 1) {
return cacheDb
}
else {
mongoose.connect(process.env['MONGODB_URI'], options, function(error){
if(error){
console.log(error.toString())
}
cacheDb = mongoose.connection
return cacheDb
})
}
}
}
Do you have any idea as to an alternative approach?
In order to check for a valid connection , you should be checking your cached db serverConfig.isConnected method.
like so:
if (cachedDb && cachedDb.serverConfig.isConnected()) { ...
This is according to mongodb, in this helpful article:
https://www.mongodb.com/blog/post/optimizing-aws-lambda-performance-with-mongodb-atlas-and-nodejs

Mongo w/ Mongoose on Express connection closes

My connection to my mongo database will close or timeout if left in active. I'm getting the following error when I leave my app in active "no open connections"
My mongo DB is running in a replication set up on AWS. I'm using the following options when connecting using mongoose. I'm unsure if any other flags should be set. I was basing my options on teh monodo node driver doc # http://mongodb.github.io/node-mongodb-native/api-generated/server.html.Users will be in my applicaiton for 8+ hours at a time and I don't want anything to timout when they go to lunch or leave for a meeting.
MongoOptions : {
user: 'root',
pass: '********',
replset: {
auto_reconnect: true,
poolSize: 25,
socketOptions: { keepAlive: 1 },
ssl: true,
sslCert: fs.readFileSync('./server/config/ssl/mongodb-cert.crt'),
sslKey: fs.readFileSync('./server/config/ssl/mongodb-cert.key')}
}
mongoose.connect('mongodb://server.com:27017', config.MongoOptions);
The connection getting closed turned out to be another issue that was causing the problem.
We have a replicate set running w/ SSL and are able to keep the connection up for days using the following config:
MongoOptions : {
user: 'ssssss',
pass: 'xxxxxx',
replset: {
auto_reconnect: false,
poolSize: 10,
socketOptions: { keepAlive: 1 },
ssl: true,
sslCert: fs.readFileSync('./server/config/ssl/mongodb-cert.crt'),
sslKey: fs.readFileSync('./server/config/ssl/mongodb-cert.key')}
}
Try adding
connectTimeout: 43200000
that should be good for 12 hours