当前位置: 首页 > MySQL数据库

MySQL8批量修改字符集脚本怎么写

时间:2026-01-26 14:20:38

从低版本迁移到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>&1
2. 批量修改表字符集

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>&1
3. 批量修改列字符集

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

上一篇:mysql socket指的是什么
下一篇:mysql之数据库常用脚本有哪些
mysql
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器
  • 英特尔第五代 Xeon CPU 来了:详细信息和行业反应
  • 由于云计算放缓引发扩张担忧,甲骨文股价暴跌
  • Web开发状况报告详细介绍可组合架构的优点
  • 如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳
  • 美光在数据中心需求增长后给出了强有力的预测
  • 2027服务器市场价值将接近1960亿美元
  • 生成式人工智能的下一步是什么?
  • 分享在外部存储上安装Ubuntu的5种方法技巧
  • 全球数据中心发展的关键考虑因素
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器

    英特尔第五代 Xeon CPU 来了:详细信息和行业反应

    由于云计算放缓引发扩张担忧,甲骨文股价暴跌

    Web开发状况报告详细介绍可组合架构的优点

    如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳

    美光在数据中心需求增长后给出了强有力的预测

    2027服务器市场价值将接近1960亿美元

    生成式人工智能的下一步是什么?

    分享在外部存储上安装Ubuntu的5种方法技巧

    全球数据中心发展的关键考虑因素