MongoDB副本集-连接时出现错误“提供的节点名或服务名,或者未知”

时间:2019-11-19 06:11:35

标签: python mongodb server

MongoDB V3.6.15

我尝试设置具有1个主节点,1个辅助节点和1个仲裁器的副本集。连接到副本集时收到错误。 “ pymongo.errors.ServerSelectionTimeoutError:rep2:27017:[Errno 8]提供的节点名或服务名,或未知,rep1:27017:[Errno 8]提供的节点名或服务名,或未知,rep0:27017: [Errno 8]提供的节点名或服务名,或未知”

请在下面找到rs.config()的结果;

{     “ _id”:“ XXXXXXXXX”,     “版本”:3,     “ protocolVersion”:NumberLong(1),     “成员”:[         {             “ _id”:0,             “主机”:“ rep0:27017”,             “ arbiterOnly”:否,             “ 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")
}

}

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

提前谢谢。

用于连接数据库的代码。

import os
import pymongo
import ssl
import urllib
MONGODB_URL="mongodb://user:"+urllib.quote("password")+"@rep0xxxxxxxxxx:27017,rep1xxxxxxxxxx:27017,rep2xxxxxxxxxx:27017/admin?ssl=false&replicaSet=replicaname"
client = pymongo.MongoClient(
    MONGODB_URL
)
db = client.get_default_database()
print (db.collection_names())
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

0 个答案:

没有答案