上一篇描述有誤,這一篇比較詳細,已調試可用 def get_mongodb_client(): ssh_address_or_host = ("xxx.xxx.xxx.xxx",22), ##服務器地址與ssh_port ssh_username = "xxx", ##登錄服務器的用戶 ssh_password = "xxxx", ##登錄服務器的密碼 remote_bind_address = ('xxx.xxx.xxx.xxx', 27017) mongo_user = 'xxxx' ## 訪問數據庫的用戶名 mongo_password = 'xxxxx' #訪問數據庫的密碼 server = SSHTunnelForwarder( ssh_address_or_host=ssh_address_or_host, ssh_username = ssh_username, ssh_password = ssh_password , remote_bind_address = remote_bind_address) server.start() print(server.local_bind_port) client = pymongo.MongoClient('127.0.0.1',server.local_bind_port) ## 這裏一定要填入ssh映射到本地的端口 db = client.admin db.authenticate(mongo_user,mongo_password) return client client = get_mongodb_client() users = client.users print(users) collection = users.users # collection.remove({'mobile':{'$regex':'^123123412.*'}}) r = collection.find({}) for x in r: print(x) client.close()
python通過SSH登錄mongodb
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.