perl DBI

S

sangeetha

Hi Experts,

I'm new to PERL DBI programming i've writen following perl DBI
program, It seems it is nt able to connect to MySQL.

#!perl

use warnings;
use strict;

use DBI;

my $dbh;

$dbh = DBI->connect('dbi:mysql:test','root','pepsi');

unless ($dbh) {
print "Error opening database: $DBI::errstr\n";
exit ;
}

my $connected = $dbh->ping;

if ($connected and not int($connected)) {
print "ping not implemented by '", $dbh->{driver}->{Name},"'.\n";
} else {
print "Connection is live\n";
}

$dbh->disconnect();

<<<<<<<<<<<<< End Program >>>>>>>>>>>>>>>>>>>>

Output Error message:

DBI connect('test','root',...) failed: Can't connect to local MySQL
server through socket '/tmp/mysql.sock' (2) at db.pl line 10
Error opening database: Can't connect to local MySQL server through
socket '/tmp/mysql.sock' (2)

<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>

Please note i'm able to check my (test) table directly login into
"mysql" by "mysql -u root -p" ... checked "mysqld" is running in
localhost.. MySQL version is "mysql Ver 11.18 Distrib 3.23.56, for
redhat-linux-gnu (i386)".

Tried:
Modified the "/etc/my.cnf" file to point the "socket" file to
"/var/lib/mysql//mysql.sock". for the header of "[client] and
[mysqld]" even now it's not working....

Please point where it goes wrong.

Thanks,
Sangeetha.
 
J

Jim Rendant

Try running mysql as some one other than root.

Check your permissions on the /var/lib/mysql directory. It drove me nuts for
a while but this may be the problem. This directory should be owned by mysql
with the group mysql.


sangeetha said:
Hi Experts,

I'm new to PERL DBI programming i've writen following perl DBI
program, It seems it is nt able to connect to MySQL.

#!perl

use warnings;
use strict;

use DBI;

my $dbh;

$dbh = DBI->connect('dbi:mysql:test','root','pepsi');

unless ($dbh) {
print "Error opening database: $DBI::errstr\n";
exit ;
}

my $connected = $dbh->ping;

if ($connected and not int($connected)) {
print "ping not implemented by '", $dbh->{driver}->{Name},"'.\n";
} else {
print "Connection is live\n";
}

$dbh->disconnect();

<<<<<<<<<<<<< End Program >>>>>>>>>>>>>>>>>>>>

Output Error message:

DBI connect('test','root',...) failed: Can't connect to local MySQL
server through socket '/tmp/mysql.sock' (2) at db.pl line 10
Error opening database: Can't connect to local MySQL server through
socket '/tmp/mysql.sock' (2)

<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>

Please note i'm able to check my (test) table directly login into
"mysql" by "mysql -u root -p" ... checked "mysqld" is running in
localhost.. MySQL version is "mysql Ver 11.18 Distrib 3.23.56, for
redhat-linux-gnu (i386)".

Tried:
Modified the "/etc/my.cnf" file to point the "socket" file to
"/var/lib/mysql//mysql.sock". for the header of "[client] and
[mysqld]" even now it's not working....

Please point where it goes wrong.

Thanks,
Sangeetha.
 
J

James Willmore

$dbh = DBI->connect('dbi:mysql:test','root','pepsi');

You could try:

$dbh =
DBI->connect('dbi:mysql:database=test;host=localhost','root','pepsi');

and see if that works for you (this is a syntax from the DBD::mysql
documentation).

You could also add the following after the connect line:

DBI->(4, "tracefile.txt");

This will create a verbose trace file for your review. It will
describe the "conversation" between your script and the database.
This will help to diagnose where the failure is (ie MySQL
mis-configuration, MySQL not running, etc.).

<<<<<<<<<<<<< End Program >>>>>>>>>>>>>>>>>>>>

Output Error message:

DBI connect('test','root',...) failed: Can't connect to local MySQL
server through socket '/tmp/mysql.sock' (2) at db.pl line 10
Error opening database: Can't connect to local MySQL server through
socket '/tmp/mysql.sock' (2)

Check to see if your MySQL database server is configured to allow
connections from sources other than the mysql client. It is my
understanding that you need to allow connections to MySQL from
something other than the socket it opens - you need to allow
connections to port 3306 (or some variation). You also need to check
the MySQL log file to see if the database is being contacted and
what's it's doing when it is contacted.

I'd check MySQL first, because the error you're getting is more likely
created because of something with the way MySQL is set up versus the
way you wrote the script. The trace file and checking of the logs
will verify or invalidate what I've said.

HTH

Jim

P.S. - ALWAYS remove passwords from posts (I now know your root
password for MySQL is 'pepsi'). I HOPE that you included the password
as an example and it's NOT really the password. Use something like
'foo' or 'bar' - we'll all know that should be an example versus a
real password. And you should NEVER set up 'root' with accounts to
such things as a database unless you have a very valid reason to do
so. root has enough power as is - no sense in giving the root account
any more power without a good reason to do so - use another account
for
superuser or admin fuctions in MySQL.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top