mysql:Collations Must Be for the Right Character Set

Each character set has one or more collations, but each collation is associated with one and only one character set. Therefore, the following statement causes an error message because the latin2_bin collation is not legal with the latin1 character set:

每个character set都有一个或多个collations,当每个collation仅仅与一个character set关联。因此下面的表达式会导致collation latin2_bin和charater set latin1不匹配的错误。

mysql> SELECT _latin1 'x' COLLATE latin2_bin;
ERROR 1253 (42000): COLLATION 'latin2_bin' is not valid
for CHARACTER SET 'latin1'

In some cases, expressions that worked before MySQL 4.1 fail in early versions of MySQL 4.1 if you do not take character set and collation into account. For example, before 4.1, this statement works as is:

在某些情况下,在MySQL 4.1之前的版本工作的很正常的表达式,如果你不考虑到chracter set和collation,在MySQL 4.1中会不再能成功运行。例如,4.1之前的版本,下面的式子是工作的:

mysql> SELECT SUBSTRING_INDEX(USER(),'@',1);
+-------------------------------+
| SUBSTRING_INDEX(USER(),'@',1) |
+-------------------------------+
| root                          |
+-------------------------------+

The statement also works as is in MySQL 4.1 as of 4.1.8: In MySQL 4.1, user names are stored using the utf8character set (see Section 9.1.10, “UTF-8 for Metadata”). The literal string '@' has the server character set (latin1 by default). Although the character sets are different, MySQL can coerce the latin1 string to the character set (and collation) of USER() without data loss. It does so, performs the substring operation, and returns a result that has a character set of utf8.

这个表达式同样在 MySQL 4.1以及 4.1.8能工作,user name被通过utf8 的character set存储。字符‘@’拥有服务器字符集(默认是latin1).即使字符集不同,MySQ能强制将latin1字符转到和USER()相同的character set和collation,而不会发生数据丢失。事实上,就是先进行字符串处理,然后才返回给你一个utf8的结果。

However, in versions of MySQL 4.1 before 4.1.8, the statement fails:

然而,在MySQL 4.1.8之前的MySQL 4.1版本,这个表达式就会失败。

mysql> SELECT SUBSTRING_INDEX(USER(),'@',1);
ERROR 1267 (HY000): Illegal mix of collations
(utf8_general_ci,IMPLICIT) and (latin1_swedish_ci,COERCIBLE)
for operation 'substr_index'

This happens because the automatic character set conversion of '@' does not occur and the string operands have different character sets (and thus different collations):

之所以会发生,是因为自动的'@'的character set的转换没有发生,于是字符的操作有不同的character set和collations。

mysql> SELECT COLLATION(USER()), COLLATION('@');
+-------------------+-------------------+
| COLLATION(USER()) | COLLATION('@')    |
+-------------------+-------------------+
| utf8_general_ci   | latin1_swedish_ci |
+-------------------+-------------------+

One way to deal with this is to upgrade to MySQL 4.1.8 or later. If that is not possible, you can tell MySQL to interpret the literal string as utf8:

一种方式是你将MySQL更新到4.1.8和之后的版本。如果这不太可行,可以显示的告诉MySQL将字符转换成utf8的格式:

mysql> SELECT SUBSTRING_INDEX(USER(),_utf8'@',1);
+------------------------------------+
| SUBSTRING_INDEX(USER(),_utf8'@',1) |
+------------------------------------+
| root                               |
+------------------------------------+

Another way is to change the connection character set and collation to utf8. You can do that with SET NAMES 'utf8' or by setting the character_set_connection and collation_connection system variables directly.

另一种方式是改变connection的character set和collation成utf8。你可以通过 SET NAMES 'utf8' 或者通过直接设置 character set connection和collation connection系统变量。

在进行SQL注入时,你遇到了报错信息:"Illegal mix of collations for operation 'UNION'"。根据引用中的解决办法,这个问题是因为information_schema数据库中的表使用的字符集(collation)与目标网站user表中的字符集不一致导致的。解决方法是将user表中的字符集与information_schema数据库一致。需要注意的是,不仅仅需要更改表的字符集,还需要逐一更改每一个字段的字符集。 在实际实战中,如果无法更改目标网站的数据库,可以使用16进制的方式读取数据。根据引用中的实际测试,可以使用类似的payload和url来进行查询,并使用在线加解码工具解码返回结果。 如果你在修改字符集后仍然遇到相同的报错信息,可以参考引用中的方法。该方法使用SQL查询语句来生成修改表字段字符集的语句,可以批量修改表的字段字符集。你需要根据你自己的实际情况修改查询语句中的数据库名和字符集信息。 综上所述,你可以根据引用中提供的方法来解决报错信息"error:Illegal mix of collations for operation 'UNION'"。123 #### 引用[.reference_title] - *1* [SQL注入报错:Illegal mix of collations for operation ‘UNION‘ (sqli-labs靶场MySQL数据库)](https://blog.csdn.net/m0_47470899/article/details/118695774)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] - *2* [关于SQL注入报错:Illegal mix of collations for operation ‘UNION‘原因剖析与验证](https://blog.csdn.net/qq_43665434/article/details/114088565)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] - *3* [MySQLSQL error1271Illegal mix of collations for operation UNION](https://blog.csdn.net/yyj108317/article/details/106427128)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值