default database port change throws error for raw queries

I have in my test server’s .env : DATABASE_PORT=9812
And in my settings.py :

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': os.environ['DATABASE_NAME'],
        'USER': os.environ['DATABASE_USER'],
        'PASSWORD': os.environ['DATABASE_PASS'],
        'HOST': os.environ['DATABASE_HOST'],
        'PORT': os.environ['DATABASE_PORT'],
        'OPTIONS': {
            'init_command': "SET sql_mode=''",
        },
    },

I have this custom query and not using ORM :

cursor_default = connections['default'].cursor()
query = f"""
SELECT
a_complex_query
"""
cursor_default.execute(query)
rules = cursor_default.fetchall()

When running this in terminal via source env/bin/activate and python manage.py runserver it works properly.

But when I run this as a service, sudo service myProject start it throws and error on that page.

myProject service has this :

ExecStart=/bin/bash -c 'source /var/www/myProject/env/bin/activate && /var/www/myProject/env/bin/gunicorn -c gunicorn_config.py myProject.wsgi'
OperationalError at /some/path/2034/update
(2006, '')

Traceback (most recent call last):
  File "/var/www/myProject/env/lib/python3.11/site-packages/django/db/backends/utils.py", line 87, in _execute
    return self.cursor.execute(sql)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/myProject/env/lib/python3.11/site-packages/django/db/backends/mysql/base.py", line 75, in execute
    return self.cursor.execute(query, args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/myProject/env/lib/python3.11/site-packages/MySQLdb/cursors.py", line 183, in execute
    while self.nextset():
          ^^^^^^^^^^^^^^
  File "/var/www/myProject/env/lib/python3.11/site-packages/MySQLdb/cursors.py", line 137, in nextset
    nr = db.next_result()
         ^^^^^^^^^^^^^^^^

The above exception ((2006, '')) was the direct cause of the following exception:
  File "/var/www/myProject/env/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/myProject/env/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/myProject/server/views.py", line 1524, in azureUpdate
    cursor_default.execute(query)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/myProject/env/lib/python3.11/site-packages/django/db/backends/utils.py", line 102, in execute
    return super().execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/myProject/env/lib/python3.11/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
           
  File "/var/www/myProject/env/lib/python3.11/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/myProject/env/lib/python3.11/site-packages/django/db/backends/utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/myProject/env/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/myProject/env/lib/python3.11/site-packages/django/db/backends/utils.py", line 87, in _execute
    return self.cursor.execute(sql)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/myProject/env/lib/python3.11/site-packages/django/db/backends/mysql/base.py", line 75, in execute
    return self.cursor.execute(query, args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/myProject/env/lib/python3.11/site-packages/MySQLdb/cursors.py", line 183, in execute
    while self.nextset():
          ^^^^^^^^^^^^^^
  File "/var/www/myProject/env/lib/python3.11/site-packages/MySQLdb/cursors.py", line 137, in nextset
    nr = db.next_result()
         ^^^^^^^^^^^^^^^^

Exception Type: OperationalError at /some/path/2034/update
Exception Value: (2006, '')

Are you setting the right user and directory in your service file? Are you setting any necessary environment variables in the service file?
(It would be helpful if you posted the complete file.)

I had to resort to using chatgpt for this and this worked when entered in the service file :

# Load environment variables from .env file
EnvironmentFile=/var/www/myProject/myProject/.env

EDIT : No, its still returning the same 2006 error at times.