Bug #109366 MySQL Python Connector doesn't work with ssh in the same process
Submitted: 13 Dec 2022 15:06 Modified: 5 Jan 2023 12:03
Reporter: Ahmed Nematallah Email Updates:
Status: Verified Impact on me:
None 
Category:Connector / Python Severity:S2 (Serious)
Version:8.0.30+ OS:Any
Assigned to: CPU Architecture:Any

[13 Dec 2022 15:06] Ahmed Nematallah
Description:
Using MySQL python connector (8.0.30, 8.0.31) with ssh in the same process throws a "mysql.connector.errors.ProgrammingError: Character set 'utf8' unsupported" error. The server version is (5.6.33-log).

Version (8.0.29) works well regardless of anything.

Version (8.0.31) works well when setting up an external SSH connection to the same server (ssh -L 3306:example.com:3306 a@test.com, or creating the tunnel in one terminal window and connecting from another), but when using an SSH tunnel it fails.

Tested both using the sshtunnel library and paramiko/socketserver/threading/multiprocessing.

Note, to reproduce this error, you need to use "mysql.connector.MySQLConnection" as "mysql.connector.connect" simply hangs.

Sample code to set up a tunnel in paramiko/socketserver is here
https://github.com/paramiko/paramiko/blob/main/demos/forward.py

Sample code to connect to a MySQL server using sshtunnel is below:

How to repeat:
import mysql.connector
import sshtunnel

ssh_server='test.com'
ssh_port=22
ssh_uname='usr'
ssh_pwd='pwd'

db_server='example.com'
db_port=3306
db_uname='user'
db_pwd='pass'

tunnel = sshtunnel.SSHTunnelForwarder((ssh_server, ssh_port), ssh_username=ssh_uname, ssh_password=ssh_pwd, remote_bind_address=(db_server, db_port), local_bind_address=("127.0.0.1", 0))

# This line causes an exception
conn = mysql.connector.MySQLConnection(host=tunnel.local_bind_address[0], user=db_uname, password=db_pwd, port=tunnel.local_bind_address[1])

c = conn.cursor()

query = '''SELECT VERSION();'''
c.execute(query)
data = c.fetchall()
print(data)

conn.close()
tunnel.close()
[5 Jan 2023 12:03] MySQL Verification Team
Hello Ahmed,

Thank you for the bug report.
Verified as described.

Regards,
Ashwini Patil