IT运维笔记


MySQL数据库编码设置

问题原因

java程序返回java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '=' 百度了下意思是返回的结果有两种字符集。一般这种情况在排除编程语言中单独设置的字符集错误后,首先想到的就是数据库中的数据表设置的字符集类型和编程语言中所要得到的结果类型不一致导致的。下面介绍具体的解决办法: 1271 - Illegal mix of collations for operation 'UNION', 此类问题是由于UNION Mysql的Table的时候对应的字段Collation字符序不同导致的

解决办法

表结构的类型

SHOW VARIABLES LIKE 'character_set_%'; 修改字符集为utf-8 set character_set_database =utf8; set character_set_results =utf8; set character_set_server =utf8; set character_set_system =utf8;

字段类型

SHOW VARIABLES LIKE 'collation_%'; 修改字段类型为utf8_general_ci SET collation_server = utf8_general_ci SET collation_database = utf8_general_ci

将所有表中所有列的排序方式及字符改为UTF8以及uft8_general_ci

SELECT CONCAT('ALTER TABLE `', table_name, '` MODIFY `', column_name, '` ', DATA_TYPE, '(', CHARACTER_MAXIMUM_LENGTH, ') CHARACTER SET UTF8 COLLATE utf8_general_ci', (CASE WHEN IS_NULLABLE = 'NO' THEN ' NOT NULL' ELSE '' END), ';') FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'test' and COLLATION_NAME <> 'utf8_general_ci'; 执行上述语句,将得到的sql语句运行即可