Access MongoDB from a remote server (e.g. ec2)



First of all I would like to say what happened to me when I tried to access mongodb (on amazon ec2) from my local machine:

Story/Motivation:
(you can skip to next section if you want to know the steps)
I tried to migrate the data from mongodb to a new server (mongodb as well). I was planning to use dbcopy command. So as you know, mongodb by default is not accessible from ec2 (even with ec2 security rules as 0.0.0.0 and all ports open). So I found a way to do so which is changing mongodb configuration (bind ip), so it is obvious that a restart is needed for the new configuration to take effect. and boom, mongo db is no longer accessible. the first thing to do is to take a full backup.  and after missing around with different commands and strange error messages from mongodb I found the solution scattered, so I wanted to gather them here in one place.

some errors I faced are 1111 and 61

Steps:

1-go to /etc/mongodb.conf and change  bind_ip = 127.0.0.1 to bind_ip = 0.0.0.0

1-for me the command sudo service mongod restart doesn't work and using the command mongd directly is giving errors, so I used the below command
sudo mongod --config /etc/mongodb.conf --smallfiles &

the sign & is for it to run in the background, as it was in the front. and for the flag --smallfiles because I didn't have much space left

and viola.

3-If your local machine has mongodb installed run the commands:
mongo <your server ip address>:port/dbname

e.g.
mongo 127.0.0.1:27017/test

Note: This is not necessarily the best way to do it, but it is the way I followed

I hope this was helpful,

Comments

Popular Posts