Compass - Unable to connect to any MongoDB Atlas database using compass or otherwise - mongodb

I have been trying to connect to Atlas using the university.mongodb.com connection string:
mongodb+srv://m001-student:m001-mongodb-basics#cluster0-jxeqq.mongodb.net/test
But the compass GUI gives the following error:
queryTxt ETIMEOUT cluster-0-jxeqq.mongodb.net
I created my own cluster and tried again. But unfortunately, the same error showed up. I tried to write a JavaSript code as (the angular placeholders had the actual values in them):
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://<user>:<password>#cluster0.amffz.gcp.mongodb.net/<dbname>?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
if (err) return console.log(err);
const collection = client.db("test").collection("collection-1");
client.close();
});
...which gave me this error:
Error: queryTxt ETIMEOUT cluster0.amffz.gcp.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:202:19) {
errno: undefined,
code: 'ETIMEOUT',
syscall: 'queryTxt',
hostname: 'cluster0.amffz.gcp.mongodb.net'
}
In Atlas, I enabled the Network Access in the Security option to :
0.0.0.0/0
I disabled my firewall just to be sure that the connection is not being blocked:
sudo ufw disable
But nothing seems to work.
Any help?
[Edit]
System Config: Ubuntu 20.04 LTS

I figured out that the machine which I was using had some problem with the connectivity.
I figured out the way to solve this. I uninstalled MongoDB. It seemed like it had some configuration issues.
I removed mongodb and mongoose npm packages as well.
Re-installed everything. And viola, it got connected!

Related

Connection with MongoDB Cloud frequently fail with ESERVFAIL error

I have created a MongoDB Cloud Database to test my NodeJs Backend.
Ocassionally (probably the 70% of the time), I cant connect and the connect and the console throws the following error log:
node:internal/errors:464
ErrorCaptureStackTrace(err);
^
Error: querySrv ESERVFAIL _mongodb._tcp.cluster0.u08yi.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (node:dns:213:19) {
errno: undefined,
code: 'ESERVFAIL',
syscall: 'querySrv',
hostname: '_mongodb._tcp.cluster0.u08yi.mongodb.net'
}
The weird thing is that I can connect sometimes. Is it because it is free and then it is prone to fail? It is getting frustrating since sometimes I have to keep updating my nodemon hoping for the ddbb to connect.
I already allowed my IP access.
Im using Mongoose and connecting this way:
mongoose.connect(`mongodb+srv://username:password#cluster0.u08yi.mongodb.net/mydatabase?retryWrites=true&w=majority`,(err)=>{
if(err) throw err;
console.log("MongoDB Connected Successfully");
})

connecting to mongodb replicaSet with nestjs and typeorm is not working

problem
I'm trying to connect to mongodb with nestjs(^8.2.3) and typeorm(^0.2.28)
In test environment, connecting to mongodb standalone server is working. For your information, node mongodb library version is ^3.6.2.
production sample code(nestjs server)
I referred the typeorm code to write mongodb options
import { TypeOrmModule } from '#nestjs/typeorm';
import { MongoConnectionOptions } from 'typeorm/driver/mongodb/MongoConnectionOptions';
export const configForOrmModule = TypeOrmModule.forRootAsync({
imports: [],
useFactory: async () => {
const mongodbConfig: MongoConnectionOptions = {
type: 'mongodb',
username,
// for replicaSet (production)
hostReplicaSet: 'server1.example.com:20723,server2.example.com:20723,server.example.com:20723',
replicaSet: 'replicaSetName'
port: Number(port),
password: encodeURIComponent(password),
database,
authSource,
synchronize: true,
useUnifiedTopology: true,
entities: [Something],
};
return mongodbConfig;
},
inject: [],
});
But in production environment, when nestjs server try to connect to mongodb replicaSet, the server get this server selection loop error message over and over again like below. Interesting thing was the domain that the server tried to connect was different from replicaSet hosts(ex. another-hostname not included in server1.example.com:20723,server2.example.com:20723,server.example.com:20723). (+ edited: the another hostname is actual physical server indicated by the dns(server.example.com))
[39m01/28/2022, 2:39:16 AM [31m ERROR[39m [38;5;3m[TypeOrmModule] [39m[31mUnable to connect to the database. Retrying (3)...[39m
MongoServerSelectionError: getaddrinfo ENOTFOUND <another-hostname>
at Timeout._onTimeout (/home/node/app/node_modules/mongodb/lib/core/sdam/topology.js:430:30)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7)
what I’ve tried but these not worked
remove useUnifiedTopology: true option
downgrade mongodb library version to 3.5.11 (I've read in mongodb community there are something bug with topology after 3.6 version)
use host option not the hostReplicaSet
if you need more information, please tell me. thank you for your helping.
It was kubernates DNS issue. The hostReplicaSet server1.example.com:20723,... is resolved to host1 (physical server name. without example.com) but, k8s doesn't know it. so connection was failed.
there are two options
update kubernates /etc/hosts setting to add host1 -> host1.example.com
or update mongodb hostname host1 -> host1.example.com

Can't connect to mongoDB: Mongo error { Error: querySrv ENODATA

My set-up consists of Node.js on Express on the back-end connected to mongoDB through mongoose. When I start the server, I get this error:
Mongo error { Error: querySrv ENODATA `database_hostname`
at errnoException (dns.js:55:10)
at QueryReqWrap.onresolve [as oncomplete] (dns.js:243:19)
code: 'ENODATA',
errno: 'ENODATA',
syscall: 'querySrv',
hostname: `database_hostname` }
This results in me not being able to run my application on localhost due to it being constantly loaded.
At the same time connecting using MongoDB Compass works fine.
The database used to be working fine, but after I changed the location where I have been using it and created manually a new collection in Compass, which did not have a set-up locally, I started getting this error.
Cluster configuration:
SRV Record: off
Read Preference: Primary
SSL: System CA / Atlas Deployment
SSH Tunnel: None

SailsJS deployment to Heroku, connect to Mongolabs MongoDB

I am right now attempting my first Heroku deployment of a SailsJS API. My app uses SailsJS v0.11 andsails-mongo 0.11.2.
I have updated config/connections.js to include the connection information to MongoDB database I have hosted for free at Mongolab.
mongodb: {
adapter: 'sails-mongo',
url: "mongodb://db-user:password123#ds047812.mongolab.com:47812/testing-db"
}
Also updated config/models.js to point to that adapter.
module.exports.models = {
connection: 'mongodb',
migrate: 'safe'
};
This is basically all I have changed from running the code locally, when I deploy to Heroku the app crashes and I get this error...
/home/zacharyhustles/smallChangeAPI/node_modules/connect-mongo/lib/connect-mongo.js:186
throw err;
^
at Socket.emit (events.js:107:17)
2015-07-08T19:37:00.778316+00:00 app[web.1]:
at Socket.<anonymous> (/app/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection.js:534:10)
Error: Error connecting to database: failed to connect to [localhost:27017]
How do I get rid of this, and make sure Sails does not try connecting to localhost db?
Ok, the problem was with storing sessions.
My solution was to setup a Redis database to store sessions.
In config/sessions.js make sure everything is commented out except for the method you want for session store.
Mine looked like this:
adapter: 'redis',
host: 'example.redistogo.com',
port: 1111,
db: '/redistogo',
pass: 'XXXXXYYYYYYXYXYXYYX',
This solved my posted problem, hope this helps another person out.

nodejs chat example does not work

I've come across a node chat example on github, When I try to run it, I see the following error:
Error connecting to mongo perhaps it isn't running ?
I've installed mongo 0.9.2, nodejs 5.2 pre, npm 3.0 and other dependencies. The example can be found here: https://github.com/gregstewart/chat.io
I can not determine whether if the example not really works or I didn't run it right. Please help.
Did you install and start mongo-db on your system? This error is mostly because of a missing mongo instance running on the local machine.
Check out the follwing code excerpts from chat.io.
main.js:
/**
* Configure the user provider (mongodB connection for user data storage)
*/
var userProvider = new UserProvider('localhost', 27017);
Creates a new UserProvider object using host and port for database (localhost:27017, mongo-db default).
UserProvider.js:
UserProvider = function(host, port) {
this.db = new mongo.Db('node-mongo-chat', new Server(host, port, {auto_reconnect: true}, {}));
this.db.addListener('error', function(error) {
console.log('Error connecting to mongo -- perhaps it isn\'t running?');
});
this.db.open(function() {
});
};
Opening the connection to the server, printing out an error on failure (the error you mentioned above).
Consider reading up on the mongo-db docs concerning installation and setup here