Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I followed one set of instructions for writing a handler for use with mod_perl. Here is what I added to the end of apache2.conf (on Ubuntu 12.04):

PerlRequire /home/ted/modperlhandler/Hello.pm <Location /time> SetHandler perl-script PerlResponseHandler Hello </Location>

And here is the handler package code:

package Hello use strict; use Apache2::RequestRec (); use Apache2::RequestIO (); use Apache2::Request; use DBI; use Apache2::Const -compile => qw(OK); sub handler { my $db = 'nutrientsdb'; my $hostname = '192.168.2.24'; my $user = 'rejbyers'; my $dbpwd = 'xxxxxxxxxxx'; my $dbh = DBI->connect("DBI:mysql:database=$db;host=$hostname",$user +,$dbpwd,{RaiseError => 1}) or die "Failed to connect to the DB.\n"; my $r = shift; my $req = Apache2::Request->new($r); my $table = $req->param; my %p = %$table; my $name = $p{'name'}; if ((defined $name) && (length($name) > 0)) { print "Hello $p{'name'}, the time is " . localtime() . "\n\n\n"; foreach my $k (keys %p) { print "$k <=> $p{$k}\n"; } } else { print "Hello World, the time is " . localtime() . "\n"; } return Apache2::Const::OK; } 1;

Obviously, the example I followed did not involve the use of DBI. If the DBI code is commented out, the Hello handler works as the example implied (though I successfully extended the example to include getting the request parameters).

I know the above example does not (yet) do anything with $dbh. I first wanted to see if I could successfully connect to the DB before running any queries. Alas, I could not take this further because Apache gives me a 500 error. On examining the logs, I see the following entry (after a timestamp)): "DBI connect ('database==192.168.2.24','rejbyers',...) failed: Access denied for user 'rejbyers'@'localhost' (using password: YES) at /home/ted/modperlhandler/Hello.pm line 22."

Now, to verify that the database server is in fact reachable, I wrote the following CGI script and placed it in /usr/lib/cgi-bin (on Ubuntu):

#!perl use CGI; use CGI::Carp qw(fatalsToBrowser); use DBI; use CGI::Carp::DebugScreen ( engine => 'TT', debug => 1, lines => 5, modules => 1, environment => 1, raw_error => 1, overload => 1, ); my $query = new CGI; print $query->header; my $db = 'nutrientsdb'; my $hostname = '192.168.2.24'; my $user = 'rejbyers'; my $dbpwd = 'xxxxxxxxxxx'; my $dbh = DBI->connect("DBI:mysql:database=$db;host=$hostname",$user,$ +dbpwd,{RaiseError => 1}) or die "Failed to connect to the DB.\n"; my $title = 'My CGI Script'; &print_html_header($title); &print_body; &print_end; exit(0); sub print_html_header { print $query->start_html(shift); } sub print_end { print $query->end_html; } sub print_body { my $sql = 'SELECT * FROM FD_GROUP'; my $sth = $dbh->prepare($sql); $sth->execute; print '<table border>'; while (my $aref = $sth->fetchrow_arrayref) { my ($code,$desc) = @$aref; print "<tr><td>$code</td><td>$desc</td></tr>"; } print '</table>'; $sth->finish; }

This CGI script worked as expected. Hence, the puzzle. On the same Ubuntu (virtual) machine, my mod_perl handler fails to connect to the database server, but my cgi script, using the same credentials, does connect to the database server. Why?

As an aside, I have, in addition to mod_perl, fastcgi enabled, and Apache::DBI is installed (and, unless I misunderstood the configuration instructions, apache's httpd server is configured to use it). My worry is that all the instructional materials for using Apache::DBI and fastcgi that I have found are quite old. While I am still studying effective use of mod-perl, do fastcgi and Apache::DBI remain good options? If so, how do they fit with what I have been doing with CGI and what I am trying to learn for using mod_perl?

Thanks

Ted


In reply to puzzling problem with access to DB when using mod_perl by ted.byers

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (6)
    As of 2015-07-05 06:01 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (60 votes), past polls