系統轉移過程當中,擅自把aptitude安裝的mongoengine換成了pip安裝,系統啓動之後,報這個錯誤python
報錯提示:mongodb
- 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的參數:python2.7
- MongoClient(uri, connect=False)
方案2、在初始化MongoClient實例以前,在子進程等幾秒,代碼以下:socket
- 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之後出現的問題,錯誤提示:spa
- [Errno 104] Connection reset by peer
經SoF某貼,認爲是mongodb的讀寫文件數超標了,其實我沒在mongodb的log裏面找到相應信息,只是當時的確作了大數據量的mongodb讀寫操做,因而按照帖子的要求添加/etc/security/limits.d/mongodb.conf 文件,文件內容:server
- mongodb hard nofile 99999
- mongodb soft nofile 99999
- mongodb hard nproc 99999
- mongodb soft nproc 99999
BugFix效果還待驗證接口
參考:SoF某貼:http://stackoverflow.com/questions/30710427/pymongo-and-multiprocessing-serverselectiontimeouterror