skip to Main Content

Hi I am currently trying to set up a mongo db on my home server and expose it to the internet using cloudflare tunnels.

I have a service up and running and have the following for the connection.
client = MongoClient('<DATABASE_URL>')

I get this error…
pymongo.errors.InvalidURI: Invalid URI scheme: URI must begin with 'mongodb://' or 'mongodb+srv://'

I am tunneling the default ip that mongo gives you.

UPDATE
I tested connecting to the db and just printing the database to the console. I got this result

Database(MongoClient(host=['<my_domain>:27107'], document_class=dict, tz_aware=False, connect=True), 'test_db')

I assume that because it says "connect=true" that means it is connecting to the database now.
I tried to add a collection to the database using an example I got online and this is the error I received…

Traceback (most recent call last):
  File "/home/michael/mongo.py", line 18, in <module>
    x = mycol.insert_one(mydict)
  File "/home/michael/anaconda3/lib/python3.9/site-packages/pymongo/collection.py", line 628, in insert_one
    self._insert_one(
  File "/home/michael/anaconda3/lib/python3.9/site-packages/pymongo/collection.py", line 569, in _insert_one
    self.__database.client._retryable_write(acknowledged, _insert_command, session)
  File "/home/michael/anaconda3/lib/python3.9/site-packages/pymongo/mongo_client.py", line 1475, in _retryable_write
    with self._tmp_session(session) as s:
  File "/home/michael/anaconda3/lib/python3.9/contextlib.py", line 119, in __enter__
    return next(self.gen)
  File "/home/michael/anaconda3/lib/python3.9/site-packages/pymongo/mongo_client.py", line 1757, in _tmp_session
    s = self._ensure_session(session)
  File "/home/michael/anaconda3/lib/python3.9/site-packages/pymongo/mongo_client.py", line 1740, in _ensure_session
    return self.__start_session(True, causal_consistency=False)
  File "/home/michael/anaconda3/lib/python3.9/site-packages/pymongo/mongo_client.py", line 1685, in __start_session
    self._topology._check_implicit_session_support()
  File "/home/michael/anaconda3/lib/python3.9/site-packages/pymongo/topology.py", line 538, in _check_implicit_session_support
    self._check_session_support()
  File "/home/michael/anaconda3/lib/python3.9/site-packages/pymongo/topology.py", line 554, in _check_session_support
    self._select_servers_loop(
  File "/home/michael/anaconda3/lib/python3.9/site-packages/pymongo/topology.py", line 238, in _select_servers_loop
    raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: No servers found yet, Timeout: 30s, Topology Description: <TopologyDescription id: 63d172246419f5effc5e32d3, topology_type: Unknown, servers: [<ServerDescription ('<my_domain>', 27107) server_type: Unknown, rtt: None>]> 

For reference this is what my pymongo test file looks like.
mongo.py

import pymongo

con = pymongo.MongoClient("mongodb://<my_domain>:27107")
db = con["test_db"]

mycol = db["customers"]
print(mycol)
print(db)
mydict = { "name": "John", "address": "Highway 37" }

x = mycol.insert_one(mydict)

2

Answers


  1. If it’s a standard installation, you need to make sure cloudflare tunnel is exposing port 27017. The ingress rule must be:

    tcp://localhost:27017
    

    To connect, just use:

    pymongo.MongoClient("mongodb://user:[email protected]/table")
    

    It’s a good idea to activate authentication if you’re exposing the whole server to the internet. You can do it by setting authentication on the mongodb server, or at the cloudflare zero trust edge following this guide:
    https://developers.cloudflare.com/cloudflare-one/tutorials/mongodb-tunnel/

    Login or Signup to reply.
  2. I guess this is the case here:

    • you have a locally deployed mongodb (not some external VM)
    • you’ve set a Cloudflare tunnel in order to expose mongodb over dns
    • and you are having problems to connect to mongodb using that dns

    So I’ve recently been trying to do the same, and I got over it with these steps:

    • First off, make sure that your service type, in Cloudflare Zero Trust, is TCP
    • URL is probably localhost, make sure you specified port
    • download cloudflared: Apple Silicon & everything else probably
    • run this on your local machine that you want to connect from: cloudflared access tcp --hostname <hostname you've set on Cloudflare ZT> --url <url you want to be forwarded to>. For example: cloudflared access tcp --hostname mongo.example.com --url localhost:3000

    Then try to connect with your app to the localhost:3000.

    How does this work?

    • Well, first you install cloudflared service, which forwards encrypted connection from an app on your machine to the outer internet.

    • You can protect access to that forwarded service/app using access rules. I also recommend protecting your app/service, you can do it from MongoDB or Cloudflare ZT, or both.

    • Then, you run cloudflared app on your target machine

    • connect to Cloudflare servers which forwards your MongoDB instance connection to the specified port on your local machine

    • you can access it as its local deployment

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search