2

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
enter image description here

(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. enter image description here

Kara
  • 6,115
  • 16
  • 50
  • 57
H. Ferrence
  • 7,906
  • 31
  • 98
  • 161

3 Answers3

1

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
Alex
  • 21,273
  • 10
  • 61
  • 73
  • 1
    Thank you @Jaco. MongoDB is not installed on my local machine. It is installed on my Linux Red Hat server. I am trying my best to get started and learn MongoDB. So how do I "configure [my] Mongo server to allow remote connections" ? – H. Ferrence Jan 29 '16 at 15:38
  • You still need to install it locally to call `mongo yourserver:27017/database` =) – Alex Blex Jan 29 '16 at 15:40
  • Really. So how does it work as a web-based app service tool? Do all users out there need to have MongoDB installed on their computers in order to use my web app? – H. Ferrence Jan 29 '16 at 15:44
  • 2
    Your users do not connect to mongodb. They connect to http server, which runs your application, which connects to the db. To be able to connect to the db, you need a client installed. – Alex Blex Jan 29 '16 at 15:47
1

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:

enter image description here

Alex Blex
  • 34,704
  • 7
  • 48
  • 75
  • Ok @AlexBlex. I am getting it -- the clouds are starting to lift :) So unlike MySQL with the Web Interface of phpMyAdmin, I need to do all of my MongoDB work from the command-line? – H. Ferrence Jan 29 '16 at 15:41
  • There are some GUI https://docs.mongodb.org/ecosystem/tools/administration-interfaces/ I like Compass and robomongo, but basically yes, I don't think you can completely avoid command line. – Alex Blex Jan 29 '16 at 15:44
  • Ok. I did install Robomongo on my Mac last night. But then I came up against the mac window of needing to setup my connection. That's where I got lost. What to do next? – H. Ferrence Jan 29 '16 at 15:45
  • If I install Robomongo on my local machines and connect from there, do I need to make the `bind_id` change for localhost as recommended by @Jaco above? – H. Ferrence Jan 29 '16 at 15:51
  • I did your SSH command and got the following `mongod 10980 mongod 8u IPv4 355046325 0t0 TCP localhost.localdomain:27017 (LISTEN)` -- so am I good to go? – H. Ferrence Jan 29 '16 at 15:57
  • I have updated the answer. bind_ip works similar to how it works in mysql. It is recommended to bind to localhost for security reasons, but it will make it impossible to connect to the database from external web. – Alex Blex Jan 29 '16 at 15:58
  • Ok, so it listens to local interface only. I have added a screenshot how to connect robomongo to mongodb via ssh. – Alex Blex Jan 29 '16 at 16:02
  • I got an 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 at https://drive.google.com/open?id=0Bytqhoir_Tt5N2RFTldTamdVaFU – H. Ferrence Jan 29 '16 at 17:22
  • This is what I have in my current mongod.conf file: `#port=27017` and `bind_ip=127.0.0.1` – H. Ferrence Jan 29 '16 at 17:26
  • @H.Ferrence, please add the error you have to the question to make it useful for other people who may face the same issue. – Alex Blex Jan 29 '16 at 17:28
  • Can't say the error image is terribly informative. Please read basics about how tunnelling works https://en.wikipedia.org/wiki/Tunneling_protocol. Robomongo does exactly exactly that in the background. I have added some annotations to the images with sincere hope it helps. – Alex Blex Jan 29 '16 at 18:00
  • I posted another amendment to my OQ – H. Ferrence Jan 29 '16 at 18:06
  • How did you get the SSH tab on your client machine @AlexBlex? – H. Ferrence Jan 29 '16 at 18:11
  • Maybe Robomongo is not the right tool. It looks like it might go away due to lack of financial resources. I am looking for other more reliable and working client UI software. – H. Ferrence Jan 29 '16 at 18:31
1

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:

  1. uncomment #port=27017 to port=27017
  2. comment bind_ip=127.0.0.1 to #bind_ip=127.0.0.1
  3. 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

Community
  • 1
  • 1
H. Ferrence
  • 7,906
  • 31
  • 98
  • 161
  • That's great you have found a solution on SO. Please accept your answer to mark the question as resolved. It would be great if you add a note to the answer, that this setup opens access to your database to anyone in the Internet and should not be used in production unless you enable mongodb auth and ssl: https://docs.mongodb.org/manual/administration/security-checklist/ – Alex Blex Feb 01 '16 at 09:24