Hello wthebutcher,
Welcome to the Monastery. I do not know if you problem is resolved so I would like to add something minor.
I would agree with fellow monk Mr. Muskrat on the connect call but also I would use DBI/RaiseError on your connect call to capture any errors to see at least the possible reason of your error.
From the documentation:
The RaiseError attribute can be used to force errors to raise exceptio
+ns rather than simply return error codes in the normal way.
Sample of code:
#!/usr/bin/perl
use DBI;
use v5.10;
use strict;
use warnings;
use feature 'say';
use Config::Simple;
my %config = ();
my $path = 'conf.ini';
sub mysql_con {
Config::Simple->import_from("".$path."", \%config)
or die Config::Simple->error();
my $dbh = DBI->connect("dbi:mysql::".$config{'MySQL.host'}.":".$co
+nfig{'MySQL.port'}."",
"".$config{'MySQL.user'}."",
"".$config{'MySQL.pass'}."",
{ 'PrintError' => 1, 'RaiseError' => 1 , 'AutoInactiveD
+estroy' => 1 }
) or die "Could not connect to ". $config{'MySQL.host'} .": ". $DB
+I::errstr ."\n";
my $databases = $dbh->do("SHOW DATABASES LIKE '".$config{'MySQL.db
+'}."'")
or die "Error: " .dbh->errstr. "\n";
$dbh->disconnect()
or warn "Error disconnecting: $DBI::errstr\n";
if ($databases eq 1) {
return "Database: ". $config{'MySQL.db'} .
" exists not creating: ". $config{'MySQL.db'} ."";
}
return "Database: ". $config{'MySQL.db'} .
" does not exist create: ". $config{'MySQL.db'} ."";
}
say mysql_con();
I also use the Config::Simple module as I prefer to when I have multiple scripts connecting to multiple databases. Sample of config file (conf.ini) bellow:
[MySQL]
user=user
pass=password
host=localhost
port=3306
db=PerlMonks
Update: Adding ssl certification to connect:
#!/usr/bin/perl
use DBI;
use v5.10;
use strict;
use warnings;
use feature 'say';
use Config::Simple;
my %config = ();
my $path = 'conf.ini';
sub mysql_con {
Config::Simple->import_from("".$path."", \%config)
or die Config::Simple->error();
my $dbh = DBI->connect("dbi:mysql::".$config{'MySQL.host'}.":".$co
+nfig{'MySQL.port'}.",
mysql_ssl=1;
mysql_ssl_client_key=/etc/mysql/certs/client-key.pem;
mysql_ssl_client_cert=/etc/mysql/certs/client-cert.pem;
mysql_ssl_ca_file=/etc/mysql/certs/ca-cert.pem",
$config{'MySQL.user'},
$config{'MySQL.pass'},
{ 'PrintError' => 1, 'RaiseError' => 1 , 'AutoInactiveD
+estroy' => 1 }
) or die "Could not connect to ". $config{'MySQL.host'} .": ". $DB
+I::errstr ."\n";
my $databases = $dbh->do("SHOW DATABASES LIKE '".$config{'MySQL.db
+'}."'")
or die "Error: " .dbh->errstr. "\n";
$dbh->disconnect()
or warn "Error disconnecting: $DBI::errstr\n";
if ($databases eq 1) {
return "Database: ". $config{'MySQL.db'} .
" exists not creating: ". $config{'MySQL.db'} ."";
}
return "Database: ". $config{'MySQL.db'} .
" does not exist create: ". $config{'MySQL.db'} ."";
}
say mysql_con();
__END__
$ perl mysql.pl
Database: PerlMonks exists not creating: PerlMonks
You can also read more about in similar question with examples DBI:mysql connection over SSL fails
Hope this helps, BR.
Seeking for Perl wisdom...on the process of learning...not there...yet!
|