简体   繁体   English

如何使用 Python 3.5 通过 SSH 隧道连接到 MySQL 数据库?

[英]How to connect to a MySQL database, via SSH-tunnel, using Python 3.5?

I am using windows 7 with Python 3.4 (with Pycharm) and try to acces a remote mySQL-database through SSH with a private key.我正在使用带有 Python 3.4(带有 Pycharm)的 Windows 7,并尝试使用私钥通过 SSH 访问远程 mySQL 数据库。

I use SSHTunnel ( ghithub_link ) to setup the SSH-Tunnel as follow :我使用 SSHTunnel ( ghithub_link ) 设置 SSH-Tunnel 如下:

    from sshtunnel import SSHTunnelForwarder

server = SSHTunnelForwarder(
    ("blablabla.ddns.net", 3307),
    ssh_host_key=None,
    ssh_username="name",
    ssh_password=None,
    ssh_private_key="E:\id_rsa",
    ssh_private_key_password="pssrd",
    remote_bind_address=('192.168.0.24', 3306))

The I connect to mySQL database using Oracle's MySQL-connector-Python ( source ) as follow :我使用 Oracle 的 MySQL-connector-Python ( source ) 连接到 mySQL 数据库,如下所示:

    cnx=mysql.connector.connect(user='user_worker', password='passwrd',host="blablabla.ddns.net", port=3307, database='base_101')
    cnx.close()

I receive the following error :我收到以下错误:

       Traceback (most recent call last):

      File "C:/Users/PycharmProjects/untitled _Get.py", line 37, in <module> cnx=mysql.connector.connect(user='user_worker', password=’pass',host="blablabla.ddns.net", port=22, database=’base_101')

      File "C:\Users \Python34\site-packages\mysql\connector\__init__.py", line 179, in connectreturn MySQLConnection(*args, **kwargs)

      File "C:\Users \Python34\site-packages\mysql\connector\connection.py", line 95, in __init__self.connect(**kwargs)

      File "C:\Users \Python34\site-packages\mysql\connector\abstracts.py", line 719, in connectself._open_connection()

      File "C:\Users\YOANN\AppData\Roaming\Python\Python34\site-packages\mysq\connector\connection.py", line 206, in _open_connectionnself._socket.open_connection()

      File "C:\Users\YOANN\AppData\Roaming\Python\Python34\site-packages\mysq\connector\network.py", line 475, in open_connection errno=2003, values=(self.get_address(), _strioerror(err)))


   mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '127.0.0.1:3306' (10061 Aucune connexion n’a pu être établie car l’ordinateur cible l’a expressément refusée)

I am assuming the problem comes from the ports, but I can't figure it out.我假设问题来自端口,但我无法弄清楚。 In an other hand I can connect to the base via the SSh and SSh-key using the pycharm database tool, so no problem on the server's side nor the key/ssh connection).另一方面,我可以使用 pycharm 数据库工具通过 SSh 和 SSh-key 连接到 base,因此服务器端和 key/ssh 连接都没有问题)。

You've got your host addresses and ports mixed up.你的主机地址和端口搞混了。 This is what it needs to look like based on your example:根据您的示例,这就是它需要的样子:

server = SSHTunnelForwarder(
    ("blablabla.ddns.net", 22),
    ssh_host_key=None,
    ssh_username="name",
    ssh_password=None,
    ssh_private_key="E:\id_rsa",
    ssh_private_key_password="pssrd",
    remote_bind_address=("127.0.0.1", 3306))

server.start()

cnx=mysql.connector.connect(user='user_worker',
    password='passwrd',
    host="127.0.0.1", 
    port=server.local_bind_port, 
    database='base_101')

# Do some DB stuff...

cnx.close()
server.stop()

This configuration assumes that you are tunneling directly to the MySQL server.此配置假定您直接通过隧道连接到 MySQL 服务器。 If you are not (like when proxying through a DMZ or NAT) then the remote_bind_address will need to be the MySQL server address instead of the loopback.如果不是(例如通过 DMZ 或 NAT 代理时),则 remote_bind_address 将需要是 MySQL 服务器地址而不是环回。

Using the pymysql lib in python code solves the problem.在python代码中使用pymysql lib解决了这个问题。

I hope it helps我希望它有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM