Forums

Failing to connect to remote MySQL with Django

Hello...

When trying to start my django site, I can't seem to make a connection remotely (using Windows). I used this article: https://help.pythonanywhere.com/pages/AccessingMySQLFromOutsidePythonAnywhere/

What I did/know:

  • I can connect to SQL Workbench remotely through the tunnel.
  • I can connect from my 2 sites hosted on pythonanywhere without the tunnel just fine.
  • The dev system is set to use the database at 127.0.0.1 per the docs.
  • The issue is only in my dev environment remotely, and I am connected to the tunnel over SSH when the error occurs.

I keep getting this traceback, anyone have any ideas?

System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "(path removed)\lib\site-packages\django\db\backends\base\base.py", line 289, in ensure_connection
    self.connect()
  File "(path removed)\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "(path removed)\lib\site-packages\django\db\backends\base\base.py", line 270, in connect
    self.connection = self.get_new_connection(conn_params)
  File "(path removed)\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "(path removed)\lib\site-packages\django\db\backends\mysql\base.py", line 247, in get_new_connection
    connection = Database.connect(**conn_params)
  File "(path removed)\lib\site-packages\MySQLdb\__init__.py", line 123, in Connect
    return Connection(*args, **kwargs)
  File "(path removed)\lib\site-packages\MySQLdb\connections.py", line 185, in __init__
    super().__init__(*args, **kwargs2)
MySQLdb.OperationalError: (2013, "Lost connection to server at 'handshake: reading initial communication packet', system error: 0")

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

Traceback (most recent call last):
  File "C:\Python\Python310\lib\threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "C:\Python\Python310\lib\threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "(path removed)\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "(path removed)\lib\site-packages\django\core\management\commands\runserver.py", line 136, in inner_run
    self.check_migrations()
  File "(path removed)\lib\site-packages\django\core\management\base.py", line 574, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "(path removed)\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "(path removed)\lib\site-packages\django\db\migrations\loader.py", line 58, in __init__
    self.build_graph()
  File "(path removed)\lib\site-packages\django\db\migrations\loader.py", line 235, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "(path removed)\lib\site-packages\django\db\migrations\recorder.py", line 81, in applied_migrations
    if self.has_table():
  File "(path removed)\lib\site-packages\django\db\migrations\recorder.py", line 57, in has_table
    with self.connection.cursor() as cursor:
  File "(path removed)\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "(path removed)\lib\site-packages\django\db\backends\base\base.py", line 330, in cursor
    return self._cursor()
  File "(path removed)\lib\site-packages\django\db\backends\base\base.py", line 306, in _cursor
    self.ensure_connection()
  File "(path removed)\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "(path removed)\lib\site-packages\django\db\backends\base\base.py", line 288, in ensure_connection
    with self.wrap_database_errors:
  File "(path removed)\lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "(path removed)\lib\site-packages\django\db\backends\base\base.py", line 289, in ensure_connection
    self.connect()
  File "(path removed)\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "(path removed)\lib\site-packages\django\db\backends\base\base.py", line 270, in connect
    self.connection = self.get_new_connection(conn_params)
  File "(path removed)\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "(path removed)\lib\site-packages\django\db\backends\mysql\base.py", line 247, in get_new_connection
    connection = Database.connect(**conn_params)
  File "(path removed)\lib\site-packages\MySQLdb\__init__.py", line 123, in Connect
    return Connection(*args, **kwargs)
  File "(path removed)\lib\site-packages\MySQLdb\connections.py", line 185, in __init__
    super().__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2013, "Lost connection to server at 'handshake: reading initial communication packet', system error: 0")

From the error message I guess that it's coming from your local machine and not being displayed on PythonAnywhere. Could you provide more context to how your dev environment is set up? Is also running on PythonAnywhere or is it on only on your local machine?

Yes, so my envionment is running from a git reposititory in dev. So, I do a pull of any pushed code into my 2nd app (not the live one).

My dev environment is a VM running Windows on my laptop. Like I said, I can connect to MySQL from the MySQL workbench app. I can also connect via putty.

One thing to note, maybe this can be the kicker.. I am running in a virtual environment when I start the django app on the laptop VM. Otherwise, the same code works on a pythonanywhere server (obviously different MySQL server name). Then again, I am in a virtual environment in pythonanywhere too.

As per the docs, I change the server to 127.0.0.1, and just no connection made to pythonanywhere. Maybe I can do the same from MySQL workbench and see if that also hangs (doubting it's the virtual environment, but seen stranger things in my career).

So you're trying to connect with your PA database from the Windows VM running locally and that's failing, right?

From the webapp, it is failing using the Putty tunnel. If I connect to MySQL Workbench from the same machine, it works fine.

Could you share your database settings that are running in your dev environment and how the tunnel is established? You can email us at support@pythonanywhere.com if you do not want to share it on public forums.