mysql字符集&排序规则

mysql字符集&排序规则

查看当前MySQL实例中不符合字符集&排序规则规范的库名:

SELECT
SCHEMA_NAME '数据库',
DEFAULT_CHARACTER_SET_NAME '库字符集',
DEFAULT_COLLATION_NAME '库排序规则'
FROM
information_schema.SCHEMATA 
WHERE
(DEFAULT_CHARACTER_SET_NAME != 'utf8mb4' OR DEFAULT_COLLATION_NAME != 'utf8mb4_unicode_ci')
AND SCHEMA_NAME NOT IN ( 'sys', 'mysql', 'performance_schema', 'information_schema' );

查看当前MySQL实例中不符合排序规范的表:

SELECT TABLE_SCHEMA '数据库',TABLE_NAME '表',TABLE_COLLATION '表排序规则',TABLE_ROWS '表行数' FROM information_schema.TABLES WHERE TABLE_COLLATION != 'utf8mb4_unicode_ci' and TABLE_SCHEMA not in ('sys','mysql','performance_schema','information_schema');

注意:修改 表 以及 表字段 的字符集、排序集之前,需要先修改 库 的 字符集、排序集

如果是大表,那需要用到 gh-ost 来执行

修改 数据库 的 字符集、排序规则:

SELECT
SCHEMA_NAME '数据库',
DEFAULT_CHARACTER_SET_NAME '库字符规则',
DEFAULT_COLLATION_NAME '库排序规则',
CONCAT( 'ALTER DATABASE ', SCHEMA_NAME, ' DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;' ) '修正库规则SQL' 
FROM
information_schema.SCHEMATA 
WHERE
(DEFAULT_CHARACTER_SET_NAME != 'utf8mb4' OR DEFAULT_COLLATION_NAME != 'utf8mb4_unicode_ci')
AND SCHEMA_NAME NOT IN ( 'sys', 'mysql', 'performance_schema', 'information_schema' );

修改 表 以及 表字段 的字符集、排序规则:

SELECT TABLE_SCHEMA '数据库',TABLE_NAME '表',TABLE_COLLATION '排序规则',CONCAT('ALTER TABLE ',TABLE_SCHEMA,'.', TABLE_NAME, ' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;') '修正表规则SQL' FROM information_schema.TABLES WHERE TABLE_COLLATION != 'utf8mb4_unicode_ci' and TABLE_SCHEMA not in ('sys','mysql','performance_schema','information_schema');

posted @ 2020-01-08 07:53  Carry00  阅读(83)  评论(0编辑  收藏  举报