MySQL中文乱码处理_字符集转换处理
admin
2023-04-15 09:02:12
0

-- 中文乱码修复

-- 查看MySQL服务参数设置
mysql> show variables like '%character%';
+--------------------------+----------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.03 sec)

-- 查看建库的默认字符集
show create database test;

-- 查看建表的默认字符集
show create table yjdb;

-- 修复为utf8字符集
ALTER DATABASE db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE tb_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

-- root用户执行查询,把结果执行,把不统一的库和表及字段的字符集统一为utf8
-- 修改全库中建库默认字符集
select 'ALTER DATABASE '||db||' DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;' from mysql.db where db not in ('information_schema','mysql','test','performance_schema');
select concat('ALTER DATABASE ',db,' DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;') from mysql.db where db not in ('information_schema','mysql','test','performance_schema');

-- 修改全库中建表默认字符集
select 'ALTER TABLE '||table_schema||'.'||table_name||' DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;' as alter_sql from information_schema.TABLES where table_schema not in ('information_schema','mysql','test','performance_schema') and table_collation != 'utf8_general_ci';
select concat('ALTER TABLE ',table_schema,'.',table_name,' DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;') as alter_sql from information_schema.TABLES where table_schema not in ('information_schema','mysql','test','performance_schema') and table_collation != 'utf8_general_ci';

-- 修改全库中表的列属性为latin1的字符集为默认,请确认后执行。
-- select * from information_schema.COLUMNS where table_schema='tss';
select 'alter table '||TABLE_SCHEMA||'.'||table_name||' change '||column_name||' '||column_name||' '||column_type||' default '||''''||column_default||''''||' comment '||''''||column_comment||''''||';' as alter_sql from information_schema.COLUMNS where table_schema not in ('information_schema','mysql','test','performance_schema') and CHARACTER_SET_NAME='latin1' and is_nullable='yes' and column_default is not null
union all
select 'alter table '||TABLE_SCHEMA||'.'||table_name||' change '||column_name||' '||column_name||' '||column_type||' comment '||''''||column_comment||''''||';' as alter_sql from information_schema.COLUMNS where table_schema not in ('information_schema','mysql','test','performance_schema') and CHARACTER_SET_NAME='latin1' and is_nullable='yes' and column_default is null
union all
select 'alter table '||TABLE_SCHEMA||'.'||table_name||' change '||column_name||' '||column_name||' '||column_type||' not null default '||''''||column_default||''''||' comment '||''''||column_comment||''''||';' as alter_sql from information_schema.COLUMNS where table_schema not in ('information_schema','mysql','test','performance_schema') and CHARACTER_SET_NAME='latin1' and is_nullable='no' and column_default is not null
union all
select 'alter table '||TABLE_SCHEMA||'.'||table_name||' change '||column_name||' '||column_name||' '||column_type||' not null '||' comment '||''''||column_comment||''''||';' as alter_sql from information_schema.COLUMNS where table_schema not in ('information_schema','mysql','test','performance_schema') and CHARACTER_SET_NAME='latin1' and is_nullable='no' and column_default is null;

-- 为了避免不同环境下出现误差造成影响,可以在建库和表的时候特殊指定字符集

-- 修改库的编码
select concat('ALTER DATABASE ',db,' DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;') from mysql.db_view where db ='xjk_bbs';

-- 修改全库中建表默认字符集
select concat('ALTER TABLE ',table_schema,'.',table_name,' DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;') as alter_sql from information_schema.TABLES where table_schema='xjk_bbs';

-- 修改全库中表的列属性为latin1的字符集为默认,请确认后执行。
-- select * from information_schema.COLUMNS where table_schema='tss';

select concat('alter table ',TABLE_SCHEMA,'.',table_name,' MODIFY COLUMN ',' ',column_name,' ',column_type,' CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ',' default ','''',column_default,'''',' comment ','''',column_comment,'''',';') as alter_sql from information_schema.COLUMNS where table_schema='xjk_bbs' and table_name='aws_question' and column_type not like '%int%' and is_nullable='yes' and column_default is not null
union all
select concat('alter table ',TABLE_SCHEMA,'.',table_name,' MODIFY COLUMN ',' ',column_name,' ',column_type,' CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ',' comment ','''',column_comment,'''',';') as alter_sql from information_schema.COLUMNS where table_schema='xjk_bbs' and table_name='aws_question' and column_type not like '%int%' and is_nullable='yes' and column_default is null
union all
select concat('alter table ',TABLE_SCHEMA,'.',table_name,' MODIFY COLUMN ',' ',column_name,' ',column_type,' CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ',' not null default ','''',column_default,'''',' comment ','''',column_comment,'''',';') as alter_sql from information_schema.COLUMNS where table_schema='xjk_bbs' and table_name='aws_question' and column_type not like '%int%' and is_nullable='no' and column_default is not null
union all
select concat('alter table ',TABLE_SCHEMA,'.',table_name,' MODIFY COLUMN ',' ',column_name,' ',column_type,' CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ',' not null ',' comment ','''',column_comment,'''',';') as alter_sql from information_schema.COLUMNS where table_schema='xjk_bbs' and table_name='aws_question' and column_type not like '%int%' and is_nullable='no' and column_default is null;

相关内容

热门资讯

终于懂了“同城游比鸡是不是有挂... 网上科普关于“同城游比鸡有没有挂”话题很是火热,小编也是针对同城游比鸡作*弊开挂的方法以及开挂对应的...
【第一消息】“微乐陕西三代一到... 【第一消息】“微乐陕西三代一到底是不是挂?”(必胜开挂神器)您好,微乐陕西三代一这个游戏其实有挂的,...
今日重磅消息“瓜瓜丰城棋牌辅助... 网上科普关于“瓜瓜丰城棋牌有没有挂”话题很是火热,小编也是针对瓜瓜丰城棋牌作*弊开挂的方法以及开挂对...
【第一财经】“免安装麻将机控器... 有 亲,根据资深记者爆料免安装麻将机控器好牌是可以开挂的,确实有挂(咨询...
重磅消息“微乐云南麻将到底是不... 重磅消息“微乐云南麻将到底是不是挂?”(确实真的有挂)您好,微乐云南麻将这个游戏其实有挂的,确实是有...
玩家最新攻略“福建十三水怎么开... 网上科普关于“福建十三水有没有挂”话题很是火热,小编也是针对福建十三水作*弊开挂的方法以及开挂对应的...
终于明白“微乐陕西三代开挂器?... 网上科普关于“微乐陕西三代有没有挂”话题很是火热,小编也是针对微乐陕西三代作*弊开挂的方法以及开挂对...
最新引进“全民如意棋牌怎么开挂... 最新引进“全民如意棋牌怎么开挂?”(透视曝光猫腻)您好,全民如意棋牌这个游戏其实有挂的,确实是有挂的...
今日重大消息“德娴麻将究竟有挂... 有 亲,根据资深记者爆料德娴麻将是可以开挂的,确实有挂(咨询软件无需打开...
最新引进“科乐麻将有没有挂?”... 您好:科乐麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这款游戏...