如何使用DBI和mod_perl连接到DB2?

时间:2010-04-23 00:49:52

标签: perl db2 dbi mod-perl

我遇到了让DBI的IBM DB2驱动程序与mod_perl一起使用的问题。我的测试脚本是:

#!/usr/bin/perl

use strict;
use CGI;
use Data::Dumper;
use DBI;

{
    my $q;
    my $dsn;
    my $username;
    my $password;
    my $sth;
    my $dbc;
    my $row;

    $q = CGI->new;
    print $q->header;
    print $q->start_html();

    $dsn = "DBI:DB2:SAMPLE";
    $username = "username";
    $password = "password";

    print "<pre>".$q->escapeHTML(Dumper(\%ENV))."</pre>";

    $dbc = DBI->connect($dsn, $username, $password);

    $sth = $dbc->prepare("SELECT * FROM SOME_TABLE WHERE FIELD='SOMETHING'");
    $sth->execute();
    $row = $sth->fetchrow_hashref();
    print "<pre>".$q->escapeHTML(Dumper($row))."</pre>";

    print $q->end_html;
}

此脚本用作CGI,但不在mod_perl下。我在apache的错误日志中收到此错误:

DBD::DB2::dr connect warning: [unixODBC][Driver Manager]Data source name not found, and no default driver specified at /usr/lib/perl5/site_perl/5.8.8/Apache/DBI.pm line 190.
DBI connect('SAMPLE','username',...) failed: [unixODBC][Driver Manager]Data source name not found, and no default driver specified at /data/www/perl/test.pl line 15

首先,为什么使用ODBC?已安装本机DB2驱动程序(因此它可用作CGI)。

在RHEL5下运行Apache 2.2.3,mod_perl 2.0.4。

这家伙和我有同样的问题: http://www.mail-archive.com/dbi-users@perl.org/msg22909.html 但我不知道他是如何修理它的。 mod_php4与mod_perl有什么关系?

任何帮助都会非常感激,我对谷歌没有运气。

更新

正如james2vegas指出的,这个问题与PHP有关:我一起禁用PHP,我得到了一个不同的错误:

 Total Environment allocation failure! Did you set up your DB2 client environment?

我认为这个错误与未正确设置的环境变量有关,即DB2INSTANCE。但是,我无法关闭PHP来解决这个问题(我需要它用于一些遗留应用程序)。所以我现在有两个问题:

  1. 如何在不同时禁用PHP的情况下解决原始问题?
  2. 如何解决环境问题?
  3. 我使用SetEnv中的PerlSetEnvhttpd.conf正确设置了DB2INSTANCE,DB2_PATH和SQLLIB变量,但没有运气。

    注意:我编辑了代码以确定问题是否与全局变量持久性有关。

2 个答案:

答案 0 :(得分:1)

普通的旧DBI在mod_perl中不起作用;当你的进程分叉并且你再次尝试使用数据库句柄时,它会全部扭曲。您需要使用Apache::DBI(它是DBI的直接替代品),甚至更好,使用像DBIx::Connector这样的后现代DBI包装。

您可以在Apache's guide to using mod_perl with relational databases了解更多详情。

答案 1 :(得分:1)

这似乎已解决,供参考:在/etc/php.d/pdo_odbc.ini中禁用PHP的ODBC支持,在mod_perl的启动脚本中设置环境变量和预加载DBD::DB2,尽管可以使用{ {3}}设置为1以强制加载正确的库。

相关问题