I am trying to connect to mongodb server locally at this ip mongodb://127.0.0.1:27017/ yet still no connection to the mongodb server
I have tried using localhost instead of 127.0.0.1 but not change .
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://127.0.0.1:27017/';
const dbname = 'conFusion';
console.log("before connect");
MongoClient.connect(url, (err, client) => {
console.log('Connected SuccessFully');
});
I have also run this command Mongod --dbpath=data --bind-ip 127.0.0.1:27017 but no change
Occasionally I would get the timeout error
Related
My nodejs app can't connect to MondoDB Server.
I have already allowed all ip addresses (0.0.0.0/0) to connect.
const mongoose = require('mongoose');
const URL = '' //database url
mongoose.connect(URL, {useNewUrlParser: true}).then(db => {
console.log("Connected")
}).catch(err => {
console.log(err)
})
Above code throws "MongooseServerSelectionError: connection timed out" error
U have to provide URL in mongoose something like this
const url = 'mongodb://localhost:27017/myapp'
But before that please verify whether the mongo is running in your system or not.
Although I am not the first on Stackoverflow not to be able to connect to mongoDB atlas using mongoose, no one seems to have my specific error:
{ MongoNetworkError: failed to connect to server
[cluster0-shard-00-00-shqnc.mongodb.net:27017] on first connect
[MongoNetworkError: connection 5 to
cluster0-shard-00-00-shqnc.mongodb.net:27017 closed]
Here's how my server is set-up:
Keys.js
module.exports = {
mongoURI:
"mongodb+srv://Ahmed:<MyMongoDBAtlasPWD>#cluster0-shqnc.mongodb.net/test?retryWrites=true&w=majority"
};
Server.js
const express = require("express");
const mongoose = require("mongoose");
const app = express();
// DB Config
const db = require("./config/keys").mongoURI;
// Connect to MongoDB
mongoose
.connect(db, {
useNewUrlParser: true
})
.then(() => {
console.log("MongoDB connected!");
})
.catch(err => {
console.log(err);
});
app.get("/", (req, res) => {
res.send("Hello");
});
//process.env.Port is for Heroku
const port = process.env.Port || 5000;
// `` ES6 Template literal is used so that we can put a variable inside the String
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
And this is the code suggested by the MongoDB atlas website to be used:
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://Ahmed:<password>#cluster0-shqnc.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});
But, since I don't want to use mongoClient but rather mongoose, I am having some trouble and I cannot see why the code doesn't work;
EDIT 1: I have managed to connect using the Shell command(Check-out my answer). However, connecting through the app doesn't work and gives a different error:
{ MongoNetworkError: failed to connect to server
[cluster0-shard-00-01-shqnc.mongodb.net:27017] on first connect
[MongoError: bad auth Authentication failed.]
EDIT 2: I made a stupid mistake. I've forgotten to remove <> from the . All is good now.
The problem is that I was trying to connect using my MongoDB Atlas account password instead of the user password. Yes, those are 2 different things.
1. Click on Database Access
2. Edit the current user and modify the password
3. Use that password to connect to MongoDB Atlas
Make sure you have whitelisted your public IP. You can find that by googling "what is my ip".
As i'm new to api creation in nodejs and i tried to connect mongo db throung nodejs code and i have advance version of mongodb(V4) and nodejs(v10) and i could not able to connect please can any one help me.
my mongodb node code:
// Retrieve
var MongoClient = require('mongodb').MongoClient;
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
if(!err) {
console.log("We are connected");
}
});
and cmd:
commend for db connection in cmd prompt is mongod -- for connection establishement.
Can't connect to a MongoDB using Node.js.
This is my code (mdb.js):
var MongoClient = require('mongodb').MongoClient, format = require('util').format;
MongoClient.connect('mongodb://localhost:3000', function(err,db){
if(err){
throw err;
} else {
console.log("Connected");
}
db.close();
});
node mdb.js prints MongoError:
enter image description here
it worked for me when i changed 'localhost' to 127.0.0.1
First make sure your MongoDB server is running by running mongod in the directory that MongoDB is installed.
Then see what port it is running on ( usually it's 27017 ) . Then update your URL in your code and you're good to go .
Your error explaining itself.
Mongo cant find default db path B:/data/db.
Create this folder or on start up choose another DB directory
mongod --dbpath yourPath(C:\myDb)
Also default mongo port is 27017 so you need to change connection string like this "mongodb://#localhost:27017/dbYouWant"
Hope this helps.
Use this URL mongodb://0.0.0.0:3000
First of all please verify that have you changed your default database port from 27017 to 3000 or not.
If not changed then please try the below code it's working for me
const { MongoClient } = require("MongoDB");
// Connection URL
const url = "mongodb://localhost:27017";
const client = new MongoClient(URL);
// Database Name
const dbName = "myProject";
async function main() {
// Use connect method to connect to the server
await client.connect();
console.log("Connected successfully to server");
const db = client.db(dbName);
const collection = db.collection("documents");
// return "done.";
}
main()
.then(console.log)
.catch(console.error)
.finally(() => client.close());
Also here is I attached a my screenshot with the output
Sample output example
I'm trying to use mongoDB for my app.
I have 2 databases and use next code to connect:
var express = require("express");
var router = express.Router();
var mongojs = require("mongojs");
//var mongo_db =
//mongojs("mongodb://xxxxx:xxxxx#some_adress/cat_mean_db", ["tasks"]);
var mongo_db = mongojs("mongodb://xxxxx:xxxxx#localhost:3000/cat_db",
["tasks"]);
//get all docs(pages)
router.get("/tasks", function (req, res, next ) {
mongo_db.tasks.find(function (error, tasks) {
if(error)
res.send(error);
res.json(tasks);
});
});
if I use this db
var mongo_db =
mongojs("mongodb://xxxxx:xxxxx#some_adress/cat_mean_db",
["tasks"]);
everything is working well, but when I try to use db on localhost I got an exeption: connection 0 to localhost:3000 closed
the local db is exist and has user for shure.
From mongoose documentation on NPM :
Note: If the local connection fails then try using 127.0.0.1 instead of localhost. Sometimes issues may arise when the local hostname has been changed.
You should try using 127.0.0.1 instead of localhost.