Python MongoDB副本集-获取错误“;提供了nodename或servname,或未知;连接时
MongoDB V3.6.15 我正在尝试设置一个主副本集、一个辅助副本集和一个仲裁副本集。连接到副本集时获取错误。 pymongo.errors.ServerSelectionTimeoutError:rep2:27017:[Errno 8]提供了或未知的节点名或服务名,rep1:27017:[Errno 8]提供了或未知的节点名或服务名,rep0:27017: [Errno 8]提供了nodename或servname,或未知“ 请在下面找到rs.config()的结果 { “_id”:“XXXXXXXXX”, “版本”:3, “协议版本”:编号长(1), “成员”:[ { “_id”:0, “主机”:“rep0:27017”, “任意”:错误, “buildIndexes”:正确, “隐藏”:假, "优先":一,, “标签”:{ } 我在配置副本集时出错了什么 提前谢谢 用于连接到数据库的代码Python MongoDB副本集-获取错误“;提供了nodename或servname,或未知;连接时,python,mongodb,server,Python,Mongodb,Server,MongoDB V3.6.15 我正在尝试设置一个主副本集、一个辅助副本集和一个仲裁副本集。连接到副本集时获取错误。 pymongo.errors.ServerSelectionTimeoutError:rep2:27017:[Errno 8]提供了或未知的节点名或服务名,rep1:27017:[Errno 8]提供了或未知的节点名或服务名,rep0:27017: [Errno 8]提供了nodename或servname,或未知“ 请在下面找到rs.config()的结果 { “_id”:“X
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())
您可以发布用于连接的代码吗?@JoeDrumgoole是的,当然……我已经更新了帖子中的代码。您可以ping列为副本集成员的节点吗?您可以连接mongo shell吗?@JoeDrumgoole我无法从主计算机ping辅助计算机……但是,我可以从主计算机连接到辅助计算机mongo shelly、 我缺少什么?发布ping命令和mongoshell命令输出
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