MySQL8 批量修改字符集腳本
從低版本遷移到MySQL 8后,可能由于字符集問題出現(xiàn) Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_0900_ai_ci,IMPLICIT) 錯(cuò)誤,此時(shí)要修改對(duì)象的字符集。
1. 批量修改庫(kù)字符集
change_database_characset.sql
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');
調(diào)用:
/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
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');
調(diào)用:
/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
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;
調(diào)用:
/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
到此這篇關(guān)于MySQL8 批量修改字符集腳本的文章就介紹到這了,更多相關(guān)MySQL8批量修改字符集內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Prometheus 插件mysql_exporter安裝過程
mysql_exporter是用來收集MysQL或者M(jìn)ariadb數(shù)據(jù)庫(kù)相關(guān)指標(biāo)的,mysql_exporter需要連接到數(shù)據(jù)庫(kù)并有相關(guān)權(quán)限,這篇文章主要介紹了Prometheus插件安裝(mysql_exporter),需要的朋友可以參考下2023-06-06
mysql中datetime類型設(shè)置默認(rèn)值方法
下面小編就為大家分享一篇mysql中datetime類型設(shè)置默認(rèn)值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02
分析一條sql的性能的標(biāo)準(zhǔn)總結(jié)
在本篇文章里小編給各位分享了關(guān)于分析一條sql的性能的相關(guān)知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2019-07-07

