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

发布于 2024-08-30 03:25:17 字数 2189 浏览 6 评论 0原文

我在让 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/[电子邮件受保护]/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. 如何解决环境问题?

我已经使用 httpd.conf 中的 SetEnvPerlSetEnv 正确设置了 DB2INSTANCE、DB2_PATH 和 SQLLIB 变量,但没有成功。

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

I'm having issues with getting DBI's IBM DB2 driver to work with mod_perl. My test script is:

#!/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;
}

This script works as CGI but not under mod_perl. I get this error in apache's error log:

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

First of all, why is it using ODBC? The native DB2 driver is installed (hence it works as CGI).

Running Apache 2.2.3, mod_perl 2.0.4 under RHEL5.

This guy had the same problem as me:
http://www.mail-archive.com/[email protected]/msg22909.html
But I have no idea how he fixed it. What does mod_php4 have to do with mod_perl?

Any help would be greatly appreciated, I'm having no luck with google.

Update:

As james2vegas pointed out, the problem has something to do with PHP: I disable PHP all together I get the a different error:

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

I believe this error is to do with environment variables not being set up correctly, namely DB2INSTANCE. However, I'm not able to turn off PHP to resolve this problem (I need it for some legacy applications). So I now have 2 questions:

  1. How can I fix the original issue without disabling PHP all together?
  2. How can I fix the environment issue?

I've set DB2INSTANCE, DB2_PATH and SQLLIB variables correctly using SetEnv and PerlSetEnv in httpd.conf, but with no luck.

Note: I've edited the code to determine if the problem was to do with Global Variable Persistence.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

停顿的约定 2024-09-06 03:25:17

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

This appears solved, for reference: disable PHP's ODBC support in /etc/php.d/pdo_odbc.ini, set the environment variables and preload DBD::DB2 in mod_perl's startup script, though it might be possible to use PERL_DL_NONLAZY set to 1 to force loading of the correct library.

东北女汉子 2024-09-06 03:25:17

普通的旧式 DBI 在 mod_perl 中不起作用;当您的进程分叉并且您尝试再次使用数据库句柄时,一切都会变得不稳定。您需要使用 Apache::DBI (它是 DBI 的直接替代品),或者更好的是,使用后现代 DBI 包装器,例如 DBIx::Connector

您可以在此处阅读更多详细信息: Apache 将 mod_perl 与关系数据库结合使用的指南

Plain old DBI won't work in mod_perl; it goes all wibbly when your process forks and you try to use your db handle again. You need to use Apache::DBI (it's a drop-in replacement for DBI), or even better, use a post-modern DBI wrapper like DBIx::Connector.

You can read more details here, at Apache's guide to using mod_perl with relational databases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文