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;
  • 表默认排序规则。输出未显式列出表默认字符集,但排序规则名称以字符集名称开头。

posted @ 2018-11-13 18:10  貔貅小米豆  阅读(210)  评论(0编辑  收藏  举报