服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - MySQL8 批量修改字符集脚本

MySQL8 批量修改字符集脚本

2023-03-27 14:03wzy0623 Mysql

本文主要介绍了MySQL8 批量修改字符集脚本,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

从低版本迁移到MySQL 8后,可能由于字符集问题出现 Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_0900_ai_ci,IMPLICIT) 错误,此时要修改对象的字符集。

1. 批量修改库字符集

change_database_characset.sql

?
1
2
3
4
select concat('alter database ',schema_name,' default character set utf8mb4 collate utf8mb4_0900_ai_ci;')
  from information_schema.schemata
 where schema_name not in ('sys','mysql','performance_schema','information_schema')
   and lower(default_collation_name) in ('utf8mb4_general_ci','utf8_general_ci');

调用:

?
1
2
/home/mysql/mysql-8.0.16-linux-glibc2.12-x86_64/bin/mysql -uroot -h10.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 -h10.0.0.18 -P3306 -p70n6w+1XklMu -f < change_database_characset_result.sql > change_database_characset_result.out 2>&1

2. 批量修改表字符集

change_table_characset.sql

?
1
2
3
select concat('alter table ',table_schema,'.',table_name,' default character set utf8mb4 collate = utf8mb4_0900_ai_ci;')
  from information_schema.tables where table_schema not in ('sys','mysql','performance_schema','information_schema')
   and table_type='BASE TABLE' and lower(table_collation) in ('utf8mb4_general_ci','utf8_general_ci');

调用:

?
1
2
/home/mysql/mysql-8.0.16-linux-glibc2.12-x86_64/bin/mysql -uroot -h10.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 -h10.0.0.18 -P3306 -p70n6w+1XklMu -f < change_table_characset_result.sql > change_table_characset_result.out 2>&1

3. 批量修改列字符集

change_column_characset.sql

?
1
2
3
4
5
6
7
8
9
10
11
set group_concat_max_len=10240;
 
select concat(c1,c2,';')
  from (select c1, group_concat(c2) c2
          from (select concat('alter table ',t1.table_schema,'.',t1.table_name) c1,concat(' modify ','`',t1.column_name,'` ',t1.data_type,
                              if (t1.data_type in ('varchar','char'),concat('(',t1.character_maximum_length,')'),''),
                              ' character set utf8mb4 collate utf8mb4_0900_ai_ci',if(t1.is_nullable='NO',' not null',' null'),' comment ','''',t1.column_comment,'''') c2
                  from information_schema.columns t1, information_schema.tables t2
                 where t1.table_schema=t2.table_schema and t1.table_name=t2.table_name and t2.table_type='BASE TABLE'
                   and lower(t1.collation_name) in ('utf8mb4_general_ci','utf8_general_ci') and t1.table_schema not in ('sys','mysql','performance_schema','information_schema')) t1
         group by c1) t;

调用:

?
1
2
/home/mysql/mysql-8.0.16-linux-glibc2.12-x86_64/bin/mysql -uroot -h10.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 -h10.0.0.18 -P3306 -p70n6w+1XklMu -f < change_column_characset_result.sql > change_column_characset_result.out 2>&1

到此这篇关于MySQL8 批量修改字符集脚本的文章就介绍到这了,更多相关MySQL8批量修改字符集内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://wxy0327.blog.csdn.net/article/details/128670848

延伸 · 阅读

精彩推荐
  • MysqlMySQL函数Locate的使用详解

    MySQL函数Locate的使用详解

    本文主要介绍了MySQL函数Locate的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小...

    今夜无风亦无雨5962022-08-15
  • MysqlMySql数据库基础之子查询详解

    MySql数据库基础之子查询详解

    所谓子查询是指在一个查询中嵌套了其他的若干查询,即在一个SELECT查询语句的WHERE或FROM子句中包含另一个SELECT查询语句,下面这篇文章主要给大家介绍了关...

    在人间负债^9572022-11-14
  • Mysql使用Visual Studio Code连接MySql数据库并进行查询

    使用Visual Studio Code连接MySql数据库并进行查询

    这篇文章主要介绍了使用Visual Studio Code连接MySql数据库并进行查询,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,...

    sigmarising7792021-04-15
  • Mysql基于MYSQL中优化的一些方法

    基于MYSQL中优化的一些方法

    本篇文章是对MYSQL中优化的一些方法进行了详细的介绍,需要的朋友参考下 ...

    MYSQL教程网3442019-12-25
  • Mysql在MySQL中修改密码及访问限制的设置方法详解

    在MySQL中修改密码及访问限制的设置方法详解

    MySQL是一个真正的多用户、多线程SQL数据库服务器。MySQL是以一个客户机/服务器结构的实现,它由一个服务器守护程序mysqld和很多不同的客户程序和库组成...

    mysql教程网2862019-10-17
  • Mysqlmysql not in、left join、IS NULL、NOT EXISTS 效率问题记录

    mysql not in、left join、IS NULL、NOT EXISTS 效率问题记录

    mysql not in、left join、IS NULL、NOT EXISTS 效率问题记录,需要的朋友可以参考下。 ...

    MYSQL教程网2702019-11-27
  • MysqlMySQL中执行计划explain命令示例详解

    MySQL中执行计划explain命令示例详解

    这篇文章主要给大家介绍了关于MySQL中执行计划explain命令的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用explain命令具有一定的参考学...

    Fantasy life2842020-09-01
  • Mysqlmysql5.7.21安装配置教程

    mysql5.7.21安装配置教程

    这篇文章主要为大家详细介绍了mysql5.7.21安装配置教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    sam112632020-08-25