Hi all, I am running into a similar issue. I had tried to connect to my database on python anywhere directly from my code on my local computer. After reading previous comments, I decided to use the SSHTunnel method. Here is my code for setting everything up.
def data_cleaner():
host = 'xxx.mysql.pythonanywhere-services.com'
user = 'xxx'
password = 'xxx'
db = 'xxx$zipcode_census'
#normally local code cannot interact with pythonanywhere database servers
#must use sshtunnel to set up an external connection
with sshtunnel.SSHTunnelForwarder(
('ssh.pythonanywhere.com'),
ssh_username='xxx',
ssh_password='xxx',
remote_bind_address=(host, 3306)
) as server:
connection = mysql.connector.connect(
host='127.0.0.1',
port=server.local_bind_port,
user=user,
passwd=password,
db=db,
)
#MySQL interface
print("connection established")
cursor = connection.cursor()
However, when I run the code I receive the following error:
2018-06-01 17:26:00,573| ERROR | Could not connect to gateway ssh.pythonanywhere.com:22 : 61
Traceback (most recent call last):
File "US_zipcode_cleaner.py", line 43, in <module>
data_cleaner()
File "US_zipcode_cleaner.py", line 22, in data_cleaner
remote_bind_address=(host, 3306)
File "/usr/local/lib/python3.6/site-packages/sshtunnel.py", line 1486, in __enter__
self.start()
File "/usr/local/lib/python3.6/site-packages/sshtunnel.py", line 1228, in start
reason='Could not establish session to SSH gateway')
File "/usr/local/lib/python3.6/site-packages/sshtunnel.py", line 1038, in _raise
raise exception(reason)
sshtunnel.BaseSSHTunnelForwarderError: Could not establish session to SSH gateway
Any help would be greatly appreciated!