我已经安装了mogodb linode"mongodb应用程序;以及它的运行。。创建了用户和集合,并能够从shell访问它现在尝试从本地连接到服务器,但服务器超时''
pymongo.errors.ServerSelectionTimeoutError: 170.187.248.15:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 6218a8cccb94c35a2569ce55, topology_type: Single, servers: [<ServerDescription ('170.187.248.15', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('170.187.248.15:27017: timed out')>]>
default_config = {'MONGODB_SETTINGS': {
#Local DB
'db': 'test_db',
'host': 'localhost',
'port': 27017
#linode "mongodb-app"
'db': '<db name>',
'host': '<server ip>',
'port': 27017,
'username': '<db user>,
'password': '<db password>',
'authentication_source': 'admin'
}
"我遵循了以下几个步骤:''
1. updated /etc/mongod.conf from the server
net:
port: 27017
bindIp:0.0.0.0
''
2. sudo ufw status
sudo: unable to resolve host 170-187-248-15
Status: active
To Action From
-- ------ ----
22 ALLOW Anywhere
27017 ALLOW 0.0.0.0
22 (v6) ALLOW Anywhere (v6)
但是仍然面临相同的错误
在运行mongodb的linux服务器上,运行以下两个命令:
iptables -A INPUT -s <ip-address-of-your-app> -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -d <ip-address-of-your-app> -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT
取自https://docs.mongodb.com/manual/tutorial/configure-linux-iptables-firewall/#traffic-往返于mongod实例