This may or may not be the answer, but the first thing I would do is attempt to convert the pymysql cursor object to a normalized python type in memory to make sure it does not require any lingering connection just to return the results you are asking for after you have already closed the connection.
MySQL does not actually have a cursor like other SQL dbs do (ex. postgresql ) , so pymysql will create a pythonic SQL standardized ‘cursor-like’ object for it that sometimes can act more like a generator object attached to a stream.
So what I would try first:
mysqlcursor = connection.cursor()
sql_query = 'FROM `your_table` SELECT * LIMIT 5 '
mysqlcursor.execute(sql_query )
#turn the generator-like MySQL cursor object iterator into a list comprehension
mysqlcursor = [ row for row in mysqlcursor ]
#proceed with rest of your code/test as normal
If this does not work with a simple example then you definitely have an ssh tunnel connection issue and it is not some basic anvil server module behavior that behaves differently from your local machine.