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?