Home  >  Article  >  Database  >  Erlang在Redhat 5.3下使用unixODBC连接Oracle数据库的配置

Erlang在Redhat 5.3下使用unixODBC连接Oracle数据库的配置

WBOY
WBOYOriginal
2016-06-07 15:36:361556browse

个人在安装配置时遇到一些麻烦,特此记录如下: 环境 数据库服务器操作系统:Windows 2003 数据库:Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 erlang运行的服务器操作系统:Redhat 5.3 erlang:Erlang R14B03 (erts-5.8.4) 注意 ==========

个人在安装配置时遇到一些麻烦,特此记录如下:

  • 环境

数据库服务器操作系统:Windows 2003

数据库:Oracle Database 10g Enterprise Edition Release 10.2.0.1.0

erlang运行的服务器操作系统:Redhat 5.3
erlang:Erlang R14B03 (erts-5.8.4)

  • 注意
===============================================================================
redhat 5.3中默认安装的unixODBC是2.2.11版本,这个版本与oracle的odbc驱动不兼容。所以需要将先将其卸载,然后安装unixODBC 2.3.0
卸载unixODBC的rpm包:
rpm -e mysql-connector-odbc-3.51.12-2.2.i386
rpm -e unixODBC-devel-2.2.11-7.1
rpm -e unixODBC-2.2.11-7.1

下载安装unixODBC 2.3.0
从www.unixodbc.org处下载,或者
http://olex.openlogic.com/package_versions/download/10003?package_version_id=5966&path=openlogic%2Funixodbc%2F2.3.0%2Fopenlogic-unixodbc-2.3.0-all-src-2.zip
在configure时加两个参数:--sysconfdir=/etc --prefix=/usr
然后make install就可以啦

安装完成后执行odbcinst -j可以看到配置文件等的位置
===============================================================================

1. 从Oracle网站下载客户端安装包
http://download.oracle.com/otn/linux/instantclient/11203/oracle-instantclient11.2-basic-11.2.0.3.0-1.i386.rpm
http://download.oracle.com/otn/linux/instantclient/11203/oracle-instantclient11.2-odbc-11.2.0.3.0-1.i386.rpm
最好再安装一下sqlplus
http://download.oracle.com/otn/linux/instantclient/11203/oracle-instantclient11.2-sqlplus-11.2.0.3.0-1.i386.rpm

2. 配置lib目录
cd /usr/lib/oracle/11.2/client/lib
pwd >>/etc/ld.so.conf
ldconfig

3. 修改/etc/odbcinst.ini
增加如下:
# Driver form oracle
[Oracle]
Desription      = ODBC for Oracle
Driver          = /usr/lib/oracle/11.2/client/lib/libsqora.so.11.1
Setup           = /usr/lib/liboraodbcS.so
FileUsage       = 1

这个加到odbcinst.ini文件的最后。测试时用,正常使用时应该去掉
[ODBC]
Trace           = 1
TraceFile    =/tmp/odbc.log
Debug           = 1
Pooling         = No


4. 修改/etc/odbc.ini

增加如下
[ORCL_146] #本地数据源名
Description     = ODBC for Oracle
Driver          = Oracle
Server          = 192.168.1.146
Port            = 1521
ServerName      = ORCL_146 #tnsnames.ora中的服务名
UserID          = mycomm
Password        = mycomm123



5. 增加/usr/lib/oracle/11.2/network/admin/tnsnames.ora

ORCL_146 =
        (DESCRIPTION =
                (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.146)(PORT = 1521))
                (CONNECT_DATA =
            (SERVER = DEDICATED)
                        (SERVICE_NAME = orcl)
                )
        )


其中ORCL_146要与odbc.ini的ServerName相同,
SERVICE_NAME为oracle的服务名
HOST为数据库所在主机IP,PORT为数据库所在主机port。

6. 在/etc/profile中增加:
export TNS_ADMIN=/usr/lib/oracle/11.2/network/admin/
然后,为了让TNS_ADMIN环境变化现在就生效,执行
. /etc/profile

7. 用isql测试一下
isql ORCL_146 -v
出错:
[01000][unixODBC][Driver Manager]Can't open lib '/usr/lib/oracle/11.2/client/lib/libsqora.so.11.1' : libclntsh.so.11.1: cannot open shared object file: No such file or directory
[ISQL]ERROR: Could not SQLConnect
发现是没有libclntsh.so.11.1这个库文件,这个库文件在/usr/lib/oracle/11.2/client/lib/下,之所以没有找到是由于在第2步时执行ldconfig的终端不是现在执行isql的终端。
重新执行一下ldconfig

再执行isql ORCL_146 -v
出错:
isql: symbol lookup error: /usr/lib/oracle/11.2/client/lib/libsqora.so.11.1: undefined symbol: SQLGetPrivateProfileStringW

这个问题就是unixODBC的版本问题啦,如果前面安装了unixODBC 2.3.0的话不会出现这个错误

如果sqlplus可以连接,但isql不可以,需要确认是否export了TNS_ADMIN这一环境变量


8. erlang odbc 连接oralce时出错如下:
------------------------------------------
Erlang R14B03 (erts-5.8.4) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.8.4  (abort with ^G)
1> odbc:start().
ok
2> odbc:connect("DSN=ORCL_146;UID=scott;PWD=tiger", []).

=ERROR REPORT==== 21-Oct-2011::19:23:49 ===
ODBC: received unexpected info: {tcp_closed,#Port}

{error,connection_closed}
3>
=ERROR REPORT==== 21-Oct-2011::19:23:49 ===
** Generic server terminating
** Last message in was {#Port,{exit_status,23}}
** When Server state == {state,#Port,
                               {,#Ref},
                               ,undefined,on,undefined,undefined,on,
                               connecting,undefined,0,
                               [#Port,#Port],
                               #Port,#Port}
** Reason for termination ==
** {port_exit,collecting_of_driver_information_faild}
----------------------------------------------
根据这里的方法
http://www1.erlang.org/pipermail/erlang-questions/2005-August/016816.html
这样连接就可以啦
3> odbc:connect("DSN=ORCL_146;UID=scott;PWD=tiger", [{scrollable_cursors, off}]).  
{ok,}
4>
其实这里可以不用UID和PWD,只要一个DSN就可以啦,形如:
odbc:connect("DSN=ORCL_146", [{scrollable_cursors, off}]).



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