简体   繁体   中英

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

I am trying to connect via SSH to a MySQL server from my local Windows machine. The SSH connection works fine, but I am unable to connect to the MySQL server when running the following Python script:

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)

Here is the output:

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)

Similar questions have been asked here and here , but I think this problem has to do with the way mysql-connector-python handles the connection string for the port number, because when using the Putty terminal I can make it work:

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

But, when specifying the port number in the same way as eg the mysql-connector-python does:

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)

I realize the error numbers are different (2003 vs. 2005), but I think that they are related. So the question actually is: How can I format the port number in such a way that the connection executes as -h hostname -P port instead of -h hostname:port ?

The problem is that your ssh and MySQL connections are completely separated. In order for your MySQL connection to work through ssh, you need to tunnel the MySQL connection through the ssh one.

The first SO topic you linked actually provides the answer to this problem: uses an ssh tunnelling component to create the tunnel and the MySQL connection is directed through the tunnel.

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

The above code binds the ssh connection to port 3306 of the localhost. So any connection made to 127.0.0.1:3306 will be tunnelled through the ssh connection to the remote server.

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

In the above code the MySQL connection is made to 127.0.0.1:3306, therefore it will be tunnelled though the ssh connection. You need to follow the same pattern: create an ssh tunnel bound to a local port and in your MySQL connection string specify this local port instead of the remote one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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