MySQL8批量修改字符集脚本怎么写
这篇“MySQL8批量修改字符集脚本怎么写”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“MySQL8批量修改字符集脚本怎么写”文章吧。
从低版本迁移到MySQL 8后,可能由于字符集问题出现 Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_0900_ai_ci,IMPLICIT) 错误,此时要修改对象的字符集。
1. 批量修改库字符集change_database_characset.sql
selectconcat('alterdatabase',schema_name,'defaultcharactersetutf8mb4collateutf8mb4_0900_ai_ci;')frominformation_schema.schematawhereschema_namenotin('sys','mysql','performance_schema','information_schema')andlower(default_collation_name)in('utf8mb4_general_ci','utf8_general_ci');
调用:
/home/mysql/mysql-8.0.16-linux-glibc2.12-x86_64/bin/mysql-uroot-h20.0.0.18-P3306-p70n6w+1XklMu-N<change_database_characset.sql>change_database_characset_result.sql/home/mysql/mysql-8.0.16-linux-glibc2.12-x86_64/bin/mysql-uroot-h20.0.0.18-P3306-p70n6w+1XklMu-f<change_database_characset_result.sql>change_database_characset_result.out2>&12. 批量修改表字符集
change_table_characset.sql
selectconcat('altertable',table_schema,'.',table_name,'defaultcharactersetutf8mb4collate=utf8mb4_0900_ai_ci;')frominformation_schema.tableswheretable_schemanotin('sys','mysql','performance_schema','information_schema')andtable_type='BASETABLE'andlower(table_collation)in('utf8mb4_general_ci','utf8_general_ci');
调用:
/home/mysql/mysql-8.0.16-linux-glibc2.12-x86_64/bin/mysql-uroot-h20.0.0.18-P3306-p70n6w+1XklMu-N<change_table_characset.sql>change_table_characset_result.sql/home/mysql/mysql-8.0.16-linux-glibc2.12-x86_64/bin/mysql-uroot-h20.0.0.18-P3306-p70n6w+1XklMu-f<change_table_characset_result.sql>change_table_characset_result.out2>&13. 批量修改列字符集
change_column_characset.sql
setgroup_concat_max_len=10240;selectconcat(c1,c2,';')from(selectc1,group_concat(c2)c2from(selectconcat('altertable',t1.table_schema,'.',t1.table_name)c1,concat('modify','`',t1.column_name,'`',t1.data_type,if(t1.data_typein('varchar','char'),concat('(',t1.character_maximum_length,')'),''),'charactersetutf8mb4collateutf8mb4_0900_ai_ci',if(t1.is_nullable='NO','notnull','null'),'comment','''',t1.column_comment,'''')c2frominformation_schema.columnst1,information_schema.tablest2wheret1.table_schema=t2.table_schemaandt1.table_name=t2.table_nameandt2.table_type='BASETABLE'andlower(t1.collation_name)in('utf8mb4_general_ci','utf8_general_ci')andt1.table_schemanotin('sys','mysql','performance_schema','information_schema'))t1groupbyc1)t;
调用:
/home/mysql/mysql-8.0.16-linux-glibc2.12-x86_64/bin/mysql-uroot-h20.0.0.18-P3306-p70n6w+1XklMu-N<change_column_characset.sql>change_column_characset_result.sql/home/mysql/mysql-8.0.16-linux-glibc2.12-x86_64/bin/mysql-uroot-h20.0.0.18-P3306-p70n6w+1XklMu-f<change_column_characset_result.sql>change_column_characset_result.out2>&1
以上就是关于“MySQL8批量修改字符集脚本怎么写”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。