I am using mongoose to connect to a remote mongodb server as below; when running on my local machine it works fine; I can also shell into the db without any problem locally. But after I deployed my express api to a server running on a container in AWS ECS. I got: Server selection timed out after 30000 ms
process.env.MOCK_DB=mongodb://username:password#somedomain.com:37017/db_name?directConnection=true
connect(process.env.MOCK_DB, { autoIndex: false }, (err: any) => {
if (err) {
log('cannot connect to mongodb:', err.message);
} else {
isDBConnected = true;
log("Connected to mongodb!");
}
});
What do I need to config?
Please try connecting with time limit more than 30s.
Like this:
process.env.MOCK_DB=mongodb://username:password#somedomain.com:37017/db_name?directConnection=true&socketTimeoutMS=360000&connectTimeoutMS=360000
You can visit this link and find the appropriate solution for your error.
Related
And I am repeatedly getting the following error message:
Where is the problem? How do I connect strapi to MongoDD?
You need to first connect to MongoDB locally. Keep everything blank, or enter the values for your locally running instance of MongoDB. Then, you can retrieve the connection URL from Atlas or wherever, and then you need to edit your config/database.js:
module.exports = ({ env }) => ({
defaultConnection: 'default',
connections: {
default: {
connector: 'mongoose',
settings: {
uri: env('DATABASE_URI')
},
options: {
ssl: true
}
}
}
});
Then, in your .env file:
DATABASE_URI=[CONNECTION_STRING_HERE]
Restart the server and you will be connected with your cloud instance of MongoDB.
If you still have issues, you can see the strapi docs: https://strapi.io/documentation/developer-docs/latest/guides/databases.html#install-on-atlas-mongodb-atlas.
My MongoDB was working fine but at some point, I was facing issues to connect my express server with the DB. when I start the DB using mongod it shows me 2020-05-21T14:36:41.819+0600 I NETWORK [initandlisten] waiting for connections on port 27017 which seems like a normal "running" entry. Then I run my express server where I connect the DB like this:
...
const mongoose = require('mongoose');
const url = 'mongodb://localhost:27017/db_name';
const connect = mongoose.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
});
connect.then(
(db) => {
console.log('Connected correctly to server');
},
(err) => {
console.log(err);
}
);
...
which log Connected correctly to server but when I see the mongod running shell then there still the message which showed that it still waiting for the connection. And the most surprising issue is I can perform database related work in my express server. when I check the document using mongo, I see they correctly added to the database. I can't understand then why mongod behave like this. here is the full log of the mongod shell.
Update:
I forgot to add one important piece of information which is whenever I start my MongoDB server using mongod, in the C:\data\db directory there generate a new mongod.lock file. I tried to delete it and restart my server but eventually it generates again.
I have an EC2 instance running on elastic beanstalk host our website. Our website is a node app that connects to our replicaset and then makes a query, but that query just disappears into oblivion. Here is the code that runs when the server starts:
(function() {
logger.log('info', 'called');
const MongoClient = require('mongodb').MongoClient;
var client = new MongoClient();
client.connect(process.env.MONGO_CONNECTION_STRING, mongoClientOptions, function(err, db) {
if(err) return logger.log('error', err.message);
logger.log('info', 'Connected to mongodb replset.');
var collection = db.collection(SESSION_COLLECTION_NAME);
collection.findOne({}, function(err, doc) {
if(err) return logger.log('error', err.message);
logger.log('info', doc);
});
});
})();
This code works fine locally. But on the server, all I ever see is Connected to mongodb replset. and then nothing else. No error log or info log.
One thing to note is that the mongodb database exists in the same AWS region as our EC2 instance, but it's hosted by a third party called Compose.
So what could be going on here? I can't figure out how to debug this further.
I'm trying to create an alarm system for my application, that will trigger when one of the services (e.g. MongoDB) is not working.
What I'm doing is, once the application is started, I shut down my MongoDB server and try to connect to it, but instead of receiving an error my application just gets stuck into the execution of the method. The server console looks like something is in execution.
My current code (coffeescript) is:
checkMongoService: ()->
mongo = Npm.require 'mongodb'
assert = Npm.require 'assert'
url = 'mongodb://....'
mongo.connect url, (err, db) ->
assert.equal null, err
console.log 'Connected correctly to server'
db.close()
return
I've also been trying by doing a simple
Meteor.users.find().count();
or using MongoInternals with
testConnection = new MongoInternals.RemoteCollectionDriver("mongodb://...);
but still same issue, when mongo is not running no error is thrown and the console stops to work. If then I start Mongo again, it will just return the result (in this case the log 'Connected correctly to server')
Something that I've noticed is if I try with meteor shell to execute testConnection = new MongoInternals.RemoteCollectionDriver("mongodb://...); I get an error "Error: failed to connect to [127.0.0.1:27017]"
TL;DR
Do you might have an idea on how I can check if mongo is reachable or do you know if I'm doing something wrong with the code above?
Try setting the timeouts to be a bit shorter than the default 30 seconds:
mongo.connect(url, {
connectTimeoutMS: 1000,
socketTimeoutMS: 1000,
reconnectTries: 1
}, function(err, db) {...}
(Full set of connection params are here)
Meteor.status().status
from the docs
This method returns the status of the connection between the client and the server. The return value is an object with the following fields:
connected (Boolean)
True if currently connected to the server. If false, changes and method invocations will be queued up until the connection is reestablished.
status (String)
Blockquote
Describes the current reconnection status. The possible values are connected (the connection is up and running), connecting (disconnected and trying to open a new connection), failed (permanently failed to connect; e.g., the client and server support different versions of DDP), waiting (failed to connect and waiting to try to reconnect) and offline (user has disconnected the connection).
https://docs.meteor.com/api/connections.html
I've got a problem to connect my socketIO application (made with nodeJS) with my mongoDB.
I try to connect on an remote server, but its throws me error
Here's my code (there's no user/password set in the mongoDB) :
var url = "mongodb://192.168.1.5:27017/DB"
MongoClient.connect(url, function(err, db) {
console.log("test")
if (!err) {
console.log("test");
}
else {
console.dir(err)
throw err
}
// db.close();
});
And here's when I launch the server and I tried to launch the app in a navigator :
Server listening at port 80:
{ [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' }
/root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:228
process.nextTick(function() { throw err; })
^
Error
at Error.MongoError (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:13:17)
at Server.destroy (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:629:47)
at Server.close (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:344:17)
at Db.close (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/db.js:267:19)
at /root/fys-realtime/examples/chat/node_modules/mongodb/lib/db.js:196:12
at null.<anonymous> (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:226:9)
at g (events.js:180:16)
at emit (events.js:98:17)
at null.<anonymous> (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:238:68)
at g (events.js:180:16)
This error is returned for several errors like :
server is not running
you need to authenticate user
this database does not exists
the mongodb port is not the default port
Check this. Normaly your problem is just one of these causes
For this to work you have to make the changes in /etc/mongod.conf
comment bind_ip=127.0.0.1 As if this line in not commented it Listen to local interface only.
One of the solutions is to change 127.0.0.1 to public ip or whatever the router provided you with in mongodb config file located in /etc
There could be few reasons for ECONNREFUSED error.
Checkpoints
Check if your port is not serving any other process.
Check if your mongod is running.
Check if you localHost is configured correctly with your id or not.
for more you can see this