pymongo.errors.ServerSelectionTimeoutError:localhost:38001:[Errno 111]连接被拒绝

时间:2017-10-18 09:26:25

标签: mongodb pymongo

我在MongoDB和pymongo上遇到了问题。

我测试了pymongo。首先我运行"启动mongod",然后运行以下代码,但它遇到了跟随输出(两者都是mongod和pymongo)。 MongoDB和pymongo似乎都安装成功。 我错过了什么吗?非常感谢提前。

代码:

`from pymongo import MongoClient`
`client=MongoClient(host='localhost', port=38001, serverSelectionTimeoutMS=20000);`
`client.database_names();`

mongod的输出:

`2017-10-17T19:46:46.120+0800 I CONTROL  [initandlisten] MongoDB starting : pid=12933 port=38001 dbpath=/home/jianhaoc/s/cs/run/db 64-bit host=brain4`
`2017-10-17T19:46:46.120+0800 I CONTROL  [initandlisten] db version v3.2.9`
`2017-10-17T19:46:46.120+0800 I CONTROL  [initandlisten] git version: 22ec9e93b40c85fc7cae7d56e7d6a02fd811088c`
`2017-10-17T19:46:46.120+0800 I CONTROL  [initandlisten] allocator: tcmalloc`
`2017-10-17T19:46:46.120+0800 I CONTROL  [initandlisten] modules: none`
`2017-10-17T19:46:46.120+0800 I CONTROL  [initandlisten] build environment:`
`2017-10-17T19:46:46.120+0800 I CONTROL  [initandlisten]     distarch: x86_64`
`2017-10-17T19:46:46.120+0800 I CONTROL  [initandlisten]     target_arch: x86_64`
`2017-10-17T19:46:46.120+0800 I CONTROL  [initandlisten] options: { net: { port: 38001 }, replication: { oplogSizeMB: 64, replSet: "meteor" }, storage: { dbPath: "/home/jianhaoc/s/cs/run/db", jo`
`urnal: { enabled: false } } }`
`2017-10-17T19:46:46.152+0800 I STORAGE  [initandlisten] exception in initAndListen: 98 Unable to lock file: /home/py/run/db/mongod.lock errno:38 Function not implemented. Is a mongod instance already running?, terminating`
`2017-10-17T19:46:46.153+0800 I CONTROL  [initandlisten] dbexit:  rc: 100`

pymongo的输出:

`Traceback (most recent call last):`
`  File "./test_mongo.py", line 10, in <module>`
`    client.database_names();`
`  File "/home/py/anaconda2/lib/python2.7/site-packages/pymongo/mongo_client.py", line 1088, in database_names`
`    "listDatabases")["databases"]]`
`  File "/home/py/anaconda2/lib/python2.7/site-packages/pymongo/database.py", line 478, in command`
`    with client._socket_for_reads(read_preference) as (sock_info, slave_ok):`
`  File "/home/py/anaconda2/lib/python2.7/contextlib.py", line 17, in __enter__`
`    return self.gen.next()`
`  File "/home/py/anaconda2/lib/python2.7/site-packages/pymongo/mongo_client.py", line 798, in _socket_for_reads`
`    with self._get_socket(read_preference) as sock_info:`
`  File "/home/py/anaconda2/lib/python2.7/contextlib.py", line 17, in __enter__`
` self.gen.next()`
`  File "/home/py/anaconda2/lib/python2.7/site-packages/pymongo/mongo_client.py", line 762, in _get_socket`
`    server = self._get_topology().select_server(selector)`
`  File "/home/py/anaconda2/lib/python2.7/site-packages/pymongo/topology.py", line 210, in select_server`
`    address))`
`  File "/home/py/anaconda2/lib/python2.7/site-packages/pymongo/topology.py", line 186, in select_servers`
`._error_message(selector))`
`pymongo.errors.ServerSelectionTimeoutError: localhost:38001: [Errno 111] Connection refused`

1 个答案:

答案 0 :(得分:0)

mongod日志中的此输出表明MongoDB未成功启动:

exception in initAndListen: 98 Unable to lock file: /home/py/run/db/mongod.lock errno:38 Function not implemented. Is a mongod instance already running?, terminating`

/home/py/run/db/mongod.lock中有一个锁定文件。要么已经运行了另一个使用该dbpath的mongod程序,或者使用该路径的mongod,它突然被杀死而没有删除其锁定文件。如果前者为true,请使用不同的dbpath。如果后者为真,请删除锁定文件并重试。