在OpenStack中使用python连接MongoDB

问题描述 投票:0回答:1

我有 2 个虚拟机在 VMWare 中运行。一台运行 OpenStack,一台只是一台普通的机器。在 OpenStack 中,我正在使用 MongoDB 4.4 运行 ubuntu 映像。我的 MongoDB 正在使用“私有”接口和浮动“公共”IP 运行。现在的问题是我需要将我的 Rocky 机器连接到 MongoDB。我想用Python向其中添加数据。

我已准备好代码,但出现超时连接错误。我认为这是因为 MongoDB 在本地主机上运行,但是如何在浮动 IP 上运行它?有没有办法或者我需要在openstack中添加一个接口吗?我尝试在“公共”子网中添加一个接口,但它显示状态“向下”,我不知道如何启动它。现在,我只能从我的 rocky 虚拟机 ping MongoDB ubuntu,但仅此而已。帮助!一些Python代码:


# 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

运行代码后rocky vm中出现错误:

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',)>]>


python mongodb pymongo openstack openstack-nova
1个回答
0
投票

事实证明解决方案非常简单,我只是忘记将 MongoDB 端口添加到我的实例上的安全组中。这就是它不断超时的原因。不要忘记检查防火墙!!

© www.soinside.com 2019 - 2024. All rights reserved.