oracle exp&imp字符集设置与乱码

本文涉及的产品
云原生数据仓库AnalyticDB MySQL版,基础版 8ACU 100GB 1个月
简介:          在oracle使用exp/imp进行数据迁移时,需要注意xshell、exp/imp字符集、数据库字符集的设置,否则,一旦发生字符集转换而出现中文数据乱码,将前功尽弃。

         oracle使用exp/imp进行数据迁移时,需要注意xshellexp/imp字符集、数据库字符集的设置,否则,一旦发生字符集转换而出现中文数据乱码,将前功尽弃。相较而言,oracle数据库比较人性化,万不得已时,可以调整数据库字符集。下面举一个调整数据库字符集、exp/imp字符集、xshell字符集避免中文乱码的例子。

       首先,源库exp导出时数据库字符集是ZHS16GBKNLS_LANG分别为空、AMERICAN_AMERICA.ZHS16GBK.

       新环境部署新的数据库,需要强调的是,源端和目标端的数据库版本是相同的11.2.0.3,源端数据库字符集是ZHS16GBK,目标端数据库字符集是安装默认为AMERICAN_AMERICA.WE8MSWIN1252

       如果不调整数据库字符集,采用imp导入中文数据时一定会出现乱码

导入前检查主机主机字符集及imp工具客户端字符集,导入工具imp字符集没有设置,数据库字符集为WE8MSWIN1252;导出端exp工具客户端字符集设置为ZHS16GBK,数据库字符集是ZHS16GBK

导入端:

[oracle@orasilent ~]$ echo $NLS_LANG

 

[oracle@orasilent ~]$       

导出日志:

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production

With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,

Data Mining and Real Application Tes

Export done in US7ASCII character set and AL16UTF16 NCHAR character set

server uses ZHS16GBK character set (possible charset conversion)

About to export specified users ...

. exporting pre-schema procedural objects and actions

. exporting foreign function library names for user TNMS

. exporting PUBLIC type synonyms

. exporting private type synonyms

. exporting object type definitions for user TNMS

About to export TNMS's objects ...

. exporting database links

. exporting sequence numbers

. exporting cluster definitions

. about to export TNMS's tables via Conventional Path ...

. . exporting table                ALARM_CRITERION          3 rows exported

. exporting synonyms

. exporting views

. exporting stored procedures

. exporting operators

. exporting referential integrity constraints

. exporting triggers

. exporting indextypes

. exporting bitmap, functional and extensible indexes

. exporting posttables actions

. exporting materialized views

. exporting snapshot logs

. exporting job queues

. exporting refresh groups and children

. exporting dimensions

. exporting post-schema procedural objects and actions

. exporting statistics

Export terminated successfully without warnings.

 

导入日志:

[oracle@orasilent ~]$ imp system/redhat5 fromuser=tnms touser=tnms file=/home/oracle/tnmsexp.dmp log=/home/oracle/tnmsimp.log

Import: Release 11.2.0.3.0 - Production on Mon Aug 24 17:17:24 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

Export file created by EXPORT:V11.02.00 via conventional path

import done in US7ASCII character set and AL16UTF16 NCHAR character set

import server uses WE8MSWIN1252 character set (possible charset conversion)

. importing TNMS's objects into TNMS

. . importing table              "ALARM_CRITERION"          3 rows imported

Import terminated successfully without warnings.

验证中文是否乱码,如下:

        由上图可见,源端、目标端数据库字符集不一致,exp/imp导出客户端工具字符集不一致,一定会导致导入时中文数据乱码。

        因此,需要调整目标端的数据库字符集,操作如下:

[oracle@orasilent ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Mon Aug 24 17:31:17 2015

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to:

Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down.

SQL> startup mount;

ORACLE instance started.

Total System Global Area  409194496 bytes

Fixed Size                2228864 bytes

Variable Size                343936384 bytes

Database Buffers          58720256 bytes

Redo Buffers                  4308992 bytes

Database mounted.

SQL> ALTER SYSTEM ENABLE RESTRICTED SESSION;

System altered.

SQL> ALTER SYSTEM SET JOB_QUEUE_PROCESSES=0;

System altered.

SQL> ALTER SYSTEM SET AQ_TM_PROCESSES=0;

System altered.

SQL> ALTER DATABASE OPEN;

Database altered.

SQL> ALTER DATABASE CHARACTER SET INTERNAL_USE ZHS16GBK;

Database altered.

SQL> shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down.

SQL> startup;

ORACLE instance started.

Total System Global Area  409194496 bytes

Fixed Size                2228864 bytes

Variable Size                343936384 bytes

Database Buffers          58720256 bytes

Redo Buffers                  4308992 bytes

Database mounted.

Database opened.

SQL> SELECT USERENV('language') from dual;

USERENV('LANGUAGE')

----------------------------------------------------

AMERICAN_AMERICA.ZHS16GBK

        现在,源端库与目标端库的字符集调整到一致了,都是ZHS16GBK。接下来导入源端没有设置NLS_LANG,看中文数据是否出现乱码。

        如图所示,没有设置NLS_LANG时,oracleimp导入时,尽管源端与目标端的字符集都相同,但是中文数据依旧乱码。但是,这种显示乱码只是xshell字符集问题,数据库内部并不是真正的乱码,需要调整xshell字符集、设置NLS_LANG

        如图所示,如果导出没有设置字符集,导入时不设置字符集,导出导入时客户端使用的字符集相同,会有字符集转换(EXP:ZHS16GBK-> US7ASCII;IMP: US7ASCII-> ZHS16GBK),但是数据库中中文数据不会出现乱码,需要做的调整是设置xshell字符集和NLS_LANG与数据库保持一致。

 

       导出设置了NLS_LANG时的日志:

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production

With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,

Data Mining and Real Application Tes

Export done in ZHS16GBK character set and AL16UTF16 NCHAR character set

About to export specified users ...

. exporting pre-schema procedural objects and actions

. exporting foreign function library names for user TNMS

. exporting PUBLIC type synonyms

. exporting private type synonyms

. exporting object type definitions for user TNMS

About to export TNMS's objects ...

. exporting database links

. exporting sequence numbers

. exporting cluster definitions

. about to export TNMS's tables via Conventional Path ...

. . exporting table                ALARM_CRITERION          3 rows exported

. exporting synonyms

. exporting views

. exporting stored procedures

. exporting operators

. exporting referential integrity constraints

. exporting triggers

. exporting indextypes

. exporting bitmap, functional and extensible indexes

. exporting posttables actions

. exporting materialized views

. exporting snapshot logs

. exporting job queues

. exporting refresh groups and children

. exporting dimensions

. exporting post-schema procedural objects and actions

. exporting statistics

Export terminated successfully without warnings.

 

如果导入端不设置NLS_LANG

[oracle@orasilent ~]$ echo $NLS_LANG

 

[oracle@orasilent ~]$

[oracle@orasilent ~]$ imp system/redhat5 fromuser=tnms touser=tnms file=/home/oracle/tnmsexp_zhs16gbk.dmp  log=/home/oracle/tnmsexp_zh16gbk1.log

Import: Release 11.2.0.3.0 - Production on Mon Aug 24 19:35:09 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

Export file created by EXPORT:V11.02.00 via conventional path

import done in US7ASCII character set and AL16UTF16 NCHAR character set

import server uses ZHS16GBK character set (possible charset conversion)

export client uses ZHS16GBK character set (possible charset conversion)

. importing TNMS's objects into TNMS

. . importing table              "ALARM_CRITERION"          3 rows imported

Import terminated successfully without warnings.

验证是否乱码:

        只明确导出端exp字符集而不明确导入端imp字符集时,如果xshell字符集与数据库字符集相同,显示不会中文乱码,但是导入时会有提示。


明确exp/imp字符集均为ZHS16GBK,源端目标端数据库字符集也都是ZHS16GBKxshell字符集与数据库保持一致,不会出现中文乱码,并且导入时没有字符集转换的提示,如下所示:

[oracle@orasilent ~]$ echo $NLS_LANG

AMERICAN_AMERICA.ZHS16GBK

[oracle@orasilent ~]$ imp system/redhat5 fromuser=tnms touser=tnms file=/home/oracle/tnmsexp_zhs16gbk.dmp  log=/home/oracle/tnmsexp_zh16gbk2.log

Import: Release 11.2.0.3.0 - Production on Mon Aug 24 19:39:40 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

Export file created by EXPORT:V11.02.00 via conventional path

import done in ZHS16GBK character set and AL16UTF16 NCHAR character set

. importing TNMS's objects into TNMS

. . importing table              "ALARM_CRITERION"          3 rows imported

Import terminated successfully without warnings.

[oracle@orasilent ~]$

相关实践学习
AnalyticDB MySQL海量数据秒级分析体验
快速上手AnalyticDB MySQL,玩转SQL开发等功能!本教程介绍如何在AnalyticDB MySQL中,一键加载内置数据集,并基于自动生成的查询脚本,运行复杂查询语句,秒级生成查询结果。
阿里云云原生数据仓库AnalyticDB MySQL版 使用教程
云原生数据仓库AnalyticDB MySQL版是一种支持高并发低延时查询的新一代云原生数据仓库,高度兼容MySQL协议以及SQL:92、SQL:99、SQL:2003标准,可以对海量数据进行即时的多维分析透视和业务探索,快速构建企业云上数据仓库。 了解产品 https://www.aliyun.com/product/ApsaraDB/ads
目录
相关文章
|
8月前
|
Oracle 关系型数据库 Linux
Oracle设置监听之Navicat的锅
Oracle设置监听之Navicat的锅
125 0
Oracle11G用EXP导出时,空表不能导出解决
Oracle11G用EXP导出时,空表不能导出解决
|
5月前
|
存储 自然语言处理 Oracle
Oracle数据库字符集概述及修改方式
【8月更文挑战第15天】Oracle 数据库字符集定义了数据的编码方案,决定可存储的字符类型及其表示方式。主要作用包括数据存储、检索及跨系统传输时的正确表示。常见字符集如 AL32UTF8 支持多语言,而 WE8MSWIN1252 主用于西欧语言。修改字符集风险高,可能导致数据问题,需事先备份并评估兼容性。可通过 ALTER DATABASE 语句直接修改或采用导出-导入数据的方式进行。完成后应验证数据完整性。此操作复杂,须谨慎处理。
166 5
|
2月前
|
监控 Oracle 关系型数据库
Linux平台Oracle开机自启动设置
【11月更文挑战第8天】在 Linux 平台设置 Oracle 开机自启动有多种方法,本文以 CentOS 为例,介绍了两种常见方法:使用 `rc.local` 文件(较简单但不推荐用于生产环境)和使用 `systemd` 服务(推荐)。具体步骤包括编写启动脚本、赋予执行权限、配置 `rc.local` 或创建 `systemd` 服务单元文件,并设置开机自启动。通过 `systemd` 方式可以更好地与系统启动过程集成,更规范和可靠。
204 2
|
2月前
|
Oracle Ubuntu 关系型数据库
Linux平台Oracle开机自启动设置
【11月更文挑战第7天】本文介绍了 Linux 系统中服务管理机制,并详细说明了如何在使用 systemd 和 System V 的系统上设置 Oracle 数据库的开机自启动。包括创建服务单元文件、编辑启动脚本、设置开机自启动和启动服务的具体步骤。最后建议重启系统验证设置是否成功。
|
6月前
|
SQL Oracle 关系型数据库
关系型数据库Oracle设置 RMAN 环境:
【7月更文挑战第25天】
85 2
|
6月前
|
Oracle 关系型数据库 Unix
关系型数据库Oracle设置环境变量:
【7月更文挑战第22天】
698 4
|
6月前
|
Oracle 关系型数据库 Java
Oracle数据库导入工具IMP详解与用法
Oracle数据库导入工具IMP详解与用法
|
6月前
|
SQL Oracle 关系型数据库
使用Oracle IMP导入数据
使用Oracle IMP导入数据
|
7月前
|
Oracle 关系型数据库 Java
Oracle数据库导入工具IMP详解与用法
Oracle数据库导入工具IMP详解与用法