8

I am Testing out connection to mysql server with python. I need to ssh into the server and establish a mysql connection. The following code works:

from sshtunnel import SSHTunnelForwarder
import pymysql
import mysql.connector
    
with SSHTunnelForwarder((ssh_host, 22), ssh_username=ssh_user, ssh_password=ssh_password,
            remote_bind_address=("127.0.0.1", 3306)) as tunnel:

    config = {
                    'user': user,
                    'password': password,
                    'host': tunnel.local_bind_host,
                    'port': tunnel.local_bind_port,
                    'database': db
                }
    
    conn = pymysql.connect(**config)
    query = '''SELECT VERSION();'''
    data = pd.read_sql_query(query, conn)
    print(data)
    connection.close()

However, when using mysql.connector instead of pymysql such as below:

with SSHTunnelForwarder((ssh_host, 22), ssh_username=ssh_user, ssh_password=ssh_password,
                remote_bind_address=("127.0.0.1", 3306)) as tunnel:

        config = {
                        'user': user,
                        'password': password,
                        'host': tunnel.local_bind_host,
                        'port': tunnel.local_bind_port,
                        'database': db
                    }
        
        conn = mysql.connector.connect(**config)
        mycursor = cnx.cursor()
        mycursor.execute("SELECT VERSION()")
        myresult = mycursor.fetchall()

The code stops at conn = mysql.connector.connect(**config). It never gives an error or stops, it just hangs on this line.

Why is this?

Aren't the config attributes valid for this module?

bcsta
  • 1,963
  • 3
  • 22
  • 61
  • I also encounter this issue. It seems like a problem with mysql connector. I also tried to open the tunnel longer and connected to remote MySQL server from terminal and it worked. But nothing works for mysql connector if I use SSH tunnel. Did you resolve it yet? – chique Apr 06 '21 at 09:09
  • 5
    Adding ```use_pure=True``` to mysql connection properties solved it for me. Credits: https://github.com/pahaz/sshtunnel/issues/162#issuecomment-704679385 – André Restivo Jun 04 '21 at 12:02

1 Answers1

7

As there seems to be an aswer here I followed the comment from @André Restivo and it seems to work for me, set use_pure to True. I'm not sure what does this exactly do:

    def __get_db_connection(self) -> None:
        """
        Connect to the database
        """
        try:
            logger.debug(f'Connecting to mysql db "{self.config.mysql_database}"...')
            with mysql.connector.connect(
                host=self.config.mysql_ip,
                port=self.tunnel.local_bind_port,
                user=self.config.mysql_user,
                password=self.config.mysql_password,
                database=self.config.mysql_database,
                use_pure=True
            ) as connection:
                self.connection = connection
                logger.info(connection)
        except mysql.connector as e:
            logger.error(e)
            raise
nck
  • 1,673
  • 16
  • 40
  • I really thank you for this info. I searched through all forums but this simple `use_true` really worked for me. – ejabu Sep 11 '22 at 06:01
  • `use_pure` worked for me, too. I believe `use_pure=True` means, "use the pure Python connector," and `use_pure=False` means, "use the C library installed locally." I'm running on a Mac and had to use `use_pure=True`. – J.D. Pace May 15 '23 at 19:46
  • this worked for me too, it was driving me insane! – jaltiere Aug 01 '23 at 00:39