Search code examples
pythonmongodbpymongoopenstackopenstack-nova

Using python to connect to MongoDB in OpenStack


I have 2 VM's running in VMWare. One is running OpenStack and one is just a regular rocky machine. In OpenStack I'm running an ubuntu image with MongoDB 4.4. My MongoDB is running with a 'private' interface and a floating 'public' ip. The issue now is I need to connect my rocky machine to the MongoDB. I want to add data into it with Python.

I have the code ready but I'm getting timeout connection errors. I assume it's because MongoDB is running on localhost, but how do I run it on the floating ip? Is there a way or do I need to add an interface in openstack? I tried adding an interface in the 'public' subnet but it says status 'DOWN' and I have no idea how to get it up. Right now, I can only ping the MongoDB ubuntu from my rocky vm, but that's all. Help! SOME of the python code:


# MongoDB setup
client = pymongo.MongoClient("mongodb://192.168.1.53:27017/")
database = client["climdns"]
collection = database["dnszone"]

...
# Add record to MongoDB
        collection.insert_one({
            'fqdn': domain,
            'ipv4': ipv4,
            'last_change': datetime.now(),
            'user': {
                'email': email,
                'id': user_id
            }
    })
...
def login(provider_name):
    # We need response object for the WerkzeugAdapter.
    response = make_response()
    # Log the user in, pass it the adapter and the provider name.
    result = authomatic.login(WerkzeugAdapter(request, response), provider_name)

    # If there is no LoginResult object, the login procedure is still pending.
    if result:
    if result.user:
            # We need to update the user to get more info.
           result.user.update()
           if result.user.email and result.user.id:
               headers = {'email': result.user.email, 'user_id': result.user.id}
               response = requests.post('http://localhost:5001/add_dns', headers=headers)
               return response.json()

           return gui_index()

mongod.conf:

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

OpenStack dashboard

error in rocky vm after running code:

pymongo.errors.ServerSelectionTimeoutError: 192.168.1.53:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 6611380c7c10562e5bc268a4, topology_type: Unknown, servers: [<ServerDescription ('192.168.1.53', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('192.168.1.53:27017: timed out',)>]>



Solution

  • Turns out the solution was very simple, I just forgot to add the MongoDB port to the security group that was on my instance. That’s why it kept timing out. Don’t forget to check the firewall!!