资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

-- 中文乱码修复

创新互联建站主要从事成都网站设计、做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务马龙,10年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

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


本文名称:MySQL中文乱码处理_字符集转换处理
本文链接:http://cdkjz.cn/article/ipjdoh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220