Node.js app won't connect to MongoDB server - node.js

I can't seem to connect my MongoDB to my Node.js app
This is the error I get when I run mongo status
2020-11-07T16:38:29.570-0500 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect#src/mongo/shell/mongo.js:341:17
This is my app.js code
const MongoClient = require('mongodb').MongoClient
const uri = "mongodb+srv://username:<password>#cluster0.9lxbq.mongodb.net/<MongNode>?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("MongNode").collection("devices");
// perform actions on the collection object
client.close();
});

Related

Node.js and MongoDB Atlas Mongoose Connection Error

I am trying to connect a MongoDB atlas instance to a nodejs/express server :
const mongoose = require("mongoose");
const dbURI =
"mongodb+srv://(url copied from atlas connect)";
const options = {
useNewUrlParser: true,
dbName: "data"
};
mongoose.connect(dbURI, options).then(
() => {
console.log("Database connection established!");
},
err => {
console.log("Error connecting Database instance due to: ", err);
}
);
But I keep get the following error:
MongoNetworkError: connection 5 to cluster ... 27017 closes at TLSSocket. ...
How can I fix this?
Resolved -- Check IP whitelist!

MongoError: failed to connect to serve on first connect in mongoDB atlas

My connection code is entered below. I'm trying to connect to the MongoDB atlas free shared cluster and I keep getting an error saying cannot connect to the server in the first time.
const express = require("express");
const app = express();
const morgan = require("morgan");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
mongoose.Promise = require('bluebird');
const productRoutes = require("./api/routes/products");
const orderRoutes = require("./api/routes/orders");
mongoose.connect(
"mongodb://username:password6#cluster0-shard-00-00-3xdjv.mongodb.net:27017,cluster0-shard-00-01-3xdjv.mongodb.net:27017,cluster0-shard-00-02-3xdjv.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin",
{
useMongoClient: true
}
) .then(() => { // if all is ok we will be here
return server.start();
})
.catch(err => { // we will not be here...
console.error('App starting error:', err.stack);
process.exit(1);
});
Can someone explain why I keep getting this error:
App starting error:
MongoError: failed to connect to server [cluster0-shard-00-00-3xdjv.mongodb.net:27017] on first connect
[MongoError: getaddrinfo EAI_AGAIN
cluster0-shard-00-00-3xdjv.mongodb.net:27017].....
I had the same problem before I realized that I had set up a network access rule allowing only connections from a certain IP. After deleting that restriction and adding a new rule for allowing all connections from anywhere, the problem was gone
My datasource config is as follows:
"mongodb-atlas": {
"url": "mongodb+srv://dbuser:PaSsWOrd#cluster0-oxxxb.gcp.mongodb.net/test?retryWrites=true&w=majority&authSource=admin",
"name": "mongodb-atlas",
"connector": "mongodb",
"database": "admin"
}

How to connect to MongoDB server with SSL in NodeJS 0.10.x?

I'm trying to connect to MongoDB with ssl option using old NodejS version. Connection fails with a timeout error.
I found that connection start works after iojs 3.0.0 release. But I must use NodeJS 0.10.40 in my environment.
It seems a data event is never emitted on a tls socket.
My original problem is there: https://github.com/thaliproject/jxcore/issues/115
Code example and output:
const mongodb = require('mongodb');
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://****?ssl=true'; // Any cloud server with ssl
MongoClient.connect(url, function (err, db) {
if (err) return console.log(err);
console.log('Connected correctly to server');
db.close();
});
{ [MongoError: connection 0 to
aws-us-east-1-portal.28.dblayer.com:23087 timed out] name:
'MongoError', message: 'connection 0 to
aws-us-east-1-portal.28.dblayer.com:23087 timed out' }

ConnectionError: Failed to connect in mssql in nodeJS

i'm trying to connect to my database using MSSQL Server using NodeJS
but i have an error
{ ConnectionError: Failed to connect to 10.10.17.199:undefined in 15000ms
here is the beginning of my code:
var sql = require("mssql");
// config for your database
var config = {
user: 'mat1',
password: 'a',
server: '10.10.17.199\\MSSQLLocalDB',
database: 'master' ,
port: 1433
};
//\\MSSQLLocalDB
console.log("passed 1")
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
what's wrong ?
You are getting an error trying to connect to the server. Maybe you dont have access to the server. Also can happen is timing out to connect, if thats the case you can modify the connection timeout setting in the connection configuration.

mongodb failed to connect to server [127.0.0.1:27017] on first connect

my code below
var mongoose = require('mongoose');
for(let i = 0;i<600;i++)
{
let db1 = mongoose.createConnection('mongodb://127.0.0.1:27017/abc');
db1.on('error', function(error) {
console.log("error = "+(i)+" "+error +db1);
});
db1.on('close', function() {
console.log("close = "+(i)+" "+db1);
});
db1.once('open', function() {
"use strict";
// db1.close();
});
}
I wanted to test mongodb ,the result is
error = 364 MongoError: failed to connect to server [127.0.0.1:27017] on first connect[object Object]
error = 365 MongoError: failed to connect to server [127.0.0.1:27017] on first connect[object Object]
error = 385 MongoError: failed to connect to server [127.0.0.1:27017] on first connect[object Object].......
Another question is whether the connection needs to be closed?
thanks.
Make sure that you have Mongo running on on port 27017 of the same computer that your Node app is running. To verify that, run this in the command line:
mongo localhost:27017
You don't need to close the connection and open it multiple times. You should open the connection once in your app and close it only when you want to close your app. See those answers for more datails:
Where to initialize a new database connection in nodejs?
Using mongoDB in Express routers

Resources