mongoose readPreference in options object? - node.js

right now i am connecting to mongodb like this:
uri = 'mongodb://mongodomain1.com:1234,mongodomain2.com:1234/mydb?replicaSet=replicasetname&readPreference=secondaryPreferred';
opts = {
user: 'myuser',
pass: 'mypass',
server: {
auto_reconnect: true,
socketOptions: {
keepAlive: 120
}
},
replset: {
socketOptions: {
keepAlive: 120
}
}
};
mongoose.connect(uri, opts);
now i would like to bring the readPreference url parameter into the options object to be consitent and have all connection variables in the options.
i am struggling to find documentation on how to define the readPreference in the object.
same thing the replicaSet parameter in the url. is that equivalent to the rs_name
is there anything else i could/should improve in the way i am connecting to mongodb?

Related

Mongoose throwing `Authentication failed`

I am using Mongoose Ver:4.3
I am able to connect to mongoDb using Studio3T mongo client.
But getting an authentication error when trying to connect using Mongoose within NodeJs.
My code looks as below:
const sslOptions = {
server: {
sslValidate: false,
ssl: true,
sslCert: certFileBuf,
sslKey: certFileBuf,
checkServerIdentity: false,
sslPass: 'mysslPass',
},
user: 'myUser',
pass: 'myPass'
};
connName = mongoose.createConnection("mongodb://server.com:27017/db1?tls=true&authSource=db1", sslOptions);
The above throwing an error:
mongodb\node_modules\mongoose\node_modules\mongodb\lib\utils.js:98
process.nextTick(function() { throw err; });
^
Error [MongoError]: Authentication failed.
set username and password in url
connName = mongoose.createConnection("mongodb://myUser:myPass#server.com:27017/db1?tls=true&authSource=db1", {
sslValidate: false,
ssl: true,
sslCert: certFileBuf,
sslKey: certFileBuf,
checkServerIdentity: false,
sslPass: 'mysslPass',
});

Node.js mssql error: tedious deprecated The default value for `config.options.enableArithAbort` will change from `false` to `true`

I get the error below. How do I fix it?
tedious deprecated The default value for `config.options.enableArithAbort` will change from `false` to `true` in the next major version of `tedious`. Set the value to `true` or `false` explicitly to silence this message. node_modules\mssql\lib\tedious\connection-pool.js:61:23
Change your database config options to the following:
var config = {
user: 'username',
password: 'password',
server: 'localhost',
database: 'databasename',
"options": {
"encrypt": true,
"enableArithAbort": true
}
};
read issue details here: https://github.com/tediousjs/node-mssql/issues/976
The following worked for me :
const config = {
user: 'sa',
password: '<YOUR_PASSWORD>',
server: '<COMPUTER_NAME>\\SQLEXPRESS',
database: '<DBNAME>',
requestTimeout: 180000, // for timeout setting
connectionTimeout: 180000, // for timeout setting
"options": {
"encrypt": false, // need to stop ssl checking in case of local db
"enableArithAbort": true
}
}
According to tedious docs
and
SET ARITHABORT
enableArithAbort: true // Ends a query when an overflow or divide-by-zero error occurs during query execution.
encrypt: true, // A boolean determining whether or not the connection will be encrypted. Set to true if you're on Windows Azure.
instead of setting in config in your project, set the value in node_modules
node_modules/sequelize/lib/dialects/mssql/connection-manager.js.
options: {
enableArithAbort: true,//<----------set this to true
port: parseInt(config.port, 10),
database: config.database,
trustServerCertificate: true
}

Mongoose Replicaset Connection Not Working

I am trying to connect to AWS MongoDB instances and its not even attempting to connect:
This is the connection URI:
mongodb://<usr>:<pwd>#host:port,host2:port2/db
And this is the code I am using to connect.
var mongoose = require('mongoose');
var dbOptions = { server: { socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 } },
replset: { socketOptions: { keepAlive: 300000, connectTimeoutMS : 30000 } } };
mongoose.connect(uri, dbOptions);
I also added logs inside events.. none of the events are firing. If I remove host2:port2 it works. So issue seems to be with the connection string. But connection string looks totally fine.. but still its not working.. Any idea why?

multiple connection to mongodb in node.js

In below code we can connect MongoDB
var options = {
db: { native_parser: true },
server: { poolSize: 5 },
replset: { rs_name: 'myReplicaSetName' },
user: 'myUserName',
pass: 'myPassword'
}
mongoose.connect(uri, options);
Here poolSize is 5. So that 5 parallel connection can be perform on request.
But I see if we try to create second connect node gives error that I'm trying to create connection which is not closed. So at the same time one connection can do perform for one application.
So what is meaning of poolSize is 5 and how it perform?
I need a solution and a way to increase pool size when my system is scale up.
Thanks in advanced.
Mongoose (or rather the mongodb driver it uses) will automatically manage the number of connections to the MongoDB server. You should call mongoose.connect() just once.
If you need a larger number of connections, all you have to do is increase the poolSize property. However, since you're using a replicate set, you should set replset.poolSize instead of server.poolSize:
var options = {
db: { native_parser: true },
replset: { rs_name: 'myReplicaSetName', poolSize : POOLSIZE },
user: 'myUserName',
pass: 'myPassword'
}

mongo read from slave with primaryPreferred and mongoose

i've got a replica set on compose.io.
I've setted primaryPreferred on the config which is fine.
{ replset: {
rs_name: 'set-53453xxxxxxxxx',
strategy: 'ping',
read_secondary: true,
readPreference: 'primaryPreferred',
slaveOk: true,
safe: true,
socketOptions: { keepAlive: 1 }
}}
But i'd like to read on secondary for background process such as stats.
should i open 2 connections ? one for primary, and one for slave ?
that's what i did. But when connecting on the slave i get a not master and slaveOk=false when querying.
i know that i can rs.slaveOK() on the mongo client to allow query but i need it in my app connection
this is how i init my slave connection:
var slaveCon = mongoose.createConnection()
slaveCon.open('slaveURL', { slaveOk: true }, callback)
// this give me the notMaster error when querying
any ideas ?

Resources