系统转移过程中,擅自把aptitude安装的mongoengine换成了pip安装,系统启动以后,报这个错误
报错提示:
- File "/usr/local/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 748, in _socket_for_reads
- with self._get_socket(read_preference) as sock_info:
- File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
- return self.gen.next()
- File "/usr/local/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 712, in _get_socket
- server = self._get_topology().select_server(selector)
- File "/usr/local/lib/python2.7/dist-packages/pymongo/topology.py", line 141, in select_server
- address))
- File "/usr/local/lib/python2.7/dist-packages/pymongo/topology.py", line 117, in select_servers
- self._error_message(selector))
- ServerSelectionTimeoutError: No servers found yet
因为是系统转移,刚开始是以为mongodb服务器那边服务的问题,后来上网查了一下,发现是pymongo的3.0.*以后版本接口向前兼容的一个bug,SoF某贴(地址见最后)提供了2个方案:
方案一,多传一个connect=False的参数:
- MongoClient(uri, connect=False)
方案二、在初始化MongoClient实例之前,在子进程等几秒,代码如下:
- def start(uri):
- time.sleep(2)
- mclient = MongoClient(uri)
- mclient.db.collection.find_one()
- if __name__ == '__main__':
- p = multiprocessing.Process(target=start, args=('mongodb://localhost:27017/',))
- p.start()
我的解决方案:虽然我用的不是上面任何方案的接口,我使用的是mongoengine的接口函数,底层任然是pymongo
- from mongoengine import connect
按照方案一,在这个connect原调用位置的参数里面加了connect=False,问题解决。
第二个问题,使用新版本pymongo以后出现的问题,错误提示:
- [Errno 104] Connection reset by peer
经SoF某贴,认为是mongodb的读写文件数超标了,其实我没在mongodb的log里面找到相应信息,只是当时的确做了大数据量的mongodb读写操作,于是按照帖子的要求添加/etc/security/limits.d/mongodb.conf 文件,文件内容:
- mongodb hard nofile 99999
- mongodb soft nofile 99999
- mongodb hard nproc 99999
- mongodb soft nproc 99999
BugFix效果还待验证
参考:SoF某贴:http://*.com/questions/30710427/pymongo-and-multiprocessing-serverselectiontimeouterror