Mysql DBI connection

software development


since i was unable to find the correct resolution to my DBI connection issue via this forum, now that it’s been resolved, i will post the solution to perhaps save someone similar problems in the future.

I encountered the following error while trying to use DBI to establish a database connection with mysql in a perl CGI script.

DBI connect(‘;’,‘mydb_user’,…) failed: Can’t connect
to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)
at ./perltest.cgi line 22

While I tried several slight variations, a successful connection was established this way:

my $user = “your_username_here”;
my $pass = “your_password_here”;
my $dataSource = “DBI:mysql:database=your_dbname_here;”;

eg: “DBI:mysql:database=mydb;”;

my $dbh = DBI->connect($dataSource, $user, $pass,
{ RaiseError => 1, AutoCommit => 0 }
) or die $DBI::errstr;


And for future reference, the documentation for the DBI mysql driver is located here:

DBD::mysql documentation

:cool: Perl / MySQL / HTML CSS


Thanks for your posting, your recipe works!

FYI: The perl version on our current machine is 5.8.4.

Talk to you soon,