oracle 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

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29357786/viewspace-2056179/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29357786/viewspace-2056179/

你可能感兴趣的:(oracle exp&imp字符集设置与乱码)