I'm using Django with below config in settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '-',
'USER': '-',
'PASSWORD': '-',
'HOST': '-',
'PORT': '-',
'OPTIONS': {'charset': 'utf8mb4'}
}
}
The db server is running on AWS RDS. I have two EC2 instances, one of them is able to run the exact same code and fetch same data while from the second EC2 I'm getting this error:
return self._execute_with_wrappers(sql params many=False executor=self._execute)
File "/home/ubuntu/.virtualenvs/python39/lib/python3.9/site-packages/django/db/backends/utils.py" line 75 in _execute_with_wrappers
return executor(sql params many context)
File "/home/ubuntu/.virtualenvs/python39/lib/python3.9/site-packages/django/db/backends/utils.py" line 84 in _execute
return self.cursor.execute(sql params)
File "/home/ubuntu/.virtualenvs/python39/lib/python3.9/site-packages/django/db/backends/mysql/base.py" line 73 in execute
return self.cursor.execute(query args)
File "/home/ubuntu/.virtualenvs/python39/lib/python3.9/site-packages/MySQLdb/cursors.py" line 206 in execute
res = self._query(query)
File "/home/ubuntu/.virtualenvs/python39/lib/python3.9/site-packages/MySQLdb/cursors.py" line 321 in _query
self._post_get_result()
File "/home/ubuntu/.virtualenvs/python39/lib/python3.9/site-packages/MySQLdb/cursors.py" line 355 in _post_get_result
self._rows = self._fetch_row(0)
File "/home/ubuntu/.virtualenvs/python39/lib/python3.9/site-packages/MySQLdb/cursors.py" line 328 in _fetch_row
return self._result.fetch_row(size self._fetch_type)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 180: invalid start byte
The django html response additionally shows this:
Unicode error hint
The string that could not be encoded/decoded was: \n<p>�</p>\
Also the code snippet responsible for throwing the error is:
exp = MyModel.objects.all()
**for e in exp:** <-- this line is throwing the error
#do something
Versions on both servers:
EC2-1st has:
Ubuntu 16.04.4
Django==1.11.2
mysqlclient==1.3.10
django-mysql==2.1.0
python3 --version
Python 3.5.2
mysql --version
mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrapper
while EC2-2nd is a replica of EC2-1st with updates applied:
Ubuntu 20.04.3
Django==3.2.6
mysqlclient==2.0.3
django-mysql==3.10.0
python3 --version
Python 3.9.5
mysql --version
mysql Ver 14.14 Distrib 5.7.35, for Linux (x86_64) using EditLine wrapper
Also, my local server is able to run fine with these versions of the tools: [I had imported the RDS db locally with a local config that's close to the prod config to try debugging the issue].
Mac OS 11.5.2
Django==3.2.6
mysqlclient==2.0.3
django-mysql==3.10.0
Python 3.9.6
mysql Ver 8.0.25 for macos11.3 on x86_64 (Homebrew)
What should I try?