1. 程式人生 > >MySQL中文亂碼處理_字符集轉換處理

MySQL中文亂碼處理_字符集轉換處理

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;

MySQL中文亂碼處理_字符集轉換處理