当前位置: 首页 > 工具软件 > Perl DBI > 使用案例 >

perl DBI模块和DBD-mysql模块安装

李兴安
2023-12-01

首先,来一段代码来自NinGoo的检测系统中已经安装的Perl模块:

[root@rh65sql1 tmp]# cat check_module.pl 
#!/usr/bin/perl

use ExtUtils::Installed;
my $inst = ExtUtils::Installed->new();
print join "\n",$inst->modules();
print"\n";
安装前的结果:

[root@rh65sql1 tmp]# ./check_module.pl 
Perl

一、安装DBI模块

1.下载模块http://download.csdn.net/detail/u010587433/8857567(或者http://search.cpan.org/~timb/DBI-1.633/

2.将下载模块上传到相应服务器上:/perl/Module/

3.解压安装 

tar -zxvf DBI-1.633.tar.gz
cd DBI-1.633
perl Makefile.PL [PREFIX="/perl/Module/DBI"] <span style="color:#ff6666;">#可以安装自定义目录</span>
make 
make test
make install

4.简单测试

<pre name="code" class="html"><pre name="code" class="html">[root@rh65sql1 tmp]# cat testDBI 
#!/usr/bin/perl -w

use strict;
use DBI;
print "Text DBI success!\n";
[root@rh65sql1 tmp]# ./testDBI 
Text DBI success!

 
 

[root@rh65sql1 tmp]# ./check_module.pl 
DBI
Perl

说明:使用自定义目录,在调用DBI时将use DBI换成use lib "/perl/Module/DBI/lib64/perl5/"; require DBI;

二、安装DBD::Mysql模块

1.下载模块: http://download.csdn.net/detail/u010587433/8857609(或者 http://search.cpan.org/~capttofu/DBD-mysql-4.018/

2.上传服务器

3.安装同上

tar -zxvf DBD-mysql-4.032_01.tar.gz
cd DBD-mysql-4.032_01
perl Makefile.PL [PREFIX="/perl/Module/Mysql"] #可以安装自定义目录
make 
make test
make install
4.测试

<pre name="code" class="html">[root@rh65sql1 tmp]# cat check_mysql.pl 
#!/usr/bin/perl

use strict;
use DBI;
use DBD::mysql;
my $user = "root";
my $passwd = "123456";
my $host = "192.168.7.221";
my $db = "test";
my $data_source = "DBI:mysql:database=$db;host=$host";
my $dbh = DBI->connect($data_source,$user,$passwd,{'RaiseError'=>1}) ;
my $sql = qq{select now()};
my $sth = $dbh->prepare($sql);
$sth->execute();
my $set;
$sth->bind_columns(undef,\$set);
while($sth->fetch()){
        print $set."\n";
}
$sth->finish();
$dbh->disconnect();

 使用的root用户安装在默认路径下/usr/local/lib64/per5。安装完mysql模块后,测试报错: 

[root@rh65sql1 tmp]# ./check_modu_mysql.pl 
Can't load '/usr/local/lib64/perl5/auto/DBD/mysql/mysql.so' for module DBD::mysql: libmysqlclient.so.18: cannot open shared object file: No such file or directory at /usr/lib64/perl5/DynaLoader.pm line 200.

查找是否存在此Lib

[root@rh65sql1 tmp]# find / -name libmysqlclient.so.18
/usr/local/mysql/lib/libmysqlclient.so.18
/usr/local/mysql-5.6.4-m7/libmysql/libmysqlclient.so.18

修复

[root@rh65sql1 tmp]# ldd /usr/local/lib64/perl5/auto/DBD/mysql/mysql.so
        linux-vdso.so.1 =>  (0x00007ffff9198000)
        <span style="color:#ff6666;">libmysqlclient.so.18 => not found</span>
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f0e71568000)
        libz.so.1 => /lib64/libz.so.1 (0x00007f0e71352000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f0e710ce000)
        librt.so.1 => /lib64/librt.so.1 (0x00007f0e70ec5000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f0e70cc1000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f0e7092d000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003ae0400000)

发现在mysql的安装目录文件,把文件拷贝过去(注意目录)

[root@rh65sql1 tmp]# cp /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/
之后修复一下:

[root@rh65sql1 tmp]# ldd /usr/local/lib64/perl5/auto/DBD/mysql/mysql.so
        linux-vdso.so.1 =>  (0x00007fff8dbff000)
        libmysqlclient.so.18 => /usr/lib64/libmysqlclient.so.18 (0x00007fcd63100000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fcd62ee3000)
        libz.so.1 => /lib64/libz.so.1 (0x00007fcd62ccc000)
        libm.so.6 => /lib64/libm.so.6 (0x00007fcd62a48000)
        librt.so.1 => /lib64/librt.so.1 (0x00007fcd62840000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007fcd6263b000)
        libc.so.6 => /lib64/libc.so.6 (0x00007fcd622a7000)
        libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007fcd61fa1000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fcd61d8a000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003ae0400000)

最后测试成功:

[root@rh65sql1 tmp]# ./check_mysql.pl 
2015-07-01 12:13:41
查看已安装的模块

[root@rh65sql1 tmp]# ./check_module.pl 
DBD::mysql
DBI
Perl

参考:

http://yonghui702.blog.163.com/blog/static/81718342011629111724666/

http://blog.sina.com.cn/s/blog_9d0445d50101czsr.html


 类似资料: