无法使用pymongo连接到MongoDB中的副本集

时间:2019-07-03 15:57:55

标签: mongodb pymongo

我正在尝试使用以下命令连接到MongoDB中的副本集:

client = MongoClient(
        "PublicIP:27017,PublicIP:27017,PublicIP:27017,PublicIP:27017,PublicIP:27017",
        replicaSet="rs0", readPreference='nearest',
        localThresholdMS=500)

db = client.sampledb


# checks the connection to RS in Mongo
try:
    db.command("serverStatus")
except Exception as e:
    print(e)
else:
    print("\n Connection established to the Replica Set!)

我在AWS中设置了5个节点(1个主节点,4个从节点),所有节点中的/etc/mongod.conf文件如下所示:

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

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

    # how the process runs
    processManagement:
      fork: true  # fork and run in background
      pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile

    # network interfaces
    net:
      port: 27017
      #bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.


    security:
      authorization: "disabled"
    #operationProfiling:

    replication:
      replSetName: rs0

当我尝试从本地计算机连接时,会出现如下超时:

  

ip:27017:超时,ip:27017:超时,ip:27017:超时,ip:27017:   ip:27017:超时

有人知道为什么吗?

1 个答案:

答案 0 :(得分:0)

Starting 3.6 try: model=load_model(fname) except: model = createAndTrainModel() 默认为仅本地主机。因此,假设您运行的是3.6或更高版本,则由于注释了bindIp,因此无法从外部访问您的配置。

这篇文章可能有助于理解某些细微差别:https://www.mongodb.com/blog/post/enabling-ip-security-for-mongodb-36-on-ubuntu

显然,不要忘记确保实例上的AWS Security Group允许在端口27017上进行Ingress连接。