OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python mysql-connector hangs indefinitely when connecting to remote mysql via SSH

  • Thread starter Thread starter bcsta
  • Start date Start date
B

bcsta

Guest
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:

Code:
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:

Code:
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?
<p>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:</p>
<pre><code>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()
</code></pre>
<p>However, when using <code>mysql.connector</code> instead of <code>pymysql</code> such as below:</p>
<pre><code>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()
</code></pre>
<p>The code stops at <code>conn = mysql.connector.connect(**config)</code>. It never gives an error or stops, it just hangs on this line.</p>
<p><strong>Why is this?</strong></p>
<p>Aren't the config attributes valid for this module?</p>
 
Top