我在 settings.py 中使用具有以下配置的 Django
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '-',
'USER': '-',
'PASSWORD': '-',
'HOST': '-',
'PORT': '-',
'OPTIONS': {'charset': 'utf8mb4'}
}
}
db 服务器在 AWS RDS 上运行。我有两个 EC2 实例,其中一个能够运行完全相同的代码并获取相同的数据,而从第二个 EC2 我收到此错误:
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
django html 响应还显示了这一点:
Unicode error hint
The string that could not be encoded/decoded was: \n<p>�</p>\
此外,负责引发错误的代码片段是:
exp = MyModel.objects.all()
**for e in exp:** <-- this line is throwing the error
#do something
两台服务器上的版本:
EC2-1st具有:
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
而 EC2-2nd 是 EC2-1st 的副本,并应用了更新:
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
此外,我的本地服务器能够使用这些版本的工具正常运行:[我已经使用接近 prod 配置的本地配置在本地导入了 RDS db,以尝试调试问题]。
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)
我应该尝试什么?