When I run my MongoDB connection with my node.js application which gaming platform here I facing the problem of an increasing number of MongoDB connections [Without using any query but its increasing repeatedly] which reaches to 819 and my MongoDB replication server stop responding and indirectly application stop the work. But I want to maintain a minimum of 20 connections on how to solve these issues please help me.
**
Mongodb connection: const connectionString =
'mongodb://AAAA:PASSWORD#HOST1:27001,HOST2:27002,HOST3:27003/dbName?replicaSet=rep1';
MongoClient.connect(connectionString, function (err, database) {
if
(err) {
console.log(err);
console.log("unable to connect Mongodb database on url: " + connectionString);
} else {
dbs['dashboardLog'] = database;
console.log("Mongodb database connected to server on url: " + connectionString); } });
**
I am using MongoDB client driver version: 3.4.23
Node.js: version 6.11
Fade-up with this issue please helps thanks in advance!!!
MongoClient already comes with connection pool support, just set the value you want for poolSize, if I remember correctly the default is 5 so set it to 20. You don't have to create a new connection everytime you want to run a new query. Just connect once and then keep that around.
See this article for more information https://blog.mlab.com/2017/05/mongodb-connection-pooling-for-express-applications/
Related
Am quiet new to backend and database. I have a solution currently using mongodb. It was working fine till yesterday when i starting having the connect ETIMEDOUT 13.37.254.237:27017 error. Nothing was changed in the URI path or tampered with. It just started and i have not been able to sort it out.
is there any help available please?
I have created another cluster and its working well. But my initial cluster that has datas which are live from clients is not connecting still.
My connection code
I have used these connections code but it has not worked. It was connecting fine all through yesterday but today without tampering with the code, couldn't connect to my mongodb
mongoose.connect(process.env.MONGO_URI,{ useNewUrlParser: true, useUnifiedTopology: true });
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URL);
console.log(`MongoDB Connected: ${conn.connection.host}`);
} catch (error) {
console.log(error);
process.exit(1);
}
};
my mongoose connection and the timedout error
Whenever you connect to Mongodb using IPs that keep updating from your system causes this kind of issues.
also this can be due to your network connection. So i will advice you to:
To allow connection from any IP address(but must ensure your URI is not made known to the public to avoid attack/ access from unwanted users.)
2.Check your network status(data)
3. Run the mongo URI on your atlas
I'm always connecting to the "admin" DB, which is a fixed bug.
Using Mongoose 5.0.6 MongoDb 3.6 and trying to connect to Atlas.
My question, what driver Mongoose 5.0.6 depend on?
How can I find out when Mongoose will have that fix?
On a different direction, is there a way to connect with MongoDB then use this connection with
Mongoose?
Cheers
Basically you should try connecting with your url link, and specify the DB name on the mongoose connect method
so if your cluster link is:
mongodb+srv://userName:Passwrod#clustor.mongodb.net/
and your DB name is:
testDB
then you should call the mongoose.connect method as follows:
mongoose.connect('mongodb+srv://userName:Passwrod#cluster.mongodb.net/', {dbName: 'testDB'});
Connection is established when you use this connection string for (MongoShell 3.6+) :
var connDB = "mongodb+srv://<username>:<password>#cluster-fax0w.mongodb.net/test"
However, you will not be able to read/write data without entering the DBName in the mongoose.connect().
mongoose.connect(uri, { dbName: <your DB name> })
.then( () => {
console.log('Connection to the Atlas Cluster is successful!')
})
.catch( (err) => console.error(err));
Just a side note if you ever get an error connecting to the MongoDB Atlas make sure:
You've whitlisted our IP address / all (0.0.0.0/0)
Your credentials are right
Firewall / anti-virus are disabled
And if all fails
Make sure your DNS is a public one. Or maybe change it to Google's public DNS.
Throwing a link for the same:
https://www.youtube.com/watch?v=F9u5ULrRt3Y
Goodluck!
I am having difficulty connecting to MongoDB via NodeJS. The Mongo setup consists of a shared cluster.
The URL is below:
mongodb://dev.testserver.com:27017,dev.testserver.com:27018,dev.testserver.com:27019/ua?w=0&replicaSet=dev-testserver-com&readPreference=secondary&slaveOk=true
The code that I have is below:
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect(url, function(err db) {
if(!err) {
console.log("We are connected");
}
db.close();
});
The error that I keep getting is "MongoError: no primary found in replicaset".
Can someone please help?
Looks like there is a problem in your server(s). You need to get on the server(s) and run rs.status() to find out the state of your replica set, and each server's logs to find out if and why one or more has stopped or become disconnected.
I can't connect to a database with mongodb or mongoose on nodejs on my arduino yun.
Unable to connect to the mongoDB server. Error: { [MongoError: Authentication failed.]
name: 'MongoError',
message: 'Authentication failed.',
ok: 0,
code: 18,
errmsg: 'Authentication failed.' }
I get that error. The code works fine on my desktop computer.
//lets require/import the mongodb native drivers.
var mongodb = require('mongodb');
//We need to work with "MongoClient" interface in order to connect to a mongodb server.
var MongoClient = mongodb.MongoClient;
// Connection URL. This is where your mongodb server is running.
var url = 'mongodb://*******:********#address:23118/arduino';
// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
//HURRAY!! We are connected. :)
console.log('Connection established to', url);
// do some work here with the database.
//Close connection
db.close();
}
});
I am trying to connect to a mongo lab database. I have not tried connecting to any other database to troubleshoot. This is because I cannot find a free trial version where I do not need a credit card.
EDIT:
I got an Object Rocket instance and it works perfectly on my arduino yun. However it is very expensive; $30 a month (currently on the trial). And I don't need speed or lots of data. So if anyone could figure out why it doesnt work on mlab that would be great.
I've experienced some authentication issues in the past and I've addressed them following the next steps:
1) Identify version numbers of both mongo server and mongoose
2) Identify what authentication mechanism does your server use and make sure that mongoose is trying to authenticate with the one your server is expecting. If you're using an old version, some authentication mechanism might not be supported, so you'd need to upgrade.
3) Identify where the user has been created. Check out Authentication Database and make sure that your client is authenticating using such database. It can be different than the database you store your data. Indeed, it uses to be admin. If so, take a look to the authSource option you can add to your connection string. More info here.
In addition to all of the above, I'd recommend you to use the the mongo client to verify your credentials are right and you can connect to the database.
i am using node-oracleDB connector to connect to my local database , and i want to ask if it was an other(optimal) way to connect to the database, in order to improve communication ability between nodejs and oracle. thanks
I think using connection pooling is the good way. You can find some examples of pooling at here.
Please look at this file https://github.com/oracle/node-oracledb/blob/master/test/pool.js and read carefully to understand how handling connections in a pool.
Node-oracledb is the connector to use when writing Node.js applications that need to connect to Oracle Database.
Most apps will want to use a connection pool
var oracledb = require('oracledb');
oracledb.createPool (
{
user : "hr"
password : "welcome"
connectString : "localhost/XE"
},
function(err, pool)
{
pool.getConnection (
function(err, connection)
{
// use the connection
connection.execute(. . .
// release the connection when finished
connection.close(
function(err)
{
if (err) { console.error(err.message); }
});
});
});
});
There are some simplifications such as the 'default' connection pool that make it easier to share a pool across different modules. This is all covered in the documentation.