connect to mongodb on separate ec2 instance - node.js

I am running two different instances on AWS, one for node application and other for mongoDB. I am trying to connect to mongoDB on other instance but not able to and failing with "504 Gateway timed out".
My db_conf.js to connect to node application is like below:
var express = require('express');
var mongodb = require('mongodb');
var url = "mongodb://<PUBLIC IP of mongoDB instance>:27017/local";
module.exports = url;
I have commented the "bind_ip" in mongodb.conf and restarted mongoDB.
Also, I have opened the port 27017 for the node application server's public IP from the security groups of mongoDB instance for both inbound and outbound but of no use.
Please suggest a way to achieve this(if there is any). Thanks in advance :)

All looks correct here, you do correctly open port 27017 on the mongoDB instance. Also, you may want to try and kill the original mongod server process, and restart it explicitly pointing to your config file so that it knows to use those settings. Something like the following should accomplish this (assuming your on a linux machine and your config file is in the default folder)-
sudo kill mongod PID
and then
sudo mongod --fork --config /etc/mongod.conf

Related

Connect and allow multiple ip's for mongodb accepting connections

I have a mongodb config file where bind ip is 127.0.0.1
Now mongodb is hosted on one instance and nodejs server is hosted on another instance which ip is (dummy ip) 184.54.125.132
On my mongodb Config file is
net:
port: 27017
bindIp: 0.0.0.0
By this my node server connect because it allows all (which is wrong)
How I add localhost ip of mongodb and my node server ip on bindIp so that mongodb server will listen only through node server and mongodb local.
I tried
bindIP: [127.0.0.1,184.54.125.132] //Not work
I tried
bindIp: 127.0.0.1,184.54.125.132 //Not work
I tried
bind_ip: [127.0.0.1,184.54.125.132] //Not work
I tried
bind_ip: 127.0.0.1,184.54.125.132 //Not work
Anyone suggest me where I am doing mistake. I also enabled authentication
It should be :
bindIp : 127.0.0.1,184.54.125.132
And dont forget to restart your mongod, after changing the config file, otherwise it wont work.
sudo service mongod restart
or
sudo systemctl mongod restart
.
Also, dont forget to open the port so that outside network can access that port(27017 or whatever port your mongodb is hosted on).
If you are using AWS EC2 to host your mongodb, you need to add that port to your inbound security rules, and that specific IP wherever you want to access it from

MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]

I'm new in nodeJS, started learning by following a trailer on youtube, everything goes well until I added the connect function if mongodb,
mongo.connect("mongodb://localhost:27017/mydb")
when I run my code on cmd (node start-app), get the following error,
MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
Could someone explain me which step I missed ?
my code :
var express = require("express");
var MongoClient = require('mongodb');
var url = "mongodb://localhost:27017/mydb";
var webService = require("./webService");
var server = express();
MongoClient.connect(url, function (err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
server.use(express.urlencoded({ extended: true }));
server.set('views', __dirname);
server.get('/', function (request, response) {
response.sendFile(__dirname + '/MainPage.html');
});
server.get('/Sign', function (request, response) {
response.render(__dirname + '/Sign.ejs');
});
server.post("/signUp", webService.signUp);
server.post("/createUser", webService.createUser);
server.listen(5500);
You have to install MongoDB database server first in your system and start it.
Use the below link to install MongoDB
https://docs.mongodb.com/manual/installation/
If you have installed MongoDB check if the server is in which state (start/stop). Try to connect through mongo shell client.
Many of them don't add this, especially in AWS EC2 Instance, I had the same issue and tried different solutions.
Solution: one of my database URL inside the code was missing this parameter 'authSource', adding this worked for me.
mongodb://myUserName:MyPassword#ElasticIP:27017/databaseName?authSource=admin
I faced same issue but after a lot of RND. I found that whts the problem so run this command on your terminal.
sudo service mongod start
then run mongo on terminal
After trying EVERY solution google came up with on stack overflow, I found what my particular problem was. I had edited my hosts file a long time ago to allow me to access my localhost from my virtualbox.
Removing this entry solved it for me, along with the correct installation of mongoDB from the link given in the above solution, and including the correct promise handling code:
mongoose.connect('mongodb://localhost/testdb').then(() => {
console.log("Connected to Database");
}).catch((err) => {
console.log("Not Connected to Database ERROR! ", err);
});
Following the logic behind #CoryM's answer above :
After trying EVERY solution google came up with on stack overflow, I found what my particular problem was. I had edited my hosts file a long time ago to allow me to access my localhost from my virtualbox.
Removing this entry solved it for me...
I had edited my hosts file too for Python Machine Learning setup 2 months ago. So instead of removing it because I still need it, I use 127.0.0.1 in place of localhost and it worked :
mongoose.connect('mongodb://127.0.0.1/testdb')
Your IP address probably changed.
If you've recently restarted your modem, this changes your IP which was probably whitelisted on Atlas.
Soooo, you'll need to jump back onto Atlas and add your new IP address to the whitelist under Security>Network Access.
This had occurred to me and I have found out that it was because of faulty internet connection. If I use the public wifi at my place, which blocks various websites for security reasons, Mongo refuses to connect. But if I were to use my own mobile data, I can connect to the database.
If the mongoDB server is already installed and if you are unable to connect from a remote host then follow the below steps,
Login to your machine, open mongodb configuration file located at /etc/mongod.conf and change the bindIp field to specific ip / 0.0.0.0 , after that restart mongodb server.
sudo vi /etc/mongod.conf
The file should contain the following kind of content:
systemLog:
destination: file
path: "/var/log/mongodb/mongod.log"
logAppend: true
storage:
journal:
enabled: true
processManagement:
fork: true
net:
bindIp: 127.0.0.1 // change here to 0.0.0.0
port: 27017
setParameter:
enableLocalhostAuthBypass: false
Once you change the bindIp, then you have to restart the mongodb, using the following command
sudo service mongod restart
Now you'll be able to connect to the mongodb server, from remote server.
I solved this problem by upgrading major version of mongoose:
Before doing this, make sure (using mongo shell) that you have the correct URL and a running mongo server is available at that URL and the problem still persists.
"dependencies": {
- "mongoose": "^5.4.13",
+ "mongoose": "^6.2.4",
}
just run mongod in terminal on the base folder if everything has been set up like installing mongo db and the client for it like mongoose. After running the command run the project file that you are working on and then the error shouldn't appear.
You can check detail of error by running this command
sudo service mongod status
if error is something like this
Failed to unlink socket file /tmp/mongodb-27017.sock Unknown error
Fatal Assertion 40486 at src/mongo/transport/transport_layer_asio.cpp 670
simply running this will resolve your issue
rm /tmp/mongodb-27017.sock
I don't know if this might be helpful, but when I did this it worked:
Command mongo in terminal.
Then I copied the URL which mongo command returns, something like
mongodb://127.0.0.1:*port*
I replaced the URL with this in my JS code.
first create folder by command line mkdir C:\data\db (This is for database)
then run command mongod --port 27018 by one command prompt(administration mode)- you can give name port number as your wish
I had this issue while working at the local Starbucks and I remembered that when I initially set up my database through Mongo Atlas. I set my IP address to be able to access the database. After looking through several threads, I changed my IP address on Atlas and the issue went away. Hope this helps someone.
This worked for me.
mongoose.Promise = global.Promise;
.connect(
"mongodb://127.0.0.1:27017/mydb",
{ useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true}).then(db => {
console.log("Database connected");
}).catch(error => console.log("Could not connect to mongo db " + error));
I was using localhost, so i changed it to:
mongodb://127.0.0.1:27017/mydb
My problem was the wrong port number for mongoDB server.
I had:
DATABASE_URL= "mongodb://localhost:3000/node-express-mongodb-server"
in my .env file (my environmental variables), but I had written it before running mongoDB server. So when I ran the mongoDB server, it wrote a different port number and I had to change it. I changed it to the right port number (which was written on my cmd window by mongoDB):
DATABASE_URL= "mongodb://localhost:27017/node-express-mongodb-server"
and now it works fine.
if you are a Mac user just upgrade your homeBrew from terminal:
$ brew upgrade
$ mongod --config usr/local/etc/mongod.config
$ Xcode-select --install
$ mongo
1) If you haven't installed mongodb, install it.
2) open a new terminal, type "mongo". This is going to connect you to a MongoDB instance running on your localhost with default port 27017:
mongoose.connect('mongodb://localhost:27017/').then(() => {
console.log("Connected to Database");
}).catch((err) => {
console.log("Not Connected to Database ERROR! ", err);
});
Better just connect to the localhost Mongoose Database only and create your own collections. Don't forget to mention the port number. (Default: 27017)
For the best view, download Mongoose-compass for MongoDB UI.
This one helped me.
Try creating a new folder, if your MongoDB is installed in C:\Program Files the folder should be called db and in a folder data.
C:\data\db
When you start the mongod there should be a log where the db 'isnt found'.
So when none of the above solutions worked for me, after installing everything correctly, I thought to restart the system.
It's working now.
Note that I did everything said above, but no luck. The only restart worked for me.!!
You may also want to restart once.
You have to install MongoDB database server first in your system and start it.
Use the below link to install MongoDB
If you have already installed MongoDB database in your system then you have to check that your DB is in start position or not with the help of following steps:
press CTRL + Shift + Esc
go to the service tab and search for Mongo
check the status - it may be stopped. So click on the Services tab at the bottom right corner and again search for MongoDB
Click on it and start the DB by right click or in left panel.
If the error happens on macbook run this command to keep the mongodb server running.
mongod --config /usr/local/etc/mongod.conf --fork
The issue majorly is that your mongodb server is rejecting the connection it might be that the server is not on/active eventhough it has been installed on your macbook.
In my case the problem was that there was another instance of mongoDB server running I had shutdown my computer without stopping the server hence when I tried running mongosh it gives me that error. Try restarting the computer it will shutdown all the servers and the erro was gone.
I was trying to connect, without starting the service.
This is how i fixed the error (MacOS env).
$ brew services start mongodb-community#6.0
$ mongosh // connected to db and fixed the error.
$ brew services stop mongodb-community#6.0
For me the problem resolved when I started the MongoDB on port other than 27017. Even though nothing was running on 27017 but the problem resolved when I started it on another port.
To do that navigate to the /etc/mongod.conf and change the port: 27017 to some other port like port: 27019.
Then restart the service by:
sudo systemctl restart mongod.service.
And then try to connect to MongoDB by specifying the --port parameter like:
mongod --port 27019, or
mongo --port 27019
Best!
this was my erros:
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.2
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
SOLUTION:
The best Answes is if you using mac M1 and M2
use restrat with sudo like below
MongoNetworkError solution
sudo brew services restart mongodb-community#6.0
I connected to a VPN and the connection accomplished. I was using school's WiFi which has some restrictions apparently.
I guess you must be connecting to cloud.mongodb.com to your cluster.
One quick fix is to go to the connection tab and add your current IP address(in the cluster portal of browser or desktop app). The IP address must have changed due to a variety of reasons, such as changing the wifi.
Just try this approach, it worked for me when I got this error.
You need to initialize your mongoDB database first, you can run "mongod" in your terminal and then it will be working fine.

Memcached Could not connect to Remote Server | memcached.js

My local environment for API
node -v: v8.9.4
npm -v: 5.6.0
Package
memcached.js: "memcached": "^2.2.2"
We have a Node API in which we are using package memcached.js to connect to Memcache server with below configurations.
MEMCACHED_CONFIG:
{
MAX_VALUE: 1024,
SERVER: "X.X.X.X",
PORT: 11211,
COMPLETE_PATH: "X.X.X.X:11211",
CACHE_TIMEOUT: 3600,
POOL_SIZE: 50,
maxKeySize: 1024,
timeout: 5000
}
So X.X.X.X is remote server IP where our Memcache server is running.
and I am able to connect this X.X.X.X server from my system by using telnet command like c:/> telnet X.X.X.X 11211 and it works.
cacheUtility.js
var MEMCACHED_CONFIG= require('./MEMCACHED_CONFIG');
var Memcached = require('memcached');
Memcached.config.maxValue = MEMCACHED_CONFIG.MAX_VALUE;
Memcached.config.poolSize = MEMCACHED_CONFIG.POOL_SIZE;
Memcached.config.maxKeySize= MEMCACHED_CONFIG.maxKeySize;
Memcached.config.timeout= MEMCACHED_CONFIG.timeout;
var memcached = new Memcached();
memcached.connect(MEMCACHED_CONFIG.COMPLETE_PATH, function( err,
conn ){
if( err ) {
CONFIG.CONSOLE_MESSAGE("Cache Connect Error "+conn.server);
}
});
We are using above code to connect to Memcached Server and as you can see remote server IP is coming from MEMCACHED_CONFIG.
My issue is that it is always trying to connect to 127.0.0.1 server instead of passing Remote Memcached Server. So in order to run it, I have to make changes in the memcached.js file of the core package.
C:\BitBucketProjects\Licensor Server\node_modules\memcached\lib\memcached.js
function Client (args, options) {
var servers = []
, weights = {}
, regular = 'localhost:11211'
//, regular = 'X.X.X.X:11211'
, key;
I don't want to make any change in core package.
Why is it not connecting to the given server?
When you have memcached server setup on a different machine than the server using it then always mention the server IP and options otherwise it defaults to localhost. You can see that if you view the "server" property of the client (using NodeJs memcached client version 2.2.2):
var Memcached = require('memcached');
var memcached = new Memcached();
console.log(memcached.server);
Seems to be some issue with the "memcache.connect" method as it does not override the localhost server. To make it work, you have to mention the IP of memcached server in the constructor as mentioned in the documentation:
var Memcached = require('memcached');
var memcached = new Memcached('192.168.10.10:11211');
Now you should be able to connect to the server without an issue if you have the 11211 port opened on the host. If not allowed, you can execute the following command on Memcached host to open the port:
$ sudo ufw allow 11211
To ensure you are able to connect to memcached server use following command:
telnet 192.168.10.10:11211
If even that does not work, your server might have stopped working so you need to start it either as a service or as a process:
Start as a process:
$ memcached -u memcached -d -m 30 -l 192.168.10.10 -p 11211
Start as a service:
$ sudo systemctl start memcached
OR
$ sudo service memcached start
Just for reference for those who might not know, to expose memcached server on the network, you can either specify the IP and port like in the command above or in the memcached configuration file. To provide default configuration, look for "-l 127.0.0.1" in the following file and replace the loopback address with your host server's network IP:
$ sudo nano /etc/default/ufw
Of course, above commands will work only if you have memcached installed on the server, if not installed then run the following to install it first:
$ sudo apt-get install memcached
I hope it helps.

Accessing MongoDB from Windows & Mac Client Machines

I have MongoDB 3.2 installed on my Linux Red Hat server.
I am starting to access it and looking at the mongo Shell instructions.
For a Windows machine, the instructions want me to get to the command prompt and change dirs to the installation directory. The problem is, MongoDB is installed on my web server and not my local windows machine.
Question: does Mongo Shell apply to me then? How do I start using, connecting and accessing Mongo from my Windows and Mac machines?
[Note: I am a traditional MySQL / phpMyAdmin developer looking to advance to MongoDB]
Amendments:
(1) With the help of #AlexBlex I am progressing to trying to connect to my MongoDB on my server from Robomongo on my windows client. I get the following error when trying to setup my connection. I tried the address with just my server ip and with http://{my server ip}. Neither worked. See screen shot of error
(2) This is what I have in my current mongod.conf file:
#port=27017
bind_ip=127.0.0.1
(3) here is what my connection settings look like. Oddly, #AlexBlex's solution below shows an SSH tab on his Mac version. The Windows and Mac versions I just installed lacks that tab.
If you install MongoDB on your local machine, you can use the Mongo shell like below to connect to your remote server
mongo yourserver:27017/database
You will have to configure your Mongo server to allow remote connections. In order to achieve this you need to have the following line in your /etc/mongodb.conf file. You need to replace 10.0.0.2 with the ip address of your client machine.
bind_ip = 127.0.0.1,10.0.0.2
You need either ssh to the server where mongodb is installed, or install mongodb on local machine.
For robomongo to connect to remote host you need to ssh to the server, and check it listens on external interface:
lsof -i | grep 27017
In case it is bound to localhost only, you need to edit a line with bind_ip in /etc/mongodb.conf and restart the service.
I would recommend to keep it listening on the local interface only for security reasons, and use ssh tunnelling to connect:
I found the answer. #ShahNewasKhan is brilliant. See How to connect Robomongo to MongoDB
All you need to do is SSH to server and edit mongod.conf file:
uncomment #port=27017 to port=27017
comment bind_ip=127.0.0.1 to #bind_ip=127.0.0.1
restart mongodb via service mongod restart
Then create a mongo connection via your server ip in the address field and 27017 in the port field
Hope this helps mongo newbies and start-ups like me :) Good luck.
Now I just need to figure out how to make this secure. My concern is that anyone who knows my server ip can hack into my MongoDB

Problem with access to Mongodb on Amazon EC2

i've got another question for you.
I have Amazon EC2 instance with mondodb installed.
It works great except one thing - i can't access (connect to) it from outside (my PC).
I think the problem with Security Groups. It's some sort of default firewall.
Does anyone know how to configure EC2 instance to have access to mongodb?
Thanks in advance.
Think carefully before doing this. If you open the ports, make sure you restrict the IP numbers that can access it, otherwise anyone will be able to access your database. You can enable authentication in MongoDB, but it's not particularly safe, just a username and password. You should not have your database open to the internet, it is not a good idea.
A better way than opening up ports in the EC2 firewall is to open an SSH tunnel an forward the port, this makes sure that only you can access the database, and only while the SSH tunnel is active.
Open up a new terminal and run this command (replacing user and host with the user you use when SSH'ing to your server and the name of the server):
ssh user#host -N -L 27017:127.0.0.1:27017
The command will forward the port 27017 on your computer to the same port on the server. To connect to the MongoDB instance simply run mongo in a terminal (if that doesn't work, try mongo --host 127.0.0.1 or even mongo --host 127.0.0.1 --port 27017).
If you run MongoDB on your local machine you will have to change the first port, since the local server is already using it. In that case run this command instead:
ssh user#host -N -L 27018:127.0.0.1:27017
and then connect with
mongo --port 27018
(possibly adding --host 127.0.0.1 if it doesn't work).
When you're done working with the database, exit mongo and press ctrl-C in the terminal with the SSH command.
You need to add a security group exception for the port 27017 if you are using default config for you to access it from outside. For security group configuration, please check the amazon EC2 documentation. And if you are using a different port on Mongo, change the security group port accordingly.
--Sai
Is your EC2 instance a Windows server by any chance? If so, in addition to EC2's Security Groups you also need to configure Windows Firewall to allow the incoming connection.
Go To Administrative Tools, Windows Firewall with Advanced Security, and configure a new Rule that allows incoming connections on port 27017 (the default mongo port) or whatever port you've chosen.

Resources