I'm using mongoose to connect to my MongoDB server on production.
mongo : {
uri : process.env.MONGO_URL,
options: {
user : process.env.MONGO_USER,
pass : process.env.MONGO_PASS,
server : {
socketOptions: {
keepAlive : 1,
socketTimeoutMS : 300000,
connectTimeoutMS: 300000
}
},
replset: {
rs_name : process.env.MONGO_RS_NAME || "set-name",
socketOptions: {
keepAlive : 1,
socketTimeoutMS : 300000,
connectTimeoutMS: 300000
},
readPreference: 'secondaryPreferred',
poolSize: 50
}
}
}
I've set tcp_keepalive_time to 120:
echo 120 | sudo tee /proc/sys/net/ipv4/tcp_keepalive_time
The problem is the connections keep opening to the server but never closing.
Why are connections keep opening to the server?
Why are connections not being closed?
Related
currently i'am using tedious package to connect to the database and do operations but i would like to switch to node-mssql (seems less messy).
The problem i'm getting is connection timeout:
originalError: ConnectionError: Failed to connect to yyy:1433 in 15000ms
code: 'ETIMEOUT',
isTransient: undefined
}
My config with tedious :
const config = {
server: process.env.HOST, // update me
authentication: {
type: 'default',
options: {
userName: process.env.USER, // update me
password: process.env.PASS, // update me
},
},
options: {
// If you are on Microsoft Azure, you need encryption:
database: process.env.DB,
rowCollectionOnDone: true, // update me
},
};
My config with mssql :
const configMssql = {
user: process.env.USER,
password: process.env.PASS,
server: process.env.HOST, // update me
database: process.env.DB,
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000,
},
options: {
encrypt: false, // for azure
trustServerCertificate: false, // change to true for local dev / self-signed certs
},
};
or
const configMssqlString = `Server=${process.env.HOST},1433;Database=${process.env.DB};User Id=${process.env.USER};Password=${process.env.PASS};Encrypt=false`;
Can't figure out whats wrong
I have used all the options to use local time zone instead of UTC but not work properly as my database connection below any body help to solve this.
const sequelize = new Sequelize(process.env.POSTGRES_DBNAME, process.env.POSTGRES_USERNAME, process.env.POSTGRES_PASS, {
dialect: 'postgres',
logging: false,
host: process.env.POSTGRES_HOST,
port: process.env.POSTGRES_PORT,
pool: {
max: Number(process.env.MAX_DB_CONNECTIONS) || 100,
min: Number(process.env.MIN_DB_CONNECTIONS) || 50,
},
dialectOptions: {
useUTC: true, // -->Add this line. for reading from database
},
timezone: "Asia/Karachi"
});
MongoClient.connect('mongodb://mongo1,mongo2,mongo3?replicaSet=rs', {
useUnifiedTopology: true,
}, (err, mongoClient) => {
setTimeout(() => {
mongoClient.db("mydb").collection('mycollection').insertOne({...})
}, 10000)
})
Should MongoClient reconnect to another mongo instance in a replicaSet when the primary goes down and when using the UnifiedTopology? Because in this example mongoClient still points to the old primary instance. Is this the intended behavior?
EDIT
Replica Set config
Try using **mongoose** as such :
if(config.db.indexOf('replicaSet') > - 1) {
dbOptions = {
db: {native_parser: true},
replset: {
auto_reconnect:false,
poolSize: 10,
socketOptions: {
keepAlive: 1000,
connectTimeoutMS: 30000
}
},
server: {
poolSize: 5,
socketOptions: {
keepAlive: 1000,
connectTimeoutMS: 30000
}
}
};
}
var db = mongoose.connect(config.db, dbOptions);
where
config.db = 'mongodb://USER:PW#host1:port1,host2:port2/DBNAME?replicaSet=RSNAME'
More elaboration :
The "server" block is only invoked when the connection URI contain a single non clustered connection (aka a single connection string).
The "replset" block is only invoked when the connection URL contains a list of comma separated connection strings (aka replication set).
var options = {
db: {
native_parser: true
},
// This block gets run for a non replica set connection string (eg. localhost with a single DB)
server: {
poolSize: 5,
reconnectTries: Number.MAX_VALUE,
ssl: false,
sslValidate: false,
socketOptions: {
keepAlive: 1000,
connectTimeoutMS: 30000
}
},
// This block gets run when the connection string indicates a replica set (comma seperated connections)
replset: {
auto_reconnect: false,
poolSize: 10,
connectWithNoPrimary: true,
ssl: true,
sslValidate: false,
socketOptions: {
keepAlive: 1000,
connectTimeoutMS: 30000
}
}
};
This block worked on both localhost and the production env.
According to Mongo 3.4 documentation:
In 3.4:
mongo "mongodb://host1,host2,host3/myDB?replicaSet=myRS"
or
mongo "mongodb://host1:27017,host2:27017,host3:27017/myDB?replicaSet=myRS"
Turns out the actual problem was a bad configuration
The 3rd node had priority 0 and votes 0 instead of 1
{
"_id" : 2,
"host" : "mongo3:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 0
}
I have a question about sequelize and sql server.
So I can connect to my database with "localhost" or name of my computer, but i cant to "(LocalDB)\MSSQLLocalDB". This is my connection parameter.
PASSWORD: "sw",
DB: "BusinessDB",
CONFIG: {
host: '(LocalDB)\\MSSQLLocalDB',
dialect: 'mssql',
dialectOptions: {
options: {
encrypt: true,
}
},
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
define:{
timestamps: false,
}
}
```
And this is the error when i'm trying to connect with this config
> Failed to connect to (LocalDB)\MSSQLLocalDB:1433 - getaddrinfo ENOTFOUND (LocalDB)\MSSQLLocalDB
Someone have the solutions for that. I search on google but I doesn't find a solution.
Thanx
I found a solution with the help of "msnodesqlv8" module.
Now i'm using this configuration for connect to my DB.
dialect: 'mssql',
dialectModule: require('msnodesqlv8/lib/sequelize'),
bindParam: false,
/*logging: false,*/
dialectOptions: {
options: {
connectionString: 'Driver={ODBC Driver 17 for SQL Server};Server= (LocalDB)\\MSSQLLocalDB;Database=MyDB;Trusted_Connection=yes;',
},
},
define:{
timestamps: false,
}
The driver version can be found on ODBC data sources software.(type in windows search bar)
From what I have read there are 3 options : "ping" and "strategy" and a default as null.
Can someone explain what are the differences between them ?
I am using secondary preferred so whom should I pick based the fact all my servers are in the same data center ?
When I chose "ping" I see that most of the reading goes only to one secondary (and I have more then 1)
So what am I missing ?
Here is my configuration :
dbOptions: {
db: {
readPreference: 'secondaryPreferred'
},
server: {
socketOptions: {
keepAlive: 1000,
connectTimeoutMS: 30000
},
readPreference: 'secondaryPreferred',
strategy: 'ping'
},
replset: {
socketOptions: {
keepAlive: 1000,
connectTimeoutMS: 30000
},
rs_name: 'test',
readPreference: 'secondaryPreferred',
strategy: 'ping'
}
}