in reply to Re: DBI module not using new mysql.sock
in thread DBI module not using new mysql.sock

Thanks for your reply -
I like the mysql_read_default file option best so I can safely remove the files in /var/lib/mysql, but it is still not working. I get -
DBI connect('SPT:localhost:3306;mysql_read_default_file=/etc/my.cnf','spt',...) failed: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)

My dbi connection code looks like this -

$dsn="DBI:mysql:SPT:localhost:3306;mysql_read_default_file=/etc/my.cnf +"; $dbh = DBI->connect($dsn, $db_user, $db_pass) or die ;

Any ideas ?

Replies are listed 'Best First'.
Re^3: DBI module not using new mysql.sock
by afoken (Chancellor) on Feb 17, 2015 at 22:05 UTC

    In your first post:

    DBI connect('SPIT:localhost:3306','spit',...) failed

    In your last post:

    $dsn="DBI:mysql:SPT:localhost:3306;mysql_read_default_file=/etc/my.cnf";

    The second one lacks an "I", or the first one has an "I" too much.

    The DSN looks at least strange. I don't use MySQL, but I think that ":localhost:3306" should not be there.

    Let be assume you want to connect to a MySQL database named "SPT". According to the documentation, the basic DSN should be "DBI:mysql:SPT" or "DBI:mysql:database=SPT". Now, you can specify how to connect to the database. For TCP/IP, you would add a host and optional a port parameter: "DBI:mysql:database=SPT;host=127.0.0.1;port=3306". Note that the hostname "localhost" is special for MySQL and is not equivalent to 127.0.0.1. Instead, it switches from TCP/IP sockets to unix domain sockets, making the port number meaningless. So, to connect using a unix socket, you would use "DBI:mysql:database=SPT;host=localhost". That would use the default location for the socket file. To override the location, you need to add either a mysql_socket parameter with the location, or specify the configuration file using the mysql_read_default_file parameter. So your DSN will be either "DBI:mysql:database=SPT;host=localhost;mysql_socket=/data/mysql/mysql.sock" or "DBI:mysql:database=SPT;host=localhost;mysql_read_default_file=/etc/my.cnf".

    By the way: Did you try creating a symlink /var/lib/mysql pointing to /data/mysql, as proposed by jmacloue?

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Thanks Alexander,
      In a moment of paranoia I took out the I from my DB name forgetting that I already sent it.
      I will try your DSN, but what I had before always worked - $dsn="DBI:mysql:SPT:localhost:3306";
      I tried setting up the symlink as jmacloue suggested but I don't think I did it correctly. Linux is not my strong point and I've never set up a symlink. Seems like it should be easy but ...

      Thanks for your help !

        I tried setting up the symlink as jmacloue suggested but I don't think I did it correctly. Linux is not my strong point and I've never set up a symlink. Seems like it should be easy but ...
        1. cd /where/the/symlink/should/be
        2. ln -s /target/of/symlink symlink-name

        In your case:

        1. cd /var/lib
        2. ln -s /data/mysql mysql

        After those two commands, you will find a symlink named mysql in the directory /var/lib, pointing to /data/mysql. For more details, read the fine manual: man ln

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      When I tried the mysql_read_default_file approach it still tried to read /var/lib/mysql/mysql.sock, but the mysql_socket approach worked !!!

      Thank you for your outstanding help !

      Robert