脚本专栏 发布日期:2025/11/9 浏览次数:1
#!/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集群方法详解的文章请查看下面的相关文章