linux环境下perl访问Oracle的步骤

oracle需要文件准备
下载oracle在linux环境下的接口库:
http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
按照网页说明进行下载和设置
共三个文件:
instantclient-basic-linux.x64-12.1.0.2.0.zip
instantclient-sdk-linux.x64-12.1.0.2.0.zip
instantclient-sqlplus-linux.x64-12.1.0.2.0.zip
安装说明:
1、将这个三个文件放到一个目录下,直接将三个文件用unzip解压缩,这三个文件会解压缩到同一个目录
我的放到了/usr/local/src,解压缩后的目录是/usr/local/src/instantclient_12_1
2、做软链
cd /usr/local/src/instantclient_12_1
ln -s libclntsh.so.12.1 libclntsh.so
ln -s libocci.so.12.1 libocci.so

3. 设置环境变量:

export LD_LIBRARY_PATH=/usr/local/src/instantclient_12_1:$LD_LIBRARY_PATH

export PATH=/usr/local/src/instantclient_12_1:$PATH

 
perl需要文件准备
1、下载DBI和DBD
如果失效,可以去cpan: http://search.cpan.org/ 搜索下载
2、查看perl是否安装,未安装还得装perl
perl -v
3、安装DBI,应该先安装DBI否则DBD无法安装成功
解压缩后进压缩文件夹执行
 perl Makefile.PL 
 make
 make install
4、安装DBD-Oracle
tar -zxvf DBD-Oracle-1.74.tar.gz
如果root下进行安装出现错误如下,则表明没未设置环境变量ORACLE_HOME
perl Makefile.PL 
Using DBI 1.605 (for perl 5.008005 on i386-linux-thread-multi) installed in /usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi/auto/DBI/
Configuring DBD::Oracle for perl 5.008005 on linux (i386-linux-thread-multi)
Remember to actually *READ* the README file! Especially if you have any problems.
Trying to find an ORACLE_HOME
Your LD_LIBRARY_PATH env var is set to ''
      The ORACLE_HOME environment variable is not set and I couldn't guess it.
      It must be set to hold the path to an Oracle installation directory
      on this machine (or a machine with a compatible architecture).
      See the appropriate README file for your OS for more information.
      ABORTED!
设置ORACLE_HOME为之前解压缩下载文件的目录
export ORACLE_HOME=/usr/local/src/instantclient_12_1
perl Makefile.PL 
make
make install
5、验证结果
脚步如下:
db.pl :
#!/usr/bin/perl  -w
use DBI;
my $tnsname="orcl";
my $dbUser="test";
my $dbPassword="test";
my $dbh = DBI->connect("dbi:Oracle:$tnsname",$dbUser,$dbPassword, {'RaiseError' => 1});
# Insert one row
my $rows = $dbh->do("INSERT INTO t (id) VALUES (1)");
# query
my $sth = $dbh->prepare("SELECT id  FROM t");
$sth->execute;
while(my @ref = $sth->fetchrow_array ) {
    print "$ref[0]\n";
}
$dbh->disconnect();
 
报错处理:
1、如果报这种错:
DBI connect('BOQU','boqu',...) failed: ORA-12154: TNS:could not resolve the connect identifier specified (DBD ERROR: OCIServerAttach) at ./fetch.pl line 11
则在ORACLE_HOME下新建NETWORK目录和ADMIN目录,并建tns文件tnsnames.ora,并设置TNS_ADMIN变量
export TNS_ADMIN=/usr/local/src/instantclient_12_1/NETWORK/ADMIN
类似这种
TEST=
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = xxx.xxx.xxx.xxx)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = XE)
    )
  )

 

转载于:https://www.cnblogs.com/selfthing/p/4384842.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值