私はsettings.pyで以下の設定でDjangoを使用しています
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '-',
'USER': '-',
'PASSWORD': '-',
'HOST': '-',
'PORT': '-',
'OPTIONS': {'charset': 'utf8mb4'}
}
}
db サーバーは AWS RDS で実行されています。2 つの EC2 インスタンスがあり、そのうちの 1 つはまったく同じコードを実行して同じデータを取得できますが、2 番目の 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 データベースをローカルにインポートしました]。
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)
何を試すべきですか?