http://www.perlmonks.org?node_id=983641

liverpaul has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm having trouble connecting my local Ubuntu 12.04 laptop to my remote MySQL database on my Ubuntu Server 10.04. The Perl code I am using is as follows:

#!/usr/bin/perl use strict; use warnings; use DBI; my $username='username'; my $pass='password'; my $db='database_name'; my $dbh = DBI->connect( "dbi:mysql:$db", $username, $pass, { 'PrintErr +or' => 1, 'RaiseError' => 1 } );

I have Wordpress installed on the server and it works - I can connect to the database. However, when I fill in the same details in my Perl script I get the following error:

DBI connect('database_name','username',...) failed: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) at .....

My server is VPS hosting by the way.

Any ideas? I'm a beginner, so keep it as simple as possible :-)

Replies are listed 'Best First'.
Re: Connect to MySQL database (on Ubuntu Server) using Perl
by moritz (Cardinal) on Jul 25, 2012 at 12:23 UTC
      Hi,

      Thanks for the reply. I've changed my code to this:

      #!/usr/bin/perl use strict; use warnings; use DBI; my $username='username'; my $pass='password'; my $db='database_name'; my $host='hostname'; my $dbh = DBI->connect( "dbi:mysql:database=$db:$host", $username, $pa +ss, { 'PrintError' => 1, 'RaiseError' => 1 } );

      I'm still getting an error: Unknown MySQL server host 'hostname' (2) at ...

Re: Connect to MySQL database (on Ubuntu Server) using Perl
by Anonymous Monk on Jul 25, 2012 at 12:33 UTC
      I am looking through these links now - hopefully one of them will help. Thanks.

        UPDATE:

        I found a solution to the problem here:

        http://www.ghacks.net/2009/12/27/allow-remote-connections-to-your-mysql-server/

        Thanks again for the replies :-)

Re: Connect to MySQL database (on Ubuntu Server) using Perl
by choroba (Cardinal) on Jul 25, 2012 at 12:25 UTC
    Do you set hostname and port in $db?