mysql 字符集排查

Posted 米豆小书童

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 字符集排查相关的知识,希望对你有一定的参考价值。

mysql 字符集排查

库级别

SELECT
	* 
FROM
	information_schema.schemata 
WHERE
	schema_name NOT IN ( \'information_schema\', \'performance_schema\', \'mysql\', \'sys\' ) 
	AND ( default_character_set_name <> \'utf8\' OR default_collation_name <> \'utf8_general_ci\' );

表级别

SELECT 
    TABLE_SCHEMA,
    TABLE_NAME,
    TABLE_TYPE,
    TABLE_ROWS,
    TABLE_COLLATION,
    CREATE_OPTIONS
FROM
    information_schema.TABLES
WHERE
    TABLE_TYPE = \'BASE TABLE\'
        AND TABLE_SCHEMA NOT IN (\'information_schema\' , \'performance_schema\', \'mysql\', \'sys\')
        AND TABLE_COLLATION <> \'utf8_general_ci\';

字段级别

SELECT 
    TABLE_SCHEMA AS \'库名\',
    DEFAULT_CHARACTER_SET_NAME AS \'库的字符集\',
    DEFAULT_COLLATION_NAME AS \'库的校对字符集\',
    TABLE_NAME AS \'表名\',
    TABLE_COLLATION AS \'表的校对规则\',
    COLUMN_NAME AS \'字段名\',
    CHARACTER_SET_NAME AS \'字段的字符集\',
    COLLATION_NAME AS \'字段的校对规则\'
FROM
    (SELECT 
        C2.TABLE_SCHEMA,
            C2.TABLE_NAME,
            C2.CHARACTER_SET_NAME,
            T2.TABLE_COLLATION,
            C2.COLUMN_NAME,
            C2.COLLATION_NAME,
            S2.DEFAULT_CHARACTER_SET_NAME,
            S2.DEFAULT_COLLATION_NAME,
            CASE
                WHEN
                    S2.DEFAULT_CHARACTER_SET_NAME = \'utf8\'
                        AND S2.DEFAULT_COLLATION_NAME = \'utf8_general_ci\'
                        AND (C2.CHARACTER_SET_NAME = \'utf8\'
                        OR C2.CHARACTER_SET_NAME IS NULL)
                        AND (C2.COLLATION_NAME = \'utf8_general_ci\'
                        OR C2.COLLATION_NAME IS NULL)
                        AND T2.TABLE_COLLATION = \'utf8_general_ci\'
                THEN
                    \'true\'
                ELSE \'false\'
            END STATUS
    FROM
        information_schema.COLUMNS AS C2
    JOIN (SELECT 
        TABLE_SCHEMA,
            TABLE_NAME,
            TABLE_TYPE,
            TABLE_ROWS,
            TABLE_COLLATION,
            CREATE_OPTIONS
    FROM
        information_schema.TABLES
    WHERE
        TABLE_TYPE = \'BASE TABLE\'
            AND TABLE_SCHEMA NOT IN (\'information_schema\' , \'sys\', \'performance_schema\', \'mysql\')) T2 ON C2.TABLE_NAME = T2.TABLE_NAME
        AND C2.TABLE_SCHEMA = T2.TABLE_SCHEMA
    JOIN (SELECT 
        SCHEMA_NAME,
            DEFAULT_CHARACTER_SET_NAME,
            DEFAULT_COLLATION_NAME
    FROM
        information_schema.SCHEMATA) S2 ON T2.TABLE_SCHEMA = S2.SCHEMA_NAME) charset_status
WHERE
    STATUS = \'false\'
ORDER BY TABLE_SCHEMA , TABLE_NAME;
  • 表默认排序规则。输出未显式列出表默认字符集,但排序规则名称以字符集名称开头。

以上是关于mysql 字符集排查的主要内容,如果未能解决你的问题,请参考以下文章

一个C++工程CPU占用100%问题的排查

一个C++工程CPU占用100%问题的排查

tomcat环境中文字符集乱码

jsch连接sftp后连接未释放掉问题排查

部分代码片段

mysql error code '1064' 排查过程