Connecting Linode to local MongoDB

I am building a web app that allows users to search information that is stored in MongoDB. I'm saving the data in Mongo on my localhost. I've created a connection in my app this way:

 client = MongoClient('localhost', 27017)
 db = client["rjh"]

When a user searches for info through the web app, the code to find the data that runs is:

 search_results = list(db.openings.find({"$text": {"$search": 
 form.search.data}}))

When I run the web app on my local server, it works fine. I'm assuming it's because everything is run locally and the web app can find the MongoDB instance. But, when I deploy the app to my Linode, the search breaks. My assumption here is that because there's no connection from Linode to my locally hosted MongoDB instance.

So, how do I create that connection between my Linode server and my locally hosted MongoDB so this will work correctly?

2 Replies

I haven't tried this, personally, but I have some resources for you that might help you get this set up.

The first is another post here in the Community that goes over connecting to a remote database:

Connect to Remote Database

Since that is designed for MySQL, I wanted to include this as well, which gives you steps for MongoDB:

How to Connect to Your Remote MongoDB Server

Using those two guides together should help you accomplish this. I'm also adding some tags to your question to give it more visibility in case other members of the Linode Community have different suggestions for you.

Is your firewall configured correctly? Unless your firewall configuration explicitly exempts all connections on all ports to/from localhost, port 27017 is blocked to/from localhost as well to/from the outside world.

See

https://stefanauwyang.com/setting-iptables-to-allow-port-connection-from-localhost-only/

Substitute 27017 for 8080 in the example.

-- sw

P.S. In your first snippet,

client = MongoClient('localhost', 27017)
db = client["rjh"]

I'll bet the code is throwing a "connection refused" or some other kind of exception. You should probably wrap this snippet in a try...catch block (or, if this is ruby…a begin...rescue block) to catch the exception when it occurs and display the nature of the error. The information will go a long way toward explaining your problem.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct