Forums

Error setting up mysql db connection via Python - InterfaceError: 2003: (60 Operation timed out)

I have successfully connected to my pythonanywhere mysql databases via python using the code below about 2-3 weeks ago, but now it is not working. Nothing has changed regarding my pythonanywhere account (using paid version) that would cause this issue. I have been able to connect remotely using the DBeaver. Any advice tips would be helpful.

Error message is received about 1m and 15s after code is started, with and without the connection timeout parameter.

Attempt failed using:

tunnel = sshtunnel.SSHTunnelForwarder(
            (c.ssh_host),
            ssh_username=c.sshUsername, ssh_password=c.sshPassword,
            remote_bind_address=c.remoteBindAddress
            )

tunnel.start()

connection = mysql.connector.connect(
        host = c.host,
        database = c.database,
        user = c.user,
        password = c.password,
        port=tunnel.local_bind_port,
        use_pure=True,
    )

Tried using a with statement:

with sshtunnel.SSHTunnelForwarder(
    (c.ssh_host),
    ssh_username=c.sshUsername, ssh_password=c.sshPassword,
    remote_bind_address=c.remoteBindAddress
    ) as tunnel:


    connection = mysql.connector.connect(
    host = c.host,
    database = c.database,
    user = c.user,
    password = c.password,
    port=tunnel.local_bind_port,
    use_pure=True,
    )

Also tried both with a connection time out parameter to the mysql connector:

connection = mysql.connector.connect(
        host = c.host,
        database = c.database,
        user = c.user,
        password = c.password,
        port=tunnel.local_bind_port,
        use_pure=True,
        connection_timeout=120,
    )

Traceback:

--------------------------------------------------------------------------- TimeoutError                              Traceback (most recent call last) File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/mysql/connector/network.py:758, in MySQLTCPSocket.open_connection(self)
    757     self.sock.settimeout(self._connection_timeout)
--> 758     self.sock.connect(sockaddr)
    759 except IOError as err:

TimeoutError: [Errno 60] Operation timed out

The above exception was the direct cause of the following exception:

InterfaceError                            Traceback (most recent call last) Cell In[7], line 8
      1 with sshtunnel.SSHTunnelForwarder(
      2     (c.ssh_host),
      3     ssh_username=c.sshUsername, ssh_password=c.sshPassword,
      4     remote_bind_address=c.remoteBindAddress
      5     ) as tunnel:
----> 8     connection = mysql.connector.connect(
      9     host = c.host,
     10     database = c.database,
     11     user = c.user,
     12     password = c.password,
     13     port=tunnel.local_bind_port,
     14     use_pure=True,
     15     )

File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/mysql/connector/pooling.py:323, in connect(*args, **kwargs)
    321 if CMySQLConnection and not use_pure:
    322     return CMySQLConnection(*args, **kwargs)
--> 323 return MySQLConnection(*args, **kwargs)

File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/mysql/connector/connection.py:179, in MySQLConnection.__init__(self, **kwargs)
    177 if kwargs:
    178     try:
--> 179         self.connect(**kwargs)
    180     except Exception:
    181         # Tidy-up underlying socket on failure
    182         self.close()

File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/mysql/connector/abstracts.py:1399, in MySQLConnectionAbstract.connect(self, **kwargs)    1396     self.config(**kwargs)    1398 self.disconnect()
-> 1399 self._open_connection()    1401 charset, collation = (    1402     kwargs.pop("charset", None),    1403     kwargs.pop("collation", None),    1404 )    1405 if charset or collation:

File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/mysql/connector/connection.py:357, in MySQLConnection._open_connection(self)
    355 self._socket = self._get_connection()
    356 try:
--> 357     self._socket.open_connection()
    359     # do initial handshake
    360     self._do_handshake()

File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/mysql/connector/network.py:760, in MySQLTCPSocket.open_connection(self)
    758     self.sock.connect(sockaddr)
    759 except IOError as err:
--> 760     raise InterfaceError(
    761         errno=2003,
    762         values=(
    763             self.server_host,
    764             self.server_port,
    765             _strioerror(err),
    766         ),
    767     ) from err
    768 except Exception as err:
    769     raise OperationalError(str(err)) from err

InterfaceError: 2003: Can't connect to MySQL server on 'jetchech.mysql.pythonanywhere-services.com:50470' (60 Operation timed out)

Are you able to ssh to your account on PythonAnywhere with the same credentials you use for tunnel?

I have been able to connect using ssh via dbeaver and mysql workbench using the same SSH credentials.

When you call mysql.connector.connect, the host you pass in should be '127.0.0.1', not c.host. You're connecting to a local server (created by the tunnel) that is on your own machine, pretending to be a MySQL server, but actually sending everything over the SSH tunnel.

That resolved the issue! Thank you!!!!

That resolved the issue! Thank you!!!!

No problem, glad to help!