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
Related
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.
I can connect via Mongodb COMPASS using my username and password but apparently i can't connect it to my website.
mongodb://localhost:27017/mydb works fine in connection string but not atlas/online cluster
{"connectionString":"mongodb+srv://myuser:mypass#thiscluster.azure.mongodb.net/mydb?retryWrites=true&w=majority"
,"secret":""}
Error :
Server listening on port 3000
Unhandled rejection MongoError: not authorized on admin to execute command { lis
tIndexes: "sessions", cursor: { } }
at Function.MongoError.create (C:\Users\user\Documents\GitHub\meanie\serv
er\node_modules\mongodb-core\lib\error.js:31:11)
at queryCallback (C:\Users\user\Documents\GitHub\meanie\server\node_modul
es\mongodb-core\lib\cursor.js:212:36)
at C:\Users\user\Documents\GitHub\meanie\server\node_modules\mongodb-core
\lib\connection\pool.js:469:18
at processTicksAndRejections (internal/process/task_queues.js:79:11)
I am using MEANIE STACK BLOG
Reference : Cloud mongodb guide
mongodb+srv://:#thiscluster.azure.mongodb.net/?retryWrites=true&w=majority
Other reference : https://docs.mongodb.com/manual/reference/connection-string/
SOLVED
config.json / node's "connectionString" can't be parsed using mongo+srv://user:pw#thiscluster.mongodb.com/mydb?thisoptions
node version i'm using is 2.2 12
fixed via :
"connectionString":"mongodb://user:pass#test-shard-00-00-sample.azure.mongodb.net:27017,test-shard-00-01-sample.azure.mongodb.net:27017,test-shard-00-02-sample.azure.mongodb.net:27017/THISDB?ssl=true&replicaSet=Test-shard-0&authSource=admin&retryWrites=true&w=majority",
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'm a begginer in NodeJS and when i run node MainApp.js in console I get this error:
C:\Assigment 2 (NodeJS)\node_modules\mongodb\lib\server.js:235
process.nextTick(function() { throw err; })
^
Error: getaddrinfo ENOTFOUND . .:27017
at errnoException (dns.js:26:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:77:26)
I don't know what this means. Inside MainApp I connect mongoose: mongoose.connect("./DataBase"); where DataBase is the folder where I run mongod --dbpath "C:\[...]\DataBase. The database server seems to start successfully, the console printing: [...] waiting for connections on port 27017.
server.js lines 231-236:
// Try to callback
try {
callback(err);
} catch(err) {
process.nextTick(function() { throw err; })
}
Package versions:
"express": "~4.13.1",
"mongodb": "~2.1.0",
"mongoose": "~4.3.1"
make sure that your database is up and running.
Go to your terminal and in your app directory run:
mongod
Then restart your node server and you should be good go
I do not understand the whole process yet, but I hope this answer will provide some insight:
It seems that running node or npm start without starting up the database returns this error.
If database is started, then changing
mongoose.connect("./DataBase"); (which previously caused the error)
to mongoose.connect('mongodb://localhost/database');fixes the issue.
Intuitively this seems to simply be the error MongoDB returns when it can't find the database used by the server, however this might not be the precise description of the issue.
If someone can provide further insight into the problem, such help would be appreciated.
I changed connection string from mongodb://localhost:27017/ to mongodb://127.0.0.1:27017/ and it worked.
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