Need help with perl callout of mysql

apps

#1

Hey there,

I am sure I am missing something pretty basic on this, but mysql/perl isn’t my bag (yet). I may have bitten off more than I can chew, but willing to try. So hopefully one of you wise ones will be able to help, while I am off poring through my O’Reilly MySQL & mSQL. When this runs I get that I cannot connect to server – nothing else.

This is a callout that I think is causing my config file problem:

mysql info

$host = ‘mysql.domain.com’;
$user = ‘setupatpanel’;
$passwrd = ‘setupatpanel’;
$database = ‘setupatpanel’;

#------------------------------------------------------------------------------
sub dbi_mysql_connect {
#------------------------------------------------------------------------------

if (!($dbi_dbh = DBI->connect(“DBI:mysql:$database:$host_name”, $user, $passwrd, { RaiseError => 1 }))) {
$mail = "$SENDMAIL ";
open(MAIL, “| $mail”) || &show_msg(“A pipe to sendmail cannot be opened: $!”);
$mailTo=‘name1@domain.com’;
$mailBcc=‘name2@domain.com’;
print MAIL (“To: $mailTo\n”);
print MAIL (“Bcc: $mailBcc\n”);
print MAIL (“From: $mailBcc\n”);
print MAIL (“Subject: CQ SERVER DOWN\n\n”);

close MAIL;
print header();
print “Can’t connect to mysql server: $!”;
exit;
}

}

#------------------------------------------------------------------------------
sub mysql_connect {
#------------------------------------------------------------------------------

if (!($dbh = Mysql->Connect($host, $database, $user, $passwrd)) ) {
print "Content-type: text/plain\n\nCan’t connect to mysql server: ", $Mysql::db_errstr;
}

}


#2
  1. You switched from $host to $host_name.
  2. You mis-spelled $password the first time.

Also, here is a script that I tested myself just now as an example of the technique I use:

[code]database.pl

#!/usr/local/bin/perl

use DBI;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

Use a hash to keep track of all databases you may need to connect to.

my Útabase = (
‘name’ => {
‘name’ => ‘name’,
‘hostname’ => ‘hostname’,
‘username’ => ‘username’,
‘password’ => ‘password’
}
);

the following function accepts a hash reference as the only parameter.

it connects to the database and returns the handle.

sub connect {
my $database = shift;

initialize database handle

my $dbh = DBI->connect(“DBI:mysql:$$database{‘name’}:$$database{‘hostname’}”, $$database{‘username’}, $$database{‘password’},
{
‘PrintError’=> 0,
‘RaiseError’=> 0,
‘AutoCommit’=> 1
}
);

error handling

if (defined $dbh) {
$dbh->{‘Warn’} = 0;
}
return $dbh;
}

main

my $dbh;

if the handle is defined connection was successful

and we do our work and then disconnect from the database.

otherwise we display error message from DBI module.

if ($dbh = &connect($database{‘name’})) {
print CGI->header,
CGI->start_html,
CGI->h2(‘Database connection successful.’),
CGI->end_html;
$dbh->disconnect;
}
else {
die(“Unable to connect to database:\n” . DBI->errstr);
}

exit;
[/code]


#3

Didn’t realize I wasn’t logged in when I posted that code.


#4

Thanks! I lost my cable last night just as I had noticed the password thing. :slight_smile:

I finally got it going. Easier than I thought just some different callouts from the previous server.

deb