【问题标题】:Connection to remote MySQL db from Python 3.4从 Python 3.4 连接到远程 MySQL 数据库
【发布时间】:2016-04-25 06:28:56
【问题描述】:

我正在尝试使用 Python 3.4 同时连接到两个 MySQL 数据库(一个本地数据库,一个远程数据库),但我真的很挣扎。将问题一分为三:

  • 第一步:连接本地数据库。这工作正常 使用 PyMySQL。 (MySQLdb 与 Python 3.4 不兼容, 当然。)
  • 第二步:连接远程数据库(需要 使用 SSH)。我可以让它在 Linux 命令提示符下工作,但不能 来自 Python... 见下文。
  • 第 3 步:在 同时。我想我应该使用不同的端口 远程数据库,以便我可以同时拥有两个连接 但我在这里超出了我的深度!如果它是相关的,那么这两个数据库将 有不同的名称。如果这个问题没有直接关系, 请告诉我,我会单独发布。

不幸的是,对于新手来说,我并没有真正从正确的地方开始......一旦我能够完成这项工作,我可以很高兴地回到基本的 Python 和 SQL,但希望有人会同情我并帮助我开始吧!

对于第 2 步,我的代码如下。它似乎与回答这个问题Python - SSH Tunnel Setup and MySQL DB Access 的sshtunnel 示例非常接近——尽管它使用了 MySQLdb。目前我正在嵌入连接参数——一旦它正常工作,我会将它们移动到配置文件中。

import dropbox, pymysql, shlex, shutil, subprocess
from sshtunnel import SSHTunnelForwarder
import iot_config as cfg

def CloseLocalDB():
    localcur.close()
    localdb.close()

def CloseRemoteDB():
    # Disconnect from the database
#    remotecur.close()
#    remotedb.close()
    # Close the SSH tunnel
#    ssh.close()
    print("end of CloseRemoteDB function")

def OpenLocalDB():
    global localcur, localdb
    localdb = pymysql.connect(host=cfg.localdbconn['host'], user=cfg.localdbconn['user'], passwd=cfg.localdbconn['passwd'], db=cfg.localdbconn['db'])
    localcur = localdb.cursor()

def OpenRemoteDB():
    global remotecur, remotedb
    with SSHTunnelForwarder(
            ('my_remote_site', 22),
            ssh_username = "my_ssh_username",
            ssh_private_key = "/etc/ssh/my_private_key.ppk",
            ssh_private_key_password = "my_private_key_password",
            remote_bind_address = ('127.0.0.1', 3308)) as server:
        remotedb = None
#Following line gives an error if uncommented
#        remotedb = pymysql.connect(host='127.0.0.1', user='remote_db_user', passwd='remote_db_password', db='remote_db_name', port=server.local_bind_port)
        #remotecur = remotedb.cursor()

# Main program starts here
OpenLocalDB()
CloseLocalDB()
OpenRemoteDB()
CloseRemoteDB()

这是我得到的错误:

2016-04-21 19:13:33,487 | ERROR   | Secsh channel 0 open FAILED: Connection refused: Connect failed
2016-04-21 19:13:33,553 | ERROR   | In #1 <-- ('127.0.0.1', 60591) to ('127.0.0.1', 3308) failed: ChannelException(2, 'Connect failed')
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 60591)
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/sshtunnel.py", line 286, in handle
    src_address)
  File "/usr/local/lib/python3.4/dist-packages/paramiko/transport.py", line 834, in open_channel
    raise e
paramiko.ssh_exception.ChannelException: (2, 'Connect failed')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.4/socketserver.py", line 613, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python3.4/socketserver.py", line 344, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python3.4/socketserver.py", line 669, in __init__
    self.handle()
  File "/usr/local/lib/python3.4/dist-packages/sshtunnel.py", line 296, in handle
    raise HandlerSSHTunnelForwarderError(msg)
sshtunnel.HandlerSSHTunnelForwarderError: In #1 <-- ('127.0.0.1', 60591) to ('127.0.0.1', 3308) failed: ChannelException(2, 'Connect failed')
----------------------------------------
Traceback (most recent call last):
  File "/home/pi/Documents/iot_pm2/iot_ssh_example_for_help.py", line 38, in <module>
    OpenRemoteDB()
  File "/home/pi/Documents/iot_pm2/iot_ssh_example_for_help.py", line 32, in OpenRemoteDB
    remotedb = pymysql.connect(host='127.0.0.1', user='remote_db_user', passwd='remote_db_password', db='remote_db_name', port=server.local_bind_port)
  File "/usr/local/lib/python3.4/dist-packages/pymysql/__init__.py", line 88, in Connect
    return Connection(*args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 678, in __init__
    self.connect()
  File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 889, in connect
    self._get_server_information()
  File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 1190, in _get_server_information
    packet = self._read_packet()
  File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 945, in _read_packet
    packet_header = self._read_bytes(4)
  File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 981, in _read_bytes
    2013, "Lost connection to MySQL server during query")
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')

提前致谢。

【问题讨论】:

  • 嗯...我是个新手,我不太明白这个问题!当我从命令提示符连接时,我没有使用任何选项,所以不,我也没有使用 Python 中的任何选项。我应该看什么样的选择?谢谢。
  • 我认为您可能会朝着正确的方向前进。我尝试了谷歌搜索 SSH 选项,但找不到任何似乎直接相关的内容。你能再解释一下吗? (远程数据库在我的 ISP 的服务器上,所以我不知道它是如何设置的 - 但我可以知道我是否知道我在寻找什么。)@IgnacioVazquez-Abrams

标签: python mysql python-3.x ssh


【解决方案1】:

回答我自己的问题,因为在 J.M. Fernández 在Github 上的大量帮助下,我有一个解决方案:我一开始复制的示例使用端口 3308,但端口 3306 是标准。一旦我改变了它,它就开始工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    相关资源
    最近更新 更多