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

Friends,

I don't know how use DBI to connect to a MySQL database that uses the default /var/lib/mysql/mysql.sock file. I can connect with the mysql command line client like so ...

$ mysql -u plankton -p --socket=/somewhere/mysql/mysql.sock
... so I thought I could do the same sort of thing in mysq script ...
#perl -w ... my %attr = ( socket => '/somewhere/mysql/mysql.sock' ); my $dbh = DBI->connect( "dbi:mysql:$db:$server", $user, $pw, \%attr ); ...
The script fails with this output:
DBI connect( 'blah:localhost', 'blah', ... ) failed: Can't connect to +local MySQL server through socket '/var/lib/mysql/mysql.sock' ...
I guess %attr is not the way to go? What should my connect statement look like?

Replies are listed 'Best First'.
Re: How do I specify the MySQL socket file?
by trammell (Priest) on Feb 16, 2005 at 21:08 UTC
    This is documented in DBD::mysql; see option mysql_socket.
Re: How do I specify the MySQL socket file?
by CountZero (Bishop) on Feb 16, 2005 at 21:38 UTC
    This will not answer your questions about sock, but what is wrong with trying to connect to MySQL with:
    $dbconnect='DBI:mysql:database=NameOfDatabase; host=NameOfHost; user=U +ser; password=PassWord'; $databasehandle=DBI->connect($dbconnect);
    In real life you will want to check the return value of the connect-method.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: How do I specify the MySQL socket file?
by Animator (Hermit) on Feb 16, 2005 at 21:28 UTC

    Have you tried without specifying the location of the socket?

    I think that DBD::mysql uses the mysql-client libraries, which should be able to read the mysql configuration file and extract the location of the socket...

Re: How do I specify the MySQL socket file?
by chanakya (Friar) on Feb 17, 2005 at 05:59 UTC
    Do you have a my.cnf file in /etc ..??
    what does the "socket" value in the file say..

    If you do not have a /etc/my.cnf create one with the following values.
    This is the sample configuration on my machine

    [client] socket=/tmp/mysql.sock [mysqld] port=3306 socket=/var/lib/mysql/mysql.sock set-variable = key_buffer_size=16M set-variable = max_allowed_packet=1M [mysqldump] quick Here is typical user option file: [client] # The following password will be sent to all standard MySQL clients #password="password" [mysql] no-auto-rehash set-variable = connect_timeout=2 [mysqlhotcopy] interactive-timeout
    Once you write the file, restart Mysql and try connecting to the Database using
    mysql -u plankton
    If the above command succeeds then you need not worry about connecting to Mysql using Perl

    Hope this helps

    Good Luck!
Re: How do I specify the MySQL socket file?
by Anonymous Monk on Nov 27, 2007 at 16:05 UTC
    Have a look at: http://dev.mysql.com/doc/refman/5.0/en/problems-with-mysql-sock.html there are notes below the documentation that talk about setting the named socket in perl.