How to tell DBD :: mysql where mysql.sock is?

Using DBD :: mysql with DBI, I get the following error when trying to connect to the database.

DBI connect('database=mydb:host=localhost','someuser',...) failed: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) at ./myscript.pl line 97 

Yes, MySQL is up and running. The problem is that mysql.sock is not located in / tmp.
I know the location of mysql.sock, and currently I hacked it, so that it works, I created a soft link to the current location of the mysql.sock file. I would prefer not to change the configuration of MySQL, although this would probably be the easiest task.

Is there a way to go into DBD :: mysql and configure it to look for mysql.sock in the right place?

+7
mysql perl dbi
source share
2 answers

You can specify the location of the socket in the connection method

 my $dbh = DBI->connect("DBI:mysql:database=dbname;host=localhost;mysql_socket=/path/to/mysql.sock","someuser","somepassword", {'RaiseError' => 1}); 

For more information, please check out the docs here.

+13
source share
 $dbh=DBI->connect("DBI:mysql:database=dbname;mysql_socket=/var/lib/mysql/mysql.sock;user=username;password=password"); 

You do not need to specify a host, since you are connecting to a socket file.

+3
source share

All Articles