perl DBI

Discussion in 'Perl Misc' started by sangeetha, Aug 5, 2003.

  1. sangeetha

    sangeetha Guest

    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.

    >>>>>>>>>>>>>> Program <<<<<<<<<<<<<<<<<<<


    #!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.
     
    sangeetha, Aug 5, 2003
    #1
    1. Advertising

  2. sangeetha

    Jim Rendant Guest

    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" <> wrote in message
    news:...
    > 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.
    >
    > >>>>>>>>>>>>>> Program <<<<<<<<<<<<<<<<<<<

    >
    > #!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.
     
    Jim Rendant, Aug 7, 2003
    #2
    1. Advertising

  3. > $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.
     
    James Willmore, Aug 7, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Irene Mettias

    last_insert_id() in perl/DBI

    Irene Mettias, Sep 22, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,613
    Irene Mettias
    Sep 22, 2003
  2. ulloa
    Replies:
    1
    Views:
    527
    Juha Laiho
    Jul 22, 2004
  3. Jerome Hauss
    Replies:
    0
    Views:
    172
    Jerome Hauss
    Oct 13, 2004
  4. Asby

    Mason, DBI, and DBI::Pg

    Asby, Jul 24, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    177
  5. Tim Haynes
    Replies:
    3
    Views:
    143
    Ron Reidy
    Sep 13, 2003
Loading...

Share This Page