非法混合排序规则

ryhaxcpt  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(494)

问题:mysql的uuid()默认排序规则与配置的连接排序规则不匹配。
我用字符集utf-8和排序规则utf8\u polish\u ci创建了一个数据库+表+字段。
my.cnf如下:

init_connect='SET NAMES utf8 COLLATE utf8_polish_ci'
character-set-server=utf8
collation-server=utf8_polish_ci

字符集:

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       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

排序规则:

mysql> SHOW VARIABLES LIKE 'collation%';
+----------------------+----------------+
| Variable_name        | Value          |
+----------------------+----------------+
| collation_connection | utf8_polish_ci |
| collation_database   | utf8_polish_ci |
| collation_server     | utf8_polish_ci |
+----------------------+----------------+

现在,当使用 uuid() 函数,返回以下错误:

mysql> select replace(uuid(),'-','');
ERROR 1270 (HY000): Illegal mix of collations (utf8_general_ci,COERCIBLE), (utf8_polish_ci,COERCIBLE), (utf8_polish_ci,COERCIBLE) for operation 'replace'

这种情况发生的原因是 uuid() 的默认排序规则似乎是utf8\u general\u ci。

mysql> select charset(uuid()), collation(uuid());
+-----------------+-------------------+
| charset(uuid()) | collation(uuid()) |
+-----------------+-------------------+
| utf8            | utf8_general_ci   |
+-----------------+-------------------+

有没有办法更改uuid()使用的默认排序规则,使其与 collation_connection ?
在我们的环境中,我们编写sql更新,这些更新在具有不同排序规则的不同mysql数据库上执行。因此,通过指定来强制排序规则不是一个选项。

dly7yett

dly7yett1#

(这并不是一个真正的答案,而是试图找出问题的根源和解决方法。)
上车 DATABASE 一个完全不相干的 CHARACTER SET 以及 COLLATION .

mysql> CREATE DATABASE `so40064402` /*!40100 DEFAULT CHARACTER SET ucs2 COLLATE ucs2_bin */
mysql> USE so40064402;
Database changed

为客户建立utf8\U抛光:

mysql> SET NAMES utf8 COLLATE utf8_polish_ci;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES LIKE 'c%a%t%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       | -- from SET NAMES
| character_set_connection | utf8                       | -- from SET NAMES
| character_set_database   | ucs2                       | -- from DATABASE
| character_set_filesystem | binary                     | -- (constant)
| character_set_results    | utf8                       | -- from SET NAMES
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       | -- (constant)
| character_sets_dir       | /usr/share/mysql/charsets/ |
| collation_connection     | utf8_polish_ci             | -- from SET NAMES
| collation_database       | ucs2_bin                   | -- from DATABASE
| collation_server         | utf8mb4_unicode_520_ci     |
+--------------------------+----------------------------+
11 rows in set (0.00 sec)

mysql> select charset(uuid()), collation(uuid());
+-----------------+-------------------+
| charset(uuid()) | collation(uuid()) |
+-----------------+-------------------+
| utf8            | utf8_general_ci   |  -- part of the problem, but can't fix this
+-----------------+-------------------+
1 row in set (0.00 sec)

mysql> select replace(uuid(),'-','');
ERROR 1270 (HY000): Illegal mix of collations
                 (utf8_general_ci,COERCIBLE),
                 (utf8_polish_ci,COERCIBLE),
                 (utf8_polish_ci,COERCIBLE) for operation 'replace'
mysql> 
mysql> 
mysql> 
mysql> SET NAMES utf8mb4 COLLATE utf8mb4_polish_ci;
Query OK, 0 rows affected (0.00 sec)

现在让我们换衣服 SET NAMES 只是。现在成功了!??尽管 UUID() 存在 utf8 !?

mysql> SHOW VARIABLES LIKE 'c%a%t%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    | -- from SET NAMES
| character_set_connection | utf8mb4                    | -- from SET NAMES
| character_set_database   | ucs2                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    | -- from SET NAMES
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
| collation_connection     | utf8mb4_polish_ci          | -- from SET NAMES
| collation_database       | ucs2_bin                   |
| collation_server         | utf8mb4_unicode_520_ci     |
+--------------------------+----------------------------+
11 rows in set (0.00 sec)

mysql> select replace(uuid(),'-','');
+----------------------------------+
| replace(uuid(),'-','')           |
+----------------------------------+
| ea841aacf83b11e8a66580fa5b3669ce |
+----------------------------------+
1 row in set (0.00 sec)

mysql>

相关问题