WebsiteKnowledge Base

Connecting to MySQL with Perl

Symptom:

Can't connect to mySQL server: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

Resolution:

You must supply a hostname when connectiong to MySQL. The above error appears when attempting to connect to localhost. We have a dedicated MySQL server instead of overloading each individual webserver with their own instance of MySQL.

The following example code shows how to connect using perl:
$database = "yourdatabasename";
$username = "yourusername";
$password = "yourpassword";
$hostname = "mysql-host";
$dbh = DBI->connect("DBI:mysql:$username:$hostname", $username, $password);