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
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 ''
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
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
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});
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;
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";
}
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)
)
)