连接时,MongoDB副本集获取错误“nodename nor servname provided,or not known”

2024-06-28 20:28:37 发布

您现在位置:Python中文网/ 问答频道 /正文

MongoDB V3.6.15版

我试图设置副本集与1个主要,1个次要和1个仲裁。连接到副本集时出错。 "pymongo.errors.ServerSelectionTimeoutError:rep2:27017:[Errno 8]提供了nodename或servname,或不知道,rep1:27017:[Errno 8]提供了nodename或servname,或不知道,rep0:27017: [Errno 8]提供了nodename或servname,或未知“

请在下面找到rs.配置()

在{ “_id”:“XXXXXXXXX”, “版本”:3, “协议版本”:数字长(1), 成员:[ { “_id”:0, “host”:“rep0:27017”, “仲裁者”:错误, “buildIndexes”:正确, “隐藏”:错误, “优先权”:1, “标签”:{

        },
        "slaveDelay" : NumberLong(0),
        "votes" : 1
    },
    {
        "_id" : 1,
        "host" : "rep1:27017",
        "arbiterOnly" : false,
        "buildIndexes" : true,
        "hidden" : false,
        "priority" : 1,
        "tags" : {

        },
        "slaveDelay" : NumberLong(0),
        "votes" : 1
    },
    {
        "_id" : 2,
        "host" : "rep2:27017",
        "arbiterOnly" : true,
        "buildIndexes" : true,
        "hidden" : false,
        "priority" : 0,
        "tags" : {

        },
        "slaveDelay" : NumberLong(0),
        "votes" : 1
    }
],
"settings" : {
    "chainingAllowed" : true,
    "heartbeatIntervalMillis" : 2000,
    "heartbeatTimeoutSecs" : 10,
    "electionTimeoutMillis" : 10000,
    "catchUpTimeoutMillis" : -1,
    "catchUpTakeoverDelayMillis" : 30000,
    "getLastErrorModes" : {

    },
    "getLastErrorDefaults" : {
        "w" : 1,
        "wtimeout" : 0
    },
    "replicaSetId" : ObjectId("5dd267845cdd7ff56f6c0fe5")
}

}

我在配置副本集时做了什么错误。在

提前谢谢。在

用于连接到数据库的代码。在

^{pr2}$
Steps

1. Create 3 VMs on Azure Cloud in a same resource group

2. Install MongoDB 3.6.15 on all the Machines

3. Add User on the 3 machines.
db.createUser(
  {
    user: "xxxxxxxxx",
    pwd: "xxxxxxxxx",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
);
db.createUser(
  {
    user: "xxxxxxxxx",
    pwd: "xxxxxxxxx",
    roles: [ { role: "clusterAdmin", db: "admin" }, "readWriteAnyDatabase" ]
  }
);

4. Generate a Key using OpenSSL
openssl rand -base64 756 > /home/rep0/repkey
chmod 400 /home/rep2/repkey
sudo chown mongodb:mongodb /home/rep0/repkey

5. Edit /etc/mongod.conf to enable authentication
security:
  authorization: enabled
  keyFile: /home/rep0/repkey

6. Restart the 3 machines

7. Edit /etc/mongod.conf to add replica set config
replication:
  replSetName: myReplica
  enableMajorityReadConcern: false

8. Restart the 3 machines

9. Add the hosts on all the VM's
Private-IP-Address rep0
Private-IP-Address rep1
Private-IP-Address rep2

10. Open MongoShell for primary machine (one of the 3)

11. execute the command to start replica set and add secondaries.
rs.initiate();
rs.add();
rs.add("host":"rep1");
rs.addArb("rep2");

12. import sample data on the primary machine and confirm the same is replicated on the secondary machines

Tags: theidfalsetruehosthomedbon