ALTER DATABASE <DATABASE_NAME> CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
查询utf8mb3 字符的表,并生成修改sql语句
SELECT CONCAT('ALTER TABLE ', TABLE_SCHEMA, '.', TABLE_NAME, ' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;') AS sql_statementsFROM information_schema.TABLESWHERE TABLE_SCHEMA ='<database_name>'AND TABLE_TYPE ='BASE TABLE';SELECT CONCAT('ALTER TABLE ', TABLE_SCHEMA, '.', TABLE_NAME, ' MODIFY ', COLUMN_NAME, ' ', DATA_TYPE, IF(ISNULL(CHARACTER_MAXIMUM_LENGTH), '', CONCAT('(', CHARACTER_MAXIMUM_LENGTH, ')')), ' CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci', IF(IS_NULLABLE ='NO', ' NOT NULL', ''), ';') AS sql_statementsFROM information_schema.COLUMNSWHERE TABLE_SCHEMA ='<database_name>'AND CHARACTER_SET_NAME ='utf8mb3';
查询结果类似如下:
ALTER TABLE <database_name>.active CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;ALTER TABLE <database_name>.article CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;...