本文档风哥主要介绍Oracle字符集迁移相关知识,包括字符集迁移的概念、字符集迁移组成、字符集迁移使用、字符集迁移配置、字符集迁移监控、字符集迁移故障处理等内容,由风哥教程参考Oracle官方文档Install and Upgrade内容编写,适合DBA人员在学习和测试中使用,如果要应用于生产环境则需要自行确认。
Part01-基础概念与理论知识
1.1 字符集迁移的概念
Oracle字符集迁移是指将数据库的字符集从一个字符集转换到另一个字符集。字符集迁移通常用于解决字符集不兼容问题、支持多语言环境、统一字符集标准等场景。字符集迁移需要谨慎操作,可能导致数据丢失或乱码。更多视频教程www.fgedu.net.cn
- 字符转换:从一个字符集转换到另一个字符集
- 数据风险:可能导致数据丢失或乱码
- 复杂操作:操作复杂,需要谨慎
- 多种方法:支持多种迁移方法
- 充分测试:需要充分测试
1.2 字符集迁移组成
Oracle字符集迁移组成:
- 源字符集:原始数据库的字符集
- 目标字符集:目标数据库的字符集
- 数据验证:验证数据完整性
- 转换工具:用于转换的工具
- 迁移方法:选择的迁移方法
1. 源字符集
– 原始数据库的字符集
– 如US7ASCII
– 如WE8ISO8859P1
– 如ZHS16GBK
– 如AL32UTF8
2. 目标字符集
– 目标数据库的字符集
– 如AL32UTF8
– 如ZHS16GBK
– 如WE8ISO8859P1
– 如US7ASCII
3. 数据验证
– 验证数据完整性
– 检查字符转换
– 检查数据丢失
– 检查乱码问题
– 验证应用程序
4. 转换工具
– 用于转换的工具
– ALTER DATABASE
– CSSCAN
– CSALTER
– Data Pump
5. 迁移方法
– 选择的迁移方法
– ALTER DATABASE方法
– 导出导入方法
– DMU工具
– CSALTER方法
1.3 字符集迁移使用
Oracle字符集迁移使用:
- 字符集升级:从旧字符集升级到新字符集
- 多语言支持:支持多语言环境
- 字符集统一:统一数据库字符集
- 数据迁移:跨字符集数据迁移
- 应用兼容:解决应用兼容性问题
Part02-生产环境规划与建议
2.1 字符集迁移规划
Oracle字符集迁移规划要点:
– 迁移评估:评估迁移的必要性和可行性
– 迁移准备:准备迁移所需资源
– 迁移测试:在测试环境中测试
– 迁移执行:执行迁移过程
# 迁移评估规划
– 字符集评估:评估源字符集和目标字符集
– 数据量评估:评估数据量大小
– 风险评估:评估迁移风险
– 兼容性评估:评估应用兼容性
# 迁移准备规划
– 备份数据:备份原始数据
– 空间准备:准备存储空间
– 时间准备:准备迁移时间
– 应用准备:准备应用程序
# 迁移测试规划
– 测试环境:在测试环境中测试
– 测试数据:使用测试数据
– 测试用例:制定测试用例
– 测试验证:验证迁移结果
# 迁移执行规划
– 迁移时间:选择合适的迁移时间
– 迁移方式:选择合适的迁移方式
– 迁移监控:监控迁移过程
– 迁移验证:验证迁移结果
2.2 字符集迁移场景
Oracle字符集迁移场景:
- 字符集升级:从旧字符集升级到新字符集
- 多语言支持:支持多语言环境
- 字符集统一:统一数据库字符集
- 数据迁移:跨字符集数据迁移
- 应用兼容:解决应用兼容性问题
1. 字符集升级
– 从旧字符集升级到新字符集
– 如US7ASCII到AL32UTF8
– 如ZHS16GBK到AL32UTF8
– 操作复杂
– 风险较高
2. 多语言支持
– 支持多语言环境
– 需要Unicode字符集
– 如AL32UTF8
– 操作复杂
– 风险适中
3. 字符集统一
– 统一数据库字符集
– 多个数据库统一
– 便于管理
– 操作适中
– 风险适中
4. 数据迁移
– 跨字符集数据迁移
– 需要字符转换
– 可能丢失数据
– 操作复杂
– 风险较高
5. 应用兼容
– 解决应用兼容性问题
– 应用需要特定字符集
– 需要调整
– 操作适中
– 风险适中
2.3 字符集迁移最佳实践
Oracle字符集迁移最佳实践:
- 充分测试:在测试环境中充分测试
- 备份数据:迁移前备份数据
- 文档记录:记录迁移过程
- 验证数据:验证迁移后数据完整性
- 经验总结:总结迁移经验
Part03-生产环境项目实施方案
3.1 字符集迁移配置
3.1.1 检查当前字符集
SQL> select * from nls_database_parameters where parameter like ‘%CHARACTERSET’;
PARAMETER VALUE
—————————— ——————————
NLS_CHARACTERSET ZHS16GBK
NLS_NCHAR_CHARACTERSET AL16UTF16
# 2. 查看会话字符集
SQL> select * from nls_session_parameters where parameter like ‘%CHARACTERSET’;
PARAMETER VALUE
—————————— ——————————
NLS_CHARACTERSET ZHS16GBK
NLS_NCHAR_CHARACTERSET AL16UTF16
# 3. 查看实例字符集
SQL> select * from nls_instance_parameters where parameter like ‘%CHARACTERSET’;
PARAMETER VALUE
—————————— ——————————
NLS_CHARACTERSET ZHS16GBK
NLS_NCHAR_CHARACTERSET AL16UTF16
# 4. 查看支持的字符集
SQL> select value from v$nls_valid_values where parameter = ‘CHARACTERSET’ and value like ‘%UTF%’;
VALUE
——————————————————————————–
UTF8
AL32UTF8
AL16UTF16
AL24UTFFSS
# 5. 检查字符集转换可行性
SQL> select value from v$nls_valid_values where parameter = ‘CHARACTERSET’ and value = ‘AL32UTF8’;
VALUE
——————————————————————————–
AL32UTF8
3.1.2 使用CSSCAN检查数据
SQL> @?/rdbms/admin/csminst.sql
User created.
Grant succeeded.
# 2. 运行CSSCAN检查
$ csscan system/password full=y tochar=AL32UTF8 array=1024000 process=8
Character Set Scanner v2.2 : Release 19.0.0.0.0 – Production on Tue Mar 31 10:00:00 2026
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.3.0.0.0 – Production
Parameter List:
———————————————————-
Source database character set: ZHS16GBK
Target database character set: AL32UTF8
Scan type: full database
Scan CHAR data? YES
Database scan depth: all
Array size: 1024000
Number of processes: 8
Maximum convertible size: 1000000
———————————————————-
Creating Database Scan Summary Report…
Creating Individual Exception Report…
Scanner terminated successfully.
# 3. 查看扫描报告
$ cat scan.txt
Database Scan Summary Report
Time Started : 03/31/2026 10:00:00
Time Completed : 03/31/2026 10:05:00
Source Database Character Set: ZHS16GBK
Target Database Character Set: AL32UTF8
Scan Summary:
———————————————————-
Number of tables scanned: 1234
Number of columns scanned: 5678
Total number of rows: 12345678
Data Dictionary Tables:
———————————————————-
Number of tables: 123
Number of columns: 456
Total number of rows: 123456
Application Data Tables:
———————————————————-
Number of tables: 1111
Number of columns: 5222
Total number of rows: 12222222
Conversion Summary:
———————————————————-
Convertible data: 12345 rows
Changeless data: 12333333 rows
Lossy data: 0 rows
# 4. 查看异常报告
$ cat scan.err
Individual Exception Report
[Table: SCOTT.EMP, Column: ENAME]
Rowid: AAASdqAAEAAAAInAAA
Value: 风哥1号
Status: Convertible
[Table: SCOTT.DEPT, Column: DNAME]
Rowid: AAASdrAAEAAAAIoAAA
Value: 研发部
Status: Convertible
3.1.3 使用ALTER DATABASE迁移字符集
$ rman target /
Recovery Manager: Release 19.0.0.0.0 – Production on Tue Mar 31 10:00:00 2026
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
connected to target database: FGEDUDB (DBID=1234567890)
RMAN> backup database plus archivelog;
Starting backup at 2026-03-31 10:00:00
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=150 device type=DISK
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00001 name=/oracle/app/oracle/oradata/FGEDUDB/system01.dbf
input datafile file number=00002 name=/oracle/app/oracle/oradata/FGEDUDB/sysaux01.dbf
input datafile file number=00003 name=/oracle/app/oracle/oradata/FGEDUDB/undotbs01.dbf
input datafile file number=00004 name=/oracle/app/oracle/oradata/FGEDUDB/users01.dbf
channel ORA_DISK_1: starting piece 1 at 2026-03-31 10:00:00
channel ORA_DISK_1: finished piece 1 at 2026-03-31 10:05:00
piece handle=/backup/FGEDUDB_full_20260331_100000 tag=TAG20260331T100000 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:05:00
Finished backup at 2026-03-31 10:05:00
RMAN> exit
# 2. 关闭数据库
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
# 3. 启动数据库到MOUNT状态
SQL> startup mount;
ORACLE instance started.
Total System Global Area 2147483648 bytes
Fixed Size 8710176 bytes
Variable Size 536870912 bytes
Database Buffers 1593835520 bytes
Redo Buffers 8069120 bytes
Database mounted.
# 4. 启用受限会话
SQL> alter system enable restricted session;
System altered.
# 5. 打开数据库
SQL> alter database open;
Database altered.
# 6. 修改字符集
SQL> alter database character set internal_use AL32UTF8;
Database altered.
# 7. 关闭数据库
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
# 8. 启动数据库
SQL> startup;
ORACLE instance started.
Total System Global Area 2147483648 bytes
Fixed Size 8710176 bytes
Variable Size 536870912 bytes
Database Buffers 1593835520 bytes
Redo Buffers 8069120 bytes
Database mounted.
Database opened.
# 9. 验证字符集
SQL> select * from nls_database_parameters where parameter like ‘%CHARACTERSET’;
PARAMETER VALUE
—————————— ——————————
NLS_CHARACTERSET AL32UTF8
NLS_NCHAR_CHARACTERSET AL16UTF16
# 10. 禁用受限会话
SQL> alter system disable restricted session;
System altered.
3.1.4 使用CSALTER迁移字符集
SQL> @?/rdbms/admin/csalter.plb
0 rows updated.
0 rows updated.
0 rows updated.
0 rows updated.
0 rows updated.
0 rows updated.
0 rows updated.
0 rows updated.
PL/SQL procedure successfully completed.
This script will update the content of the Oracle Data Dictionary.
Please ensure you have a full backup before initiating this procedure.
Would you like to proceed (Y/N)? Y
old 6: if (UPPER(‘&conf’) <> ‘Y’) then
new 6: if (UPPER(‘Y’) <> ‘Y’) then
Checking data validity…
begin
*
ERROR at line 1:
ORA-24329: invalid character set identifier
ORA-06512: at “SYS.CSM$RCONVERT”, line 37
ORA-06512: at “SYS.CSM$RCONVERT”, line 37
ORA-06512: at line 8
# 2. 如果有转换错误,需要先处理
— 查看转换错误
SQL> select * from csm$errors;
# 3. 处理转换错误后重新运行
SQL> @?/rdbms/admin/csalter.plb
# 4. 验证字符集
SQL> select * from nls_database_parameters where parameter like ‘%CHARACTERSET’;
PARAMETER VALUE
—————————— ——————————
NLS_CHARACTERSET AL32UTF8
NLS_NCHAR_CHARACTERSET AL16UTF16
3.2 字符集迁移监控
3.2.1 配置字符集迁移监控脚本
$ vi /home/oracle/scripts/monitor_charset.sh
#!/bin/bash
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# 字符集迁移监控脚本
export ORACLE_HOME=/oracle/app/oracle/product/19c/dbhome_1
export ORACLE_SID=FGEDUDB
export PATH=$ORACLE_HOME/bin:$PATH
LOG_DIR=”/home/oracle/charset_monitor”
mkdir -p $LOG_DIR
DATE=$(date +%Y%m%d)
LOG_FILE=”$LOG_DIR/charset_monitor_$DATE.log”
echo “$(date): Starting charset monitor…” > $LOG_FILE
# 检查字符集
sqlplus -s / as sysdba <
set linesize 200
set pagesize 100
— 检查数据库字符集
select * from nls_database_parameters where parameter like ‘%CHARACTERSET’;
— 检查字符数据
select count(*) from scott.emp where ename is not null;
— 检查数据库状态
select name, open_mode from v\$database;
exit
EOF
echo “$(date): Charset monitor completed.” >> $LOG_FILE
# 发送报告
mail -s “Charset Monitor Report – $DATE” admin@fgedu.net.cn < $LOG_FILE
# 2. 设置脚本权限
$ chmod +x /home/oracle/scripts/monitor_charset.sh
# 3. 测试脚本
$ /home/oracle/scripts/monitor_charset.sh
# 4. 设置定期监控
$ crontab -e
# 每小时监控字符集状态
0 * * * * /home/oracle/scripts/monitor_charset.sh >> /home/oracle/charset_monitor/cron.log 2>&1
3.3 字符集迁移故障处理
3.3.1 字符集迁移常见问题
# 错误信息
ORA-12712: new character set must be a superset of old character set
# 解决方案
# 1. 检查字符集兼容性
SQL> select value from v$nls_valid_values where parameter = ‘CHARACTERSET’;
# 2. 使用INTERNAL_USE选项
SQL> alter database character set internal_use AL32UTF8;
# 问题2:数据转换失败
# 错误信息
ORA-24329: invalid character set identifier
# 解决方案
# 1. 检查转换错误
SQL> select * from csm$errors;
# 2. 处理转换错误
— 删除或修改问题数据
— 重新运行CSALTER
# 问题3:字符乱码
# 错误信息
数据出现乱码
# 解决方案
# 1. 检查字符集
SQL> select * from nls_database_parameters where parameter like ‘%CHARACTERSET’;
# 2. 检查客户端字符集
$ echo $NLS_LANG
# 3. 设置正确的客户端字符集
$ export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
# 4. 重新导入数据
$ impdp system/password directory=datapump_dir dumpfile=data.dmp
Part04-生产案例与实战讲解
4.1 字符集迁移案例
在生产环境中使用字符集迁移的完整案例:
4.1.1 场景描述
某企业需要将Oracle数据库字符集从ZHS16GBK迁移到AL32UTF8,以支持多语言环境。
4.1.2 分析步骤
$ vi /home/oracle/scripts/auto_charset_migration.sh
#!/bin/bash
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# 字符集迁移自动化脚本
export ORACLE_HOME=/oracle/app/oracle/product/19c/dbhome_1
export ORACLE_SID=FGEDUDB
export PATH=$ORACLE_HOME/bin:$PATH
LOG_DIR=”/home/oracle/charset”
mkdir -p $LOG_DIR
DATE=$(date +%Y%m%d_%H%M%S)
LOG_FILE=”$LOG_DIR/charset_$DATE.log”
echo “========================================” > $LOG_FILE
echo “Charset Migration Report” >> $LOG_FILE
echo “Date: $(date)” >> $LOG_FILE
echo “========================================” >> $LOG_FILE
# 检查当前字符集
echo “” >> $LOG_FILE
echo “Checking current charset…” >> $LOG_FILE
sqlplus -s / as sysdba <
select * from nls_database_parameters where parameter like ‘%CHARACTERSET’;
exit
EOF
# 运行CSSCAN检查
echo “” >> $LOG_FILE
echo “Running CSSCAN…” >> $LOG_FILE
csscan system/password full=y tochar=AL32UTF8 array=1024000 process=8 >> $LOG_FILE 2>&1
# 备份数据库
echo “” >> $LOG_FILE
echo “Backing up database…” >> $LOG_FILE
rman target / log=$LOG_FILE append <
# 修改字符集
echo “” >> $LOG_FILE
echo “Changing charset…” >> $LOG_FILE
sqlplus -s / as sysdba <
shutdown immediate;
startup mount;
alter system enable restricted session;
alter database open;
alter database character set internal_use AL32UTF8;
shutdown immediate;
startup;
alter system disable restricted session;
exit
EOF
# 验证字符集
echo “” >> $LOG_FILE
echo “Verifying charset…” >> $LOG_FILE
sqlplus -s / as sysdba <
select * from nls_database_parameters where parameter like ‘%CHARACTERSET’;
exit
EOF
echo “” >> $LOG_FILE
echo “Charset migration completed at $(date)” >> $LOG_FILE
# 发送报告
mail -s “Charset Migration Report – $DATE” admin@fgedu.net.cn < $LOG_FILE
# 2. 设置脚本权限
$ chmod +x /home/oracle/scripts/auto_charset_migration.sh
# 3. 测试脚本
$ /home/oracle/scripts/auto_charset_migration.sh
4.2 字符集迁移故障处理
在字符集迁移故障处理过程中的方法和技巧:
4.2.1 故障处理流程
# 1. 识别问题
# – 查看错误信息
# – 查看日志文件
# – 检查字符集
# 2. 收集信息
# – 收集错误信息
# – 收集诊断数据
# – 收集字符集信息
# 3. 分析原因
# – 分析错误原因
# – 分析字符集兼容性
# – 分析数据问题
# 4. 制定方案
# – 制定修复方案
# – 制定回滚方案
# – 制定预防措施
# 5. 执行修复
# – 执行修复操作
# – 验证修复结果
# – 记录修复过程
# 示例:处理字符集不兼容
# 1. 识别问题
ORA-12712: new character set must be a superset of old character set
# 2. 收集信息
SQL> select * from nls_database_parameters where parameter like ‘%CHARACTERSET’;
# 3. 分析原因
— 字符集不是超集关系
# 4. 制定方案
— 使用INTERNAL_USE选项强制转换
# 5. 执行修复
SQL> alter database character set internal_use AL32UTF8;
4.3 字符集迁移优化
优化字符集迁移配置的最佳实践:
4.3.1 优化字符集迁移管理
SQL> create or replace procedure manage_charset_results(
p_migration_date in date,
p_source_charset in varchar2,
p_target_charset in varchar2,
p_migration_status in varchar2
) as
begin
dbms_output.put_line(‘— Charset Migration Results Management —‘);
dbms_output.put_line(‘Migration Date: ‘ || to_char(p_migration_date, ‘YYYY-MM-DD HH24:MI:SS’));
dbms_output.put_line(‘Source Charset: ‘ || p_source_charset);
dbms_output.put_line(‘Target Charset: ‘ || p_target_charset);
dbms_output.put_line(‘Migration Status: ‘ || p_migration_status);
— 可以扩展为记录到表中
end manage_charset_results;
/
Procedure created.
# 2. 执行管理存储过程
SQL> set serveroutput on
SQL> exec manage_charset_results(sysdate, ‘ZHS16GBK’, ‘AL32UTF8’, ‘SUCCESS’);
— Charset Migration Results Management —
Migration Date: 2026-03-31 10:00:00
Source Charset: ZHS16GBK
Target Charset: AL32UTF8
Migration Status: SUCCESS
PL/SQL procedure successfully completed.
# 3. 创建字符集迁移历史表
SQL> create table charset_history (
id number primary key,
migration_date date,
source_charset varchar2(50),
target_charset varchar2(50),
migration_status varchar2(50),
migration_duration number,
notes varchar2(4000)
);
Table created.
SQL> create sequence charset_history_seq;
Sequence created.
# 4. 记录迁移历史
SQL> insert into charset_history values (
charset_history_seq.nextval,
sysdate,
‘ZHS16GBK’,
‘AL32UTF8’,
‘SUCCESS’,
30,
‘Charset migration from ZHS16GBK to AL32UTF8’
);
1 row created.
SQL> commit;
Commit complete.
# 5. 查看迁移历史趋势
SQL> column migration_date format a20
SQL> select migration_date, source_charset, target_charset, migration_status
from charset_history
order by migration_date desc
fetch first 12 rows only;
MIGRATION_DATE SOURCE_CHARSET TARGET_CHARSET MIGRATION_STATUS
——————– ————– ————– —————-
2026-03-31 10:00:00 ZHS16GBK AL32UTF8 SUCCESS
2026-03-30 10:00:00 US7ASCII AL32UTF8 SUCCESS
2026-03-29 10:00:00 WE8ISO8859P1 AL32UTF8 SUCCESS
Part05-风哥经验总结与分享
5.1 字符集迁移总结
Oracle字符集迁移需要谨慎操作,可能导致数据丢失或乱码,具有以下特点:
- 字符转换:从一个字符集转换到另一个字符集
- 数据风险:可能导致数据丢失或乱码
- 复杂操作:操作复杂,需要谨慎
- 多种方法:支持多种迁移方法
- 充分测试:需要充分测试
5.2 字符集迁移检查清单
Oracle字符集迁移检查清单:
- 充分测试:在测试环境中充分测试
- 备份数据:迁移前备份数据
- 文档记录:记录迁移过程
- 验证数据:验证迁移后数据完整性
- 经验总结:总结迁移经验
- 定期检查:定期检查字符集状态
5.3 字符集迁移工具推荐
Oracle字符集迁移工具推荐:
- CSSCAN:字符集扫描工具
- CSALTER:字符集迁移脚本
- ALTER DATABASE:数据库字符集修改
- Data Pump:数据泵导入导出
- DMU:数据库迁移工具
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
