python连接mongodb集群方法详解

(编辑:jimmy 日期: 2026/3/26 浏览:2)

简单的测试用例

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
from pymongo import MongoClient
# 连接单机
# single mongo
# c = MongoClient(host="192.168.89.151", port=27017)
# 连接集群
c = MongoClient('mongodb://192.168.89.151,192.168.89.152,192.168.89.153')

大型项目的使用方式

三个目录:

目录a为config目录,存放项目的配置文件,主要由conf.ini和__init__.py组成"htmlcode">

import configparser
 
#定义环境变量
env = 'dev'
profile = env + '.'
 
#从conf.ini中读取变量
config = configparser.ConfigParser()
config.read(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'conf.ini'))
 
for section in config.sections():
  for key, val in config.items(section):
    if key.startswith(profile):
      CONFIG_DICT['{0}_{1}'.format(section, key.replace(profile, '').upper())] = val
 
#通过conf.ini定义变量
MONGO_HOST = CONFIG_DICT['MONGO_HOST']

更多关于python连接mongodb集群方法详解的文章请查看下面的相关文章

一句话新闻

高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。