V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
dyllen
V2EX  ›  Python

pymongo ssh 跳板机连远程 mongodb 数据库一直 timeout?有有经验的伙计帮忙看看吗?

  •  
  •   dyllen · 32 天前 · 529 次点击
    这是一个创建于 32 天前的主题,其中的信息可能已经有所发展或是发生改变。
    # -*- coding: UTF-8 -*-
    
    import pymongo
    import urllib.parse
    import logging
    from sshtunnel import open_tunnel, SSHTunnelForwarder
    
    sshHost = "1.2.3.4"
    sshPort = 4022
    sshUser = "sshUser"
    sshPass = "sshPass"
    
    mongoHost = '4.5.6.7'
    mongoPort = 27017
    mongoUser = urllib.parse.quote_plus('root')
    mongoPass = urllib.parse.quote_plus('pass123')
    
    server = SSHTunnelForwarder(
        (sshHost, sshPort),
        ssh_username=sshUser,
        ssh_password=sshPass,
        remote_bind_address=(mongoHost, mongoPort),
    )
    
    # server =  open_tunnel(
    #     (sshHost, sshPort),
    #     ssh_username=sshUser,
    #     ssh_password=sshPass,
    #     remote_bind_address=(mongoHost, mongoPort),
    #     local_bind_address=('0.0.0.0', 10022)
    # )
    server.start()
    # mongoDSN = 'mongodb://%s:%s@localhost:10022/dbname?authSource=admin' % (mongoUser, mongoPass)
    # client = pymongo.MongoClient(mongoDSN, server.local_bind_port)
    client = pymongo.MongoClient(
        '127.0.0.1', 
        server.local_bind_port, 
        username=mongoUser, 
        password=mongoPass, 
        authSource='admin'
    )
    db = client[dbName]
    

    先用注释掉的代码试了一下,总提示:

    Could not establish connection from local ('127.0.0.1', 10022) to remote ('4.5.6.7', 27017) side of the tunnel: open new channel ssh error: SSH session not active

    再提示:pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:10022: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

    未注释的代码就提示pymongo.errors.ServerSelectionTimeoutError: 4.5.6.7:27017: timed out

    ip 我隐藏修改过了,不是原本的 ip

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   914 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 20:02 · PVG 04:02 · LAX 13:02 · JFK 16:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.