PyMongo-无法连接到本地主机?

时间:2019-10-16 09:26:41

标签: python mongodb pymongo

我尝试运行以下内容:

import pymongo
from pymongo import MongoClient

client = MongoClient('mongodb://localhost:27017/')
db = client.test_database
collection = db.test_collection
collection.find_one()

但我明白了

ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

有关如何解决此问题的任何建议?我正在公司代理后面运行它,但是已经在.bashrc中为此设置了环境变量。

编辑

如果我从终端运行mongo,我会得到

$ mongo
MongoDB shell version v4.0.13
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
2019-10-16T10:30:23.269+0100 E QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:344:17
@(connect):2:6
exception: connect failed

1 个答案:

答案 0 :(得分:1)

您似乎没有在计算机上运行任何mongo实例

运行命令mongod -f <config_file>,然后再次进行测试(这可能需要sudo权限)