Perl 脚本在命令提示符下工作正常,但作为 CGI 脚本在 ODBC 上失败

发布于 2024-10-04 05:51:52 字数 2391 浏览 5 评论 0原文

问题:Perl 脚本在命令提示符下工作正常,但在 ODBC 上作为 CGI 脚本失败

设置: Win XP Pro 64 位版本 2003 SP2 阿帕奇 2.2.17 Win32 活动 Perl 5.12.2(不是 Perl64) 为 SQL SERVER 创建 ODBC DSN

可以正常工作

详细信息:从命令提示符处,Phone.pl 运行正常 - 它按预期获得查询结果 - 这意味着 DBI作为 CGI 脚本 ,Phone.pl 在 ODBC 初始化代码之前按预期打印 HTML。 (Apache 设置适用于非 ODBC 脚本。)

这是 Apache 的 error.log 中的消息

[Tue Nov 23 13:27:57 2010] [error] [client 127.0.0.1] DBI connect('SQLSVR206','administrator ',...) 失败:[Microsoft][ODBC 驱动程序管理器] 在 C:/CGI/Phone.pl 第 32 行找不到数据源名称且未指定默认驱动程序 (SQL-IM002)

知道为什么它的行为不同吗?

#!c:/perl/bin/perl.exe
use CGI qw(:standard);
use strict;
use warnings;
use DBI;

my ($sqlstr, $sql_UNITED, $dbh206);

# 206
# select * from getProvFINON('270762789')#TIN
my @colUNITED = qw/ADR_LN_1_TXT  PROV_SYS_ID  ZIP_CD  FULL_NM  SOURCE  PROV_TIN  BILL_TEL/;

sub Init(){
    $dbh206 = DBI->connect ("dbi:ODBC:SQLSVR206",'USER','PASSWORD',{RaiseError=>1})
        or die $DBI::errstr;
    $sqlstr = qq/ select * from getProvFINON(?) /; #parameters TIN
    $sql_UNITED = $dbh206->prepare($sqlstr)
        or die "prepare failed: " . $dbh206->errstr(); 
}

sub test1{
    my $tin = shift();
    $sql_UNITED->execute($tin) or die $sql_UNITED->errstr;
    my $row = $sql_UNITED->fetchrow_hashref;
    while(defined ($row)){
    foreach(@colUNITED){
        print $row->{$_}."~~~";
    }
    print "<p>";
    $row = $sql_UNITED->fetchrow_hashref;
    }
}

print header;
print "test text<p>";
Init();
test1('270762789');
print "999999999999<p>";

=================================================== ======== 更新:部分问题是 32 位 VS 64 位 ODBC 问题 请参阅 http://support.microsoft.com/kb/942976

但是,在设置 ODBC 之后, ,我在 Apache 日志中仍然收到以下错误 [2010 年 11 月 24 日星期三 01:38:48] [错误] [客户端 127.0.0.1] DBI 连接('SQLSVR206-32','',...)失败:[Microsoft][ODBC SQL Server 驱动程序][SQL Server ]用户“MYDOMAIN\GARY$”登录失败。 (SQL-28000) 在 C:/CGI/test.pl 第 15 行,

我以 MYDOMAIN\administrator 身份登录并启动 Apache。 ODBC 设置为使用“使用网络登录 ID 进行 Windows NT 身份验证” 这个“MYDOMAIN\GARY$”从何而来?谢谢!

=================================================== ======== 最终更新:Apache 服务使用“本地系统帐户”运行,这就是“MYDOMAIN\GARY$”的来源。问题是 GARY 不在公司,他的 ID 已从 SQL Server 中删除,但 CGI 中的 ODBC 代码从 Apache 继承了 GARY,导致错误 28000。 所以问题是一个卷积 1) 32/64 位 ODBC DSN 和 2) Apache使用的默认帐户

Issue: Perl script works fine in command prompt, but fails on ODBC as a CGI script

Settings:
Win XP Pro 64 bit version 2003 SP2
Apache 2.2.17 Win32
Active Perl 5.12.2 (not Perl64)
an ODBC DSN is created for SQL SERVER

Details: from command prompt, Phone.pl runs OK - it gets query results as expected - which means DBI works fine

as a CGI script, Phone.pl prints HTML as expected before ODBC init code. (Apache settings work OK for non-ODBC script.)

here's the msg in error.log of Apache

[Tue Nov 23 13:27:57 2010] [error] [client 127.0.0.1] DBI connect('SQLSVR206','administrator',...) failed: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (SQL-IM002) at C:/CGI/Phone.pl line 32

Any idea why it behaves differently?

#!c:/perl/bin/perl.exe
use CGI qw(:standard);
use strict;
use warnings;
use DBI;

my ($sqlstr, $sql_UNITED, $dbh206);

# 206
# select * from getProvFINON('270762789')#TIN
my @colUNITED = qw/ADR_LN_1_TXT  PROV_SYS_ID  ZIP_CD  FULL_NM  SOURCE  PROV_TIN  BILL_TEL/;

sub Init(){
    $dbh206 = DBI->connect ("dbi:ODBC:SQLSVR206",'USER','PASSWORD',{RaiseError=>1})
        or die $DBI::errstr;
    $sqlstr = qq/ select * from getProvFINON(?) /; #parameters TIN
    $sql_UNITED = $dbh206->prepare($sqlstr)
        or die "prepare failed: " . $dbh206->errstr(); 
}

sub test1{
    my $tin = shift();
    $sql_UNITED->execute($tin) or die $sql_UNITED->errstr;
    my $row = $sql_UNITED->fetchrow_hashref;
    while(defined ($row)){
    foreach(@colUNITED){
        print $row->{$_}."~~~";
    }
    print "<p>";
    $row = $sql_UNITED->fetchrow_hashref;
    }
}

print header;
print "test text<p>";
Init();
test1('270762789');
print "999999999999<p>";

==========================================================
UPDATE: part of the problem is the 32-bit VS 64-bit ODBC issue
see http://support.microsoft.com/kb/942976

HOWEVER, after I set up ODBC, I still got following error in Apache log
[Wed Nov 24 01:38:48 2010] [error] [client 127.0.0.1] DBI connect('SQLSVR206-32','',...) failed: [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'MYDOMAIN\GARY$'. (SQL-28000) at C:/CGI/test.pl line 15

I logged in as MYDOMAIN\administrator and started Apache.
ODBC was set up to use "With Windows NT authentication using the network login ID" Where does this "MYDOMAIN\GARY$" come from? Thanks!

==========================================================
FINAL UPDATE: Apache services runs using "Local System account", and that's where the "MYDOMAIN\GARY$" comes from. The thing is GARY is not with the company and his ID has been dropped from SQL Server, but ODBC code in CGI inherited GARY from Apache, resulting in error 28000.
So the problem was a convolution of
1) 32/64 bit ODBC DSN and
2) default account used by Apache

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

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

发布评论

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

评论(2

少女的英雄梦 2024-10-11 05:51:52

也许 ODBC 数据源列在“用户 DSN”下,而不是“系统 DSN”下。我认为您的 Web 服务器运行的身份只能访问系统 DSN。

如果我的帖子没有意义,我将对其进行编辑,以添加有关用户 DSN 与系统 DSN 之间差异的信息的链接:http://www.truthsolutions.com/sql/odbc/creating_a_new_odbc_dsn.htm

Maybe the ODBC datasource is listed under 'User DSN' and not 'System DSN'. I think that the identity that your web server runs as only has access to System DSN.

In case my post doesn't make sense, I'm editing it in order to add a link to information about the difference between User vs. System DSN's: http://www.truthsolutions.com/sql/odbc/creating_a_new_odbc_dsn.htm

吾家有女初长成 2024-10-11 05:51:52

Windows 7 ODBC 连接出现问题,我也无法弄清楚。我设置了 ODBC,它测试得很好,但出现了与您显示的类似的错误。即使将 MS SQL Server Mgmt Studio 连接到某些 SQL 服务器,我也必须显式地添加额外的连接设置。因为我只在一些 SQL 服务器(我不拥有)上遇到这个问题,所以我认为这是服务器端问题。
在 PERL 上,我最终放弃了,只是使用上述额外的连接设置创建了 DSN 文件并直接指向它。请注意服务器名称是如何出现两次的。

use DBI;
use SQL::Abstract;
my $CONNECT = "FILEDSN=thefile.dsn";
my $dbh = DBI->connect("dbi:ODBC:$CONNECT", "myidsid", "passwordThatsNotUsed");

ODBC 文件看起来像这样:

[ODBC]
DRIVER=SQL Server Native Client 10.0
UID=myidsid
Address=servername,3180
Network=DBMSSOCN
APP=Microsoft® Windows® Operating System
Trusted_Connection=Yes
SERVER=servername
PWD=someTextInPlaceOfPasswordAsItsNotUsed

Something is up with Windows 7 ODBC connections that I can't figure out either. I'd set up the ODBC and it would test fine but get a similar error as you show. Even connecting MS SQL Server Mgmt Studio to some SQL servers I have to explicitly put in extra connection settings. Since I only have the issue on some SQL servers (which I don't own) I think it's a server side problem.
On PERL i finally gave up and just created the DSN file with said extra connection settings and pointed to it directly. Note how the server name is there twice.

use DBI;
use SQL::Abstract;
my $CONNECT = "FILEDSN=thefile.dsn";
my $dbh = DBI->connect("dbi:ODBC:$CONNECT", "myidsid", "passwordThatsNotUsed");

with the ODBC file looking something like:

[ODBC]
DRIVER=SQL Server Native Client 10.0
UID=myidsid
Address=servername,3180
Network=DBMSSOCN
APP=Microsoft® Windows® Operating System
Trusted_Connection=Yes
SERVER=servername
PWD=someTextInPlaceOfPasswordAsItsNotUsed
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文