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

I replied to a post recently where the OP was unable to connect to a MySQL database because the DBD module was looking at the wrong socket. (Seems to be the default behaviour ;-)) At the time, I gave two solutions: one was to put a mysql_socket parameter in the connect string, the other was to set $ENV{MYSQL_UNIX_PORT}.

I have to report that I am unable to get the first method to work, more times than not - setting %ENV works every time.

As yet, I have been unable to see any rhyme nor reason for when the parameter method works or not. As I'm dealing with 4 machines, each with 2 different Perl versions, I simply don't have time to perform exhaustive tests. I do know that the parameter method is failing on this machine at this time - Perl 5.10.0 with a fresh Bundle::DBD::mysql installed from the CPAN shell.

Whilst this is not an issue for me - because I can use the %ENV method - it would be nice if anyone can throw any light on this, if for no other reason than a documented method not working.

  • Comment on YAMSP (Yet Another MySQL Socket Problem)

Replies are listed 'Best First'.
Re: YAMSP (Yet Another MySQL Socket Problem)
by JavaFan (Canon) on Oct 11, 2008 at 23:36 UTC
    I don't know why it's not working, but I do know that sockets are used by many clients, and that it programs get run on different machines where a different socket is used.

    Therefore, I'd prefer to use a mysql_socket setting to be set in /etc/my.cnf, instead of configuring it for every client.

    I've yet to encounter a problem with setting this in /etc/my.cnf.

      I set both client and server sockets in /etc/my.cnf on all machines in an attempt to create consistency, but this doesn't help DBD one bit.

      To get the MySQL client library to look at /etc/my.cnf, it is possible to provide a mysql_read_default_file parameter in the DSN. From the DBD::mysql documentation:

      ... can be used to read a config file like /etc/my.cnf or ~/.my.cnf. By default MySQL's C client library doesn't use any config files unlike the client programs (mysql, mysqladmin, ...) that do, but outside of the C client library. Thus you need to explicitly request reading a config file ...

      I haven't tried this myself to see if it helps. As the mysql_socket parameter seems to be getting ignored, I'm in no hurry to try this.

      Since I wrote the original post, I have updated DBD::mysql on my system Perl - now that one is looking at the wrong socket too. I have, therefore, been grep'ing through all my code for DBI->connect()s, adding the %ENV fix before them. Hey, could have been worse - I could have done this on one of my production servers ;-)

      My guess is that the problem is somewhere in the MySQL code and that DBD::mysql is just a victim.

Re: YAMSP (Yet Another MySQL Socket Problem)
by chrism01 (Friar) on Oct 13, 2008 at 01:58 UTC
    Well, I've always used this method:
    sub db_connect { my ( $db, # database to connect to $dsn, # data source name ); # Create data source string and connect ... $dsn = "DBI:mysql:". "database=".$cfg::params{'DB_NAME'}.";". "host=".$cfg::params{'DB_HOST'}.";". "port=".$cfg::params{'DB_PORT'}; $cfg::dbh = DBI->connect( $dsn, $cfg::params{'DB_USER'}, $cfg::params{'DB_PASSWD'}, {RaiseError => 0, AutoCommit => 1} ); if( $DBI::errstr) { exit_with_error($DBI::errstr); } }
    where the $cfg::params are read from a text cfg file, and I've never had a prob with it not working, on many systems.

    HTH
    Cheers
    Chris