in reply to Perl Script connecting to Mysql works from command prompt, not from Apache
To AgentM 15apr2001 0030:#!/usr/bin/perl # Loader.pl stripped down to MySQL connect error (15mar01) use strict; use CGI qw/:standard *table start_ul/; # load standard CGI r +outines use CGI::Pretty; use DBI; use CGI::Carp qw /fatalsToBrowser/; my %INI; my $dbh = undef; print "Content-Type:text/html\r\n\r\n"; print "<HTML>\n"; print "<HEAD>\n"; print "<TITLE>MySQL connect failure test</TITLE>\n"; print "</HEAD>\n"; print "<BODY BGCOLOR=\"white\">\n"; $INI{DBname} = 'Loader'; $INI{DBuser} = 'apache'; $INI{DBpass} = ''; print "opening database |$INI{DBname}|$INI{DBuser}|$INI{DBpass}|\n"; if ( $dbh = DBI->connect("DBI:mysql:$INI{DBname}",$INI{DBuser},$INI{DB +pass}) ) { my $stmt = 'SELECT * FROM ipstat ORDER BY sortip'; if ( my $sth = $dbh->prepare($stmt) ) { if ( my $rows = $sth->execute ) { print "<TABLE>\n"; while ( my $dat = $sth->fetchrow_hashref ) { # output the line print "<TR>"; print "<TD>$dat->{ip}</TD>"; print "</TR>\n"; } # while print "</TABLE>\n"; } else { ERRreport("ERROR: Could not fetch rows from |$stmt| $@",1); } } else { ERRreport("ERROR: Could not prepare statement |$stmt| $@",1); } $dbh->disconnect(); } else { ERRreport("ERROR: Could not connect to |$INI{DBname}| as |$INI{DBus +er}|$INI{DBpass}| err|$@|",1); } print "</BODY>\n"; print "</HTML>\n"; ################################## sub ERRreport { my $errmsg = $_[0]; my $severity = $_[1]; print PAGE "$errmsg<BR>\n"; if ( $severity ) { print "<B>$errmsg</B><BR>\n"; } else { print "$errmsg<BR>\n"; } } #Running the above code from the command line produces the following o +utput # <HTML> # <HEAD> # <TITLE>MySQL connect failure test</TITLE> # </HEAD> # <BODY BGCOLOR="white"> # opening database |Loader|apache|| # <TABLE> # <TR><TD>127.0.0.1</TD></TR> # <TR><TD>128.0.0.1</TD></TR> # </BODY> # </HTML> # Same code called from a form gives the folowing result # <HTML> # <HEAD> # <TITLE>MySQL connect failure test</TITLE> # </HEAD> # <BODY BGCOLOR="white"> # opening database |Loader|apache|| # <B>ERROR: Could not connect to |Loader| as |apache|| err||</B><BR> # </BODY> # </HTML>
the Carp yielded no additional info, but thanks! It will be useful in future code
I'll check the error logs next.
To eejack 15apr01 0507:
Thanks! I created the security hole on purpose out of sheer desperation! I'll fix it later.
mysql.db has the following contents (again, bad security born of desperation):
This should not matter because it does work from the command prompt but not through apache.# host = % Db= test User=(none) select=Y ... # host = % Db= Loader User=apache select=Y ... # host = localhost Db= Loader User=apache select=Y ... # host = 127.0.0.1 Db= Loader User=apache select=Y ...
The info at http://www/mysql.com/doc/P/r/Privilege_system.html is where I got the information in my original request for help. It was Ch 6.9 rather than 6.10 (so my info was older). Reading it gives me the same info. As you can see from both the mysql.user and mysql.db tables, the user apache without password is wide open for database Loader. This is borne out by the fact that the code works from the command prompt.
What would make a connect to a database, that works from the command prompt, fail from the browser? </CODE>
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Perl Script connecting to Mysql works from command prompt, not from Apache
by eejack (Hermit) on Apr 15, 2001 at 18:55 UTC |