Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell DBD::mysql where mysql.sock is?

Tags:

mysql

perl

dbi

Using DBD::mysql with DBI, I am getting the following error when attempting 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 in /tmp.
I know the location of mysql.sock and I currently have it hacked so that it works, I created a soft link to the current location of the mysql.sock file. I would rather not change the MySQL configuration, though this would probably be the easiest thing to do.

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

like image 642
bkoch Avatar asked Dec 24 '09 11:12

bkoch


People also ask

Where is my MySQL sock?

The MySQL server's socket file is named mysqld. sock and on Ubuntu systems it's usually stored in the /var/run/mysqld/ directory. This file is created by the MySQL service automatically.

How do I open MySQL socks?

In the command line, run the following command: mysql -u root -p -S /var/run/mysqld/mysql. sock . Type a password for your root user and press Enter .

How can I tell if MySQL is encrypted?

You can execute this SQL statement from inside the MySqlConnection: SHOW SESSION STATUS LIKE 'Ssl_cipher' , and it will show you whether the connection is encrypted.


2 Answers

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

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

For further information please have a look at the docs here

like image 62
Anand Shah Avatar answered Nov 15 '22 16:11

Anand Shah


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

You don't need to specify host since you're connecting to a socket file.

like image 30
xyz Avatar answered Nov 15 '22 17:11

xyz