【发布时间】:2019-07-03 15:57:55
【问题描述】:
我正在尝试使用以下命令连接到 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: 超时
有人知道为什么吗?
【问题讨论】:
-
问题出在你的服务器上,很可能它没有监听你给定的 IP/端口。检查打开的端口并查看您的端口是否正在使用中。确保您的 mongod 服务正在运行。 (不确定)但尝试绑定到 ip 0.0.0.0 而不是 127.0.0.1 - 这可能不是问题。也尝试连接 Mongodb Compass / Shell
-
我的 mongod 正在运行。如何检查我的服务器是否侦听我的给定端口?
-
我假设您使用的是 linux
sudo netstat -tulpn它应该在您的实例上显示开放端口