Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks
I noticed that when I tried to connect to a mysql server it was using the wrong socket,
it returned an error like
DBI connect('mysql:localhost','root',...) failed: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) at ./db_init.pl line 10
I looked inside /etc/mysql/my.cnf and indead there was a different socket used.
So my question is: how come that perl doesn't know this, and how do I tell perl to use this other socket?

Luca
  • Comment on perl uses wrong socket to connect to mysql

Replies are listed 'Best First'.
Re: perl uses wrong socket to connect to mysql
by Joost (Canon) on Apr 16, 2005 at 20:13 UTC
    Perl doesn't know this because my.conf can be in a different place, you might not have a local mysql server, you might not want to connect to the local mysql, or you might have more than one local mysql deamon.

    In other words; you need to supply the connection info yourself, because it's no use guessing.

    update: the DBD::mysql manual states:

        $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";
        $dbh = DBI->connect($dsn, $user, $password);
    
      ok, so always connect to a host and port and not via the deafult perl socket

      Thanks Luca
Re: perl uses wrong socket to connect to mysql
by tantarbobus (Hermit) on Apr 17, 2005 at 17:06 UTC

    You can appened "mysql_read_default_group=$somegroup" to your connect string, and then DBD::mysql should read your my.conf.

    my.conf is not being read because the mysql client libraries that DBD::mysql uses don't seem to want to read the my.conf file unless read_default_group is specified. A future version of DBD::mysql will default to "mysql_read_default_group=dbd_mysql" if no other group is specified.

Re: perl uses wrong socket to connect to mysql
by menolly (Hermit) on Apr 19, 2005 at 01:47 UTC
    Are you sure your mysql server is up and running without a problem? I've also seen that message when the service is hung/crashed/stopped/etc.