繁体   English   中英

如何使用mysql-connector-python指定端口号(错误2003)

[英]How to specify port number using mysql-connector-python (error 2003)

我正在尝试通过SSH连接到我本地Windows机器上的MySQL服务器。 SSH连接工作正常,但在运行以下Python脚本时我无法连接到MySQL服务器:

import mysql.connector
import sys
import time
import paramiko

host = 'remote-ssh-host'
i = 1

while True:
    print("Trying to connect to %s (%i/30)" % (host, i))

    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(host, port=22, username='sshuser', password='sshpwd')
        print("Connected to %s" % host)
        break
    except paramiko.AuthenticationException:
        print("Authentication failed when connecting to %s" % host)
        sys.exit(1)
    except:
        print("Could not SSH to %s, waiting for it to start" % host)
        i += 1
        time.sleep(2)

    # If we could not connect within time limit
    if i == 30:
        print("Could not connect to %s. Giving up" % host)
        sys.exit(1)



cnx = mysql.connector.connect(user="mysqluser", password="mysqlpwd",
                              host="mysqlhost",
                              port=3307)

这是输出:

Trying to connect to remote-ssh-host (1/30)
Connected to remote-ssh-host

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 469, in open_connection
    self.sock.connect(sockaddr)
TimeoutError: [WinError 10060]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\path\to\script\ssh_mysql_test.py", line 50, in <module>
    port="3307"
...
  File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 472, in open_connection
    errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'mysqlhost:3307' (10060)

在这里这里已经提出类似的问题,但我认为这个问题与mysql-connector-python处理端口号的连接字符串的方式有关,因为当使用Putty终端时我可以使它工作:

login as: sshuser
sshuser@remote-ssh-host's password:
sshuser@remote-ssh-host:~$ mysql -h mysqlhost -P 3307 -u mysqluser -p
Enter password:
...
mysql>

但是,当以与mysql-connector-python相同的方式指定端口号时:

login as: sshuser
sshuser@remote-ssh-host's password:
sshuser@remote-ssh-host:~$ mysql -h mysqlhost:3307 -u mysqluser -p
Enter password:
ERROR 2005 (HY000): Unknown MySQL server host 'mysqlhost:3307' (0)

我意识到错误数字是不同的(2003年与2005年),但我认为它们是相关的。 所以问题实际上是:我如何格式化端口号,使连接执行为-h hostname -P port而不是-h hostname:port

问题是你的ssh和MySQL连接是完全分开的。 为了使您的MySQL连接能够通过ssh工作,您需要通过ssh 隧道连接MySQL。

您链接第一个SO主题实际上提供了此问题的答案:使用ssh隧道组件创建隧道,并通过隧道引导MySQL连接。

server =  SSHTunnelForwarder(
     ('host', 22),
     ssh_password="password",
     ssh_username="username",
     remote_bind_address=('127.0.0.1', 3306))

上面的代码将ssh连接绑定到localhost的端口3306。 因此,对127.0.0.1:3306所做的任何连接都将通过ssh连接隧道连接到远程服务器。

engine = create_engine('mysql+mysqldb://user:pass@127.0.0.1:%s/db' % server.local_bind_port)

在上面的代码中,MySQL连接到127.0.0.1:3306,因此它将通过ssh连接进行隧道连接。 您需要遵循相同的模式:创建绑定到本地端口的ssh隧道,并在MySQL连接字符串中指定此本地端口而不是远程端口。

暂无
暂无

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

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