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
Related
I have been working on an small app and connecting with Heroku PostgreSQL, for many days was working right but now is showing me this SSL error
The server does not support SSL connections
I have been looking for solutions but I cannot find anything that works for me, my code is:
import pg from 'pg'
import db from '../config.js'
const pool = new pg.Pool({
host: db.host,
database: db.database,
user: db.user,
port: db.port,
password: db.password,
ssl: { rejectUnauthorized: false },
})
export default function query(text, params) {
return pool.query(text, params)
}
Try changing it from Pool to Client and then using that to connect and query.
async function get(){
const client = new pg.Client({
ssl: {
rejectUnauthorized: false
},
user: ...,
password: ...,
port: ...,
host: ...,
database: ...
});
client.connect();
const response = await client.query(`SELECT * FROM ...;`)
return response.rows
}
Double check your values in the Heroku database.
Also, in production, you should just need
const client = new pg.Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
},
}
The first set of code is for local connection to your db.
One last note, I would put your user, password, etc... into a .env file and don't commit that to your repo.
UPDATE:
You can also put this into a config file like ./db.config.js as the following
const pg = require('pg')
module.exports =
process.env.DATABASE_URL
?
new pg.Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
},
})
:
new pg.Client({
// connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
},
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
port: process.env.DATABASE_PORT,
host: process.env.DATABASE_HOST,
database: process.env.DATABASE
})
So if it is in production, it will use the database url, and if there is none (which is local) then it will use the username password connection.
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%
I am trying to establish a fixed connection pool using the typeorm for postgres in my rest service.
let option = {
name: 'default',
type: 'postgres',
host: 'host',
port: port,
username: 'username',
password: 'password',
database: 'database',
synchronize: true,
dropSchema: false,
logging: true,
ssl: {
ca: process.env.SSL_CERT,
},
entities: ['src/**/*.entity.ts'],
}
In app.ts
await createConnection(option).then(() => {
logger.debug('Database connection established');
}).catch((error: any) => {
logger.error('Database connection failed', error);
throw new Error(error);
});
When the app gets initialised the connectionpool gets established via ssl but it then closes, every time a query is executed the connection is created and then closed.
But without the ssl the connectionpool does not close until the .close method is called.
It would be helpful if someone could let me know on how to prevent the connectionpool from closing.
Or is there an option to set the time to live in ssl connection for postgres but I couldn't find reference for that in typeorm documentation.
I am trying to connect with MongoDB by mongoose. Everything was ok, when I was connecting with my local db where there is no authentication.
When I've tried to connect to other DB with set admin user and credentials, I've got error and I've tried various different options but without any positive result.
I use these versions:
"mongodb": "^3.3.2",
"mongoose": "^5.7.1"
And my server side technology is node.js
I've tried these options:
const connection = await mongoose.connect(`mongodb://${host}:${port}/${db}?authSource=admin`,
{ useNewUrlParser: true, useUnifiedTopology: true });
then I've tried this:
let options = {
"auth": { "authSource":"admin"},
"user": "SVSAdmin",
"pass":"8&PG2DCUuDPvy$hx",
"useUnifiedTopology": true,
"useNewUrlParser": true
};
const connection = await mongoose.connect(`mongodb://${host}:${port}/${db}, options);
and this:
mongoose.connect('mongodb://${user}:${pass}#${uri}/${db}?authMechanism=SCRAM-SHA-1')
mongoose.connect('mongodb://${user}:${pass}#${uri}/${db}?authMechanism=MONGODB-CR')
and also this:
mongoose.connect('mongodb://user:password#host/yourDB?authSource=admin&w=1')
but it does not work. My credentials are ok.
The error message is:
{
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {}
}
Maybe important thing is that I'm connecting with db by ssh
I would be grateful for any help.
If u are using ur database remotly then u can use it via IP.
db = mongodb://52.221.52.32/DataBaseName
I upgrade sails to 1.0, I resolved all the other errors but not able to resolved database connection issue,
It will be really helpful if anyone can reply on this.
module.exports.datastores = {
localDiskDb: {
adapter: 'sails-disk'
},
postgreSql: {
adapter: 'sails-postgresql',
// url: 'postgresql://admin:root#localhost:5432/testdb',
// ssl: true,
host: 'localhost',
user: 'admin',
password: 'root',
database: 'testdb'
},
};
Important Error Logs
I was using password which contains %
after changing the password sails successfully connected to postgresql.
It mostly looks like bug with Sails v1 or Sails-pgsql adapter.