MySQL中文乱码处理_字符集转换处理

-- 中文乱码修复

-- 查看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;

转载于:https://blog.51cto.com/paopao5541/2083930

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值