macbook中的localhost在python中用于MongoDB pymongo

2024-09-20 22:54:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Macbook中使用Spyder4将python与Anaconda Navigator结合使用。 我试图用python创建MongoDB数据库,但我遇到了一个错误,这是我的代码行话。我尝试过更改我的本地主机路径,但从internet获得的路径无效。 我需要帮助来理解这个问题。 下面是我的代码和我得到的错误


    import pymongo
    myclient = pymongo.MongoClient("mongodb://localhost:27017/")
    mydb = myclient["mydatabase"]
    mycol = mydb["customers"]
    print(mydb.list_collection_names())
    
    ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused, Timeout: 30s, 
    Topology Description: <TopologyDescription id: 60b12ac2a840595f4d119d65, topology_type: 
    Single, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, 
    error=AutoReconnect('localhost:27017: [Errno 61] Connection refused')

运行此代码时的错误如下所示

我认为localhost路径有问题,但即使尝试了一些来自internet的路径建议,也无法解决问题

我的系统配置

Macbook OSx Mojave 10.14
水蟒导航器1.10.0
Spyder 4.1.5
Python 3.8.1 64位

Tags: 代码路径localhosttype错误anacondaconnectioninternet

热门问题