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

简介:          在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.

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

Z

        由上图可见,源端、目标端数据库字符集不一致,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,看中文数据是否出现乱码。

9k=

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

Z

        如图所示,如果导出没有设置字符集,导入时不设置字符集,导出导入时客户端使用的字符集相同,会有字符集转换(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.

验证是否乱码:

Z

        只明确导出端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 ~]$

Z

相关实践学习
数据库实验室挑战任务-初级任务
本场景介绍如何开通属于你的免费云数据库,在RDS-MySQL中完成对学生成绩的详情查询,执行指定类型SQL。
阿里云云原生数据仓库AnalyticDB MySQL版 使用教程
云原生数据仓库AnalyticDB MySQL版是一种支持高并发低延时查询的新一代云原生数据仓库,高度兼容MySQL协议以及SQL:92、SQL:99、SQL:2003标准,可以对海量数据进行即时的多维分析透视和业务探索,快速构建企业云上数据仓库。 了解产品 https://www.aliyun.com/product/ApsaraDB/ads
目录
相关文章
|
4月前
|
Oracle 关系型数据库 Linux
Oracle设置监听之Navicat的锅
Oracle设置监听之Navicat的锅
32 0
|
5月前
Oracle11G用EXP导出时,空表不能导出解决
Oracle11G用EXP导出时,空表不能导出解决
|
Oracle 网络协议 关系型数据库
Oracle会话超时设置1:在sqlnet.ora和listener.ora中设置
这篇文章是Oracle会话超时设置的第一个文章,简述和Oracle 数据库 net services有关的会话超时管理。
1174 0
Oracle会话超时设置1:在sqlnet.ora和listener.ora中设置
|
SQL 运维 Oracle
Oracle 超时设置2:设置实例级参数
Oracle超时设置系列的第二篇文章,设置实例级参数
472 0
|
1月前
|
存储 Oracle 关系型数据库
Oracle系列之三:Oracle字符集
Oracle系列之三:Oracle字符集
|
8月前
|
Oracle 关系型数据库 数据库
Oracle——给数据库的某个字段设置随机数
Oracle——给数据库的某个字段设置随机数
|
8月前
|
Oracle 关系型数据库 Linux
Oracle设置连接数进程数会话数
Oracle设置连接数进程数会话数
127 0
|
9月前
|
Oracle 关系型数据库
ORACLE中设置ID自增详细
ORACLE中设置ID自增详细
204 0
|
10月前
|
Oracle 关系型数据库 数据库
【Oracle】设置自增
Oracle Database,又名OracleRDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。可以说Oracle数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小、微机环境。它是一种高效率、可靠性好的适应高吞吐量的数据库解决方案。
144 0
|
10月前
|
Oracle 前端开发 关系型数据库
在Oracle的ADR中设置自动删除trace文件的策略
姚远在一个有两万个客户的公司做数据库支持,什么稀奇古怪的事情都能遇到,有个客户的数据库不停地产生大量的trace,经常把硬盘撑爆,看看姚远怎么解决这个问题的。