Character set 'utf8mb4' is not a compiled character set

最近在一次MySQL数据迁移的过程中遭遇了字符集的问题,提示为"Character set 'utf8mb4' is not a compiled character set"。即是字符集utf8mb4不是一个编译的字符集以及没有在Index.xml文件里指定。下面是其处理过程及解决办法,供大家参考。

1、错误提示
SHELL> mysqlbinlog --database=bs_salary --stop-datetime="2014-12-15 8:24:48" /home/robin/mysql-bin.000399 \
> |mysql -uroot -p --force --database=salary_1215
mysql: Character set 'utf8mb4' is not a compiled character set and is not specified in the '/usr/share/mysql/charsets/Index.xml' file

2、分析与解决
a、错误分析
从给出的错误提示来看,说utf8mb4字符集未被编译,而事实上这个字符集根本没有用到。
其次说这个文件里/usr/share/mysql/charsets/Index.xml未指定utf8mb4字符集,需要检查字符集目录设置。

b、字符集检查
首先检查了新旧环境的字符集设置,两边都为UTF8,如下
mysql> show variables like '%char%';
+--------------------------+---------------------------------+
| Variable_name            | Value                          |
+--------------------------+---------------------------------+
| character_set_client    | utf8                            |
| character_set_connection | utf8                            |
| character_set_database  | utf8                            |
| character_set_filesystem | binary                          |
| character_set_results    | utf8                            |
| character_set_server    | utf8                            |
| character_set_system    | utf8                            |
| character_sets_dir      | /app/soft/mysql/share/charsets/ |
+--------------------------+---------------------------------+
# Author : Leshami
# Blog  :

c、服务器字符集路径设置

对于字符集的路径设置,与错误提示不一致,尝试修改器配置文件
SHELL> tail -3 /etc/my.cnf
[client]
#default-character-set=utf8
character-sets-dir=/app/soft/mysql/share/charsets/

修改配置文件后再次碰到错误提示如下,因此字符集路径不是主要原因
mysql: Character set 'utf8mb4' is not a compiled character set and is not specified in the '/app/soft/mysql/share/charsets/Index.xml' file
SHELL> /app/soft/mysql/share/charsets/Index.xml /app/soft/mysql/share/charsets/Index.xml.bk

d、添加utf8mb4字符集到Index.xml
直接复制utf8的配置,改为utf8mb4后,添加到/app/soft/mysql/share/charsets/Index.xml故障解决。
<charset>
  <family>Unicode</family>
  <description>UTF-8 Unicode</description>
  <alias>utf-8</alias>
  <collation   >
  <flag>primary</flag>
  <flag>compiled</flag>
  </collation>
  <collation           >
    <flag>binary</flag>
    <flag>compiled</flag>
  </collation>
</charset>

3、MySQL官方解释

https://dev.mysql.com/doc/refman/5.5/en/charset-configuration.html

If you try to use a character set that is not compiled into your binary, you might run into the following problems:

You can force client programs to use specific character set as follows:

[client] default-character-set=charset_name

This is normally unnecessary. However, when differs from or , and you input characters manually (as database object identifiers, column values, or both), these may be displayed incorrectly in output from the client or the output itself may be formatted incorrectly. In such cases, starting the mysql client with —that is, setting the client character set to match the system character set—should fix the problem.

从上面的描述来看我这个情形,提示的路径与character-sets-dir路劲不一致。通过修改配置文件后,保持了一致。找到缺省的Index.xml配置文件是由于多版本的问题。二是不清楚为什么要添加utf8mb4这个配置到index.xml文件,根本没用到这个字符集。我估计是要全部检测一遍。虽然添加可用了,但是还是感觉不那么可靠。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/8a2791ce1a2df771c48ed14a54722d13.html