on-internet
← Community

How to access MySQL through SSH tunnel and Python script

Stack Overflow ·

I am using MobaXterm, and have an SSH tunnel connecting port 9997 and 3306 (MySQL). I can access MySQL fine through the command line, but when I try to access it from a Python script (using python3 myscript.py on the command line), I get the error: DatabaseError: 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:9997' (111). Here is myscript.py: import mysql.connector conn = mysql.connector.connect(port=9997, user='---', password='---', database='---') csr = conn.cursor() csr.execute("SELECT * FROM mytable LIMIT 10;") for i in csr: print(i) Why won't it work?

Comments

Sign in or sign up to comment and upvote.

No comments yet.