Home >php教程 >PHP开发 >Some thoughts on solving Oracle Chinese garbled characters under Linux

Some thoughts on solving Oracle Chinese garbled characters under Linux

高洛峰
高洛峰Original
2016-12-15 16:46:572207browse

After recently installing Oracle 10gR2 on Linux, I encountered the problem of garbled character sets. I looked for it online before, and then let it go after solving it. This time I encountered such a problem again, so I took it seriously. I took some time to test it. After some testing, the problem has been solved. Now I will record the problems and solutions I encountered so that I can find them in the future.

The test environment is as follows:

Test platform: VMware? Workstation 9.0.2 build-1031769 (Note: VM is not divided into 32 or 64-bit, but if you install a 64-bit operating system, an error will be reported, and the explanation will be given below)
Operating system: Linux RedHat5.6 64-bit operating system
Database version : Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit


1. An error occurs when installing a 64-bit linux operating system or other 64bit operating system on the VM platform.
1. Since the previous installations were all 32-bit linux operating systems, So everything was fine until I switched to a new company and used 64-bit Linux. I encountered this problem. When the VM directly installed 64-bit Linux, it would report that the number of bits of the operating system was different from that of the VM platform (because at that time There is no screenshot, so I have forgotten the specific prompt), similar to this prompt: "You have configured this virtual machine to use a 64-bit guest operation system. However, 64-bit operation is not possible. This host does not support VT." To solve this problem, we need to enable the system's VT support, that is, enable the BIOS virtualization technology of our own computer. This depends on whether the specific personal computer mainly supports virtualization technology. There is a tool that can check your computer's CPU and motherboard. Whether virtualization is supported can be found on Baidu. It is very easy to find. As long as you enable virtualization technology in the BIOS and then reinstall Linux, everything will be normal.下 2. Oracle shows Chinese garbled in Chinese under Linux
1. The reasons why the ORACLE database appears:
The operating system is consistent with the server, but the client is inconsistent with the server character set. .Solution:
                                                                                                                                                                          Set up the relevant character sets to ensure that the character sets of the operating system, client, and database server are the same.

3. Based on the situation:
Set the character set of the client to be consistent with the server. If the character set of the server is: UTF8 (it is recommended to use this character set when creating a database, it is compatible with both Chinese and English)
Client Linux setting steps:
1. Switch to the installation user of the oracle database, my user name is oracle
                                                                                                                                                                                                                                                                                                                                                                       ​ //Edit the .bash_profile file under the oracle user
File content: (The specific settings of this file may be different for everyone, don’t worry, just pay attention to the red line)                                                PATH=$PATH:$HOME/bin:/usr/bin:/usr/sbin:/sbin
                                                export PATH
                                                export ORACLE_SID=orcl 
                                                export ORACLE_BASE=/u01/app/oracle 
                                                export ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1 
                                                export PATH=$PATH:$ORACLE_HOME/bin 
                                                export ORACLE_TERM=xterm 
                                                export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib
                                                export CLASSPATH=$ORACLE_HOME/JRE:ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib

                                               export NLS_LANG=AMERICAN_AMERICA.UTF8                        //设置这个参数就代表设置用户客户端的字符集,在创建数据库的时候不设置这个参数也没问题,建议是最好规划好自己的库需要用到什么时候类型的字体,然后再设置这个值,最好保持与数据库一致,当然这个参数可以随时改变.(我自己的数据库是用UTF8字符集)

                                       $ source .bash_profile                //执行source命令使修改完的.bash_profile文件生效
                         至此,设置完毕,再次登录Oracle数据库,查询显示正常,但有一点要注意,就是如果你插入数据时的字符集不是UTF8下执行的,当你客户端和服务器都变成UTF8字符集后,以后插入的中文可能显示就为乱码了。所以说只有三个点(客户端、操作系统、数据库)的字符集保持一致的时候才能让数据库里面的中文正常显示。

          3.针对情况       
Since my operating system is LINUX, I will only give my own method for the Linux platform. Since I installed Linux+Oracle on a virtual machine to test Chinese fonts, I chose to install Chinese directly on Linux. Input method, test by directly inputting Chinese (PS: Of course, the company server does not need to consider this issue, because programmers will set their own fonts to be consistent with the database). The reason why I installed the input method is that I want to try using the three Under the same circumstances, there will be no garbled characters whether you input any Chinese characters. Because in the past, when the characters of the three (operating system, client, database) on my virtual machine were inconsistent, they often appeared in the test database. Sometimes Chinese characters could be inserted and displayed normally, but sometimes they did not. Therefore, in order to ensure that the characters input by my input method are also UTF8 and that I can input Chinese directly on the virtual machine, I installed the Chinese input method directly on Linux. L 1. Here, the steps of installing the Chinese input method under Linux are also listed here, which is also convenient for it to find it yourself.换 Switch to ROOT users under Linux, load the linux installation disc or ISO mirror file on the virtual machine
# Mount /DEV /CDROM /MNT // load the ISO file to the MNT directory, and then enter the server directory to find the Chinese Chinese to be installed. Input method installation package
                                                                                                                                                                                                                                                                                                                                                              The order of the order is installed. After the installation, restart linux)
1. SCIM-LIBS-1.4-41.EL5.X86_64.rpm 2. SCIM -.4-41.EL5.X86_64.rpm 3. sciM- chinese-standard-0.0.2-1.el5.x86_64.rpm                                                         scim-tables-chinese-0.5.6-7.x86_64.rpm -pinyin-0.5.91-16.el5.x86_64.rpm
# RPM -IVH Scim-Libs-1.4.4-41.EL5.X86_64.rpm // Only the first package method is listed here. The installation method of the latter 5 packs is the same as this. Baidu, remember to restart linux after installation.

          2. After installing the Chinese input method, switch to the root user and enter the directory /etc/sysconfig/i18n directory to set the character set of the linux operating system.
                                                                              Input: locale) //Check the character set of the linux operating system under the oracle user
                                                 # vim /etc/sysconfig/i18n                                                                 The default character set of linux in i18n is as follows: LANG="en_US.GBK" //Change the value of this item to the UTF8 character set to: LANG="en_US.UTF-8" en_US represents the system It is displayed in English, and the Chinese version is: zh_CN 18n                   // Execute the source command to make the modification of the i18n file take effect.
(PS: In fact, when modifying this folder, I encountered a problem, that is, I cannot find the ZHS16GBK character set under Linux. I am using a Redhat5.6 x64 system, so when I set LANG to: LANG="en_US.ZHS16GBK", and then execute When using the source command, I found that an error would be reported, which means that there is no ZHS16GBK character set. The ZHS16GBK character set cannot be found using the command locale -a. This is why I selected UTF8 as the character set of the three in the tester environment. The reason, but then I think about it, as long as the character set of the client and the oracle server are the same on the server of a general company, the operating system should not have much impact if it is ignored, because programmers can specify the ZHS16GBK encoding in their own programs when developing. , so that the character sets of the program + client + oracle server will be consistent. Is this guess correct? I am still looking online to find out how to install the ZHS16GBK character set).

3. After modification, switch back to the oracle user, and then use the command locale or echo $LANG to check the current operating system character set, you will find It is already consistent with the client and database, both are UTF8. At this time, when you enter the test environment of the Oracle database, inserting Chinese into the table, modifying, and querying will all be displayed normally, and there will no longer be garbled characters.


Summary:
Character set conversion is a very complicated process. The above are just the problems I encountered myself. There may be some more complex problems. I will slowly add them after I discover them. But the premise is nothing more than maintaining the three (operating system, operating system, Client, database server) have the same character set. In addition, I found that different character sets can also be converted, but conversion will be dangerous because you don’t know when the conversion will go wrong, so the safest way is to keep Consistent. And it is best not to change the character set of the database. Unless it is a test environment, it is best not to change the official production library. The risk is very high. So when creating a database, you need to think about which character set to use.


Example:

                                                                                                                                                                 . ---------------------------------- 99 We are all good boys 123 Guangzhou Hua Information Co., Ltd. SYS@orcl> ;insert into test values(3,'Baiyun District, Guangzhou, China');1 row created.SYS@orcl>commit;Commit complete.SYS@orcl>select * from test; ID NAME--------- ---------------------------------------- 99 We are all good kids 123 Guangzhou Hua Information Co., Ltd. 3 Baiyun District, Guangzhou, China



For more experience on solving Oracle Chinese garbled characters under Linux, please pay attention to the PHP Chinese website for related articles!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn