UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 6: invalid continuation byte

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 6: invalid continuation byte

所以当我将我的 mint 19 升级到 20 时,这就开始了。 完整错误:

Traceback (most recent call last):
  File "/home/notification/views.py", line 206, in get
    .select_related("history__definition")
  File "/home/linuxbrew/.linuxbrew/opt/python/lib/python3.7/site-packages/django/db/models/query.py", line 653, in first
    for obj in (self if self.ordered else self.order_by('pk'))[:1]:
  File "/home/linuxbrew/.linuxbrew/opt/python/lib/python3.7/site-packages/django/db/models/query.py", line 274, in __iter__
    self._fetch_all()
  File "/home/linuxbrew/.linuxbrew/opt/python/lib/python3.7/site-packages/django/db/models/query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/linuxbrew/.linuxbrew/opt/python/lib/python3.7/site-packages/django/db/models/query.py", line 55, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/linuxbrew/.linuxbrew/opt/python/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1133, in execute_sql
    cursor.execute(sql, params)
  File "/home/linuxbrew/.linuxbrew/opt/python/lib/python3.7/site-packages/debug_toolbar/panels/sql/tracking.py", line 192, in execute
    return self._record(self.cursor.execute, sql, params)
  File "/home/linuxbrew/.linuxbrew/opt/python/lib/python3.7/site-packages/debug_toolbar/panels/sql/tracking.py", line 126, in _record
    return method(sql, params)
  File "/home/linuxbrew/.linuxbrew/opt/python/lib/python3.7/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/home/linuxbrew/.linuxbrew/opt/python/lib/python3.7/site-packages/sentry_sdk/integrations/django/__init__.py", line 469, in execute
    return real_execute(self, sql, params)
  File "/home/linuxbrew/.linuxbrew/opt/python/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/linuxbrew/.linuxbrew/opt/python/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/linuxbrew/.linuxbrew/opt/python/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/linuxbrew/.linuxbrew/opt/python/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 73, in execute
    return self.cursor.execute(query, args)
  File "/home/linuxbrew/.linuxbrew/opt/python/lib/python3.7/site-packages/MySQLdb/cursors.py", line 206, in execute
    res = self._query(query)
  File "/home/linuxbrew/.linuxbrew/opt/python/lib/python3.7/site-packages/MySQLdb/cursors.py", line 321, in _query
    self._post_get_result()
  File "/home/linuxbrew/.linuxbrew/opt/python/lib/python3.7/site-packages/MySQLdb/cursors.py", line 355, in _post_get_result
    self._rows = self._fetch_row(0)
  File "/home/linuxbrew/.linuxbrew/opt/python/lib/python3.7/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 0xe3 in position 11: invalid continuation byte

数据库是MySQL,配置为utf8mb4

mysql> SHOW VARIABLES LIKE 'char%';
Variable_name           |Value                     |
------------------------|--------------------------|
character_set_client    |utf8mb4                   |
character_set_connection|utf8mb4                   |
character_set_database  |utf8mb4                   |
character_set_filesystem|binary                    |
character_set_results   |                          |
character_set_server    |latin1                    |
character_set_system    |utf8                      |
character_sets_dir      |c:\mariadb\share\charsets\|

抛出此错误的行是这一行,查看十六进制,没问题。

description                                 |hex(description)                                                                          |
--------------------------------------------|------------------------------------------------------------------------------------------|
Necessária para as partidas na 'batalha'|4E6563657373C3A17269612070617261206173207061727469646173206E612027626174616C6861206A6F7927|

á = C3 A1

有人在放属于cp1252的0xe1。我深入调试,但转换似乎发生在 MysqlDB 库上。

关于环境的更多信息:

pip3 list | grep -i mysql
mysql-connector-python                8.0.20   
mysql-connector-python-rf             2.2.2    
mysqlclient                           2.0.1    
PyMySQL                               0.9.3

您的 character_set_% 设置看起来很奇怪:

| character_set_client     | latin7          | names
| character_set_connection | latin7          | names
| character_set_database   | utf8mb4         | ?
| character_set_filesystem | binary          | hands-off
| character_set_results    | latin7          | names
| character_set_server     | utf8mb4         | ?
| character_set_system     | utf8            | hands-off

我已将它们标记为 3 组:

  • “放开手脚”——文件系统和系统不应修改默认值,否则内部的东西可能会损坏。
  • "names" -- 例如,SET NAMES latin7 是您指定 客户端 使用 latin7 编码的方式。总体趋势是从旧的默认值 latin1 转向未来的标准 utf8mb4。 (我使用 latin7 只是为了让它脱颖而出。
  • “?” -- 目前还不清楚这两个有什么影响。我建议将它们与安装的值一起保留,在 MySQL/MariaDB.
  • 的最新版本中可能是 utf8mb4 (对于两者)

在 5.7.6 中,GLOBAL character_set_databasecollation_database 系统变量已弃用; SESSION 版本变为只读(弃用)

来自 8.0.1 更新日志:

重要更改:默认字符集已从 latin1 更改为 utf8mb4。这些系统变量受到影响:

  • character_set_server 和 character_set_database 系统变量的默认值已从 latin1 更改为 utf8mb4。

  • collation_server 和 collation_database 系统变量的默认值已从 latin1_swedish_ci 更改为 utf8mb4_0900_ai_ci。

因此,新对象的默认字符集和排序规则与以前不同,除非指定了明确的字符集和排序规则。这包括数据库和其中的对象,例如表、视图和存储的程序。保留以前默认值的一种方法是使用 my.cnf 文件中的这些行启动服务器:

[mysqld]
character_set_server=latin1
collation_server=latin1_swedish_ci