批量更改mysql字符集
Posted Demonson
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了批量更改mysql字符集相关的知识,希望对你有一定的参考价值。
1.修改mysql服务器字符集
SHOW VARIABLES LIKE '%char%';
character_set_server=utf8mb4
重启服务器参数生效
2.更改所有历史业务表字符集
脚本如下:changeChar.sh
#!/bin/sh
#数据库连接信息
USERBANE='root'
PASSWD='root123'
HOST='127.0.0.1'
PORT='3306'
#查询业务库
mysql -u$USERBANE -p$PASSWD -h$HOST -P$PORT -e "select schema_name from information_schema.schemata where schema_name not in ('mysql','sys','information_schema','performance_schema')" -B > DBName.txt
#批量生成修改字符集脚本并执行
cat DBName.txt | while read line
do
if [ $line = 'SCHEMA_NAME' ];then
continue
fi
echo "批量生成连接数据库脚本: "$line
String='SELECT CONCAT("ALTER TABLE ", TABLE_NAME," CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;") AS SQL1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE="BASE TABLE" AND TABLE_SCHEMA="'$line'"'
#echo $String
mysql -u$USERBANE -p$PASSWD -h$HOST -P$PORT -D $line -e "$String" -B > DBName.sql
sed -i '1d' DBName.sql
cat DBName.sql
mysql -u$USERBANE -p$PASSWD -h$HOST -P$PORT -D $line < DBName.sql
done
以上是关于批量更改mysql字符集的主要内容,如果未能解决你的问题,请参考以下文章