Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Connect to MySQL database (on Ubuntu Server) using Perl

by liverpaul (Novice)
on Jul 25, 2012 at 12:16 UTC ( #983641=perlquestion: print w/ replies, xml ) Need Help??
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 :-)

Comment on Connect to MySQL database (on Ubuntu Server) using Perl
Select or Download Code
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 choroba (Abbot) on Jul 25, 2012 at 12:25 UTC
    Do you set hostname and port in $db?
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 :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://983641]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2014-11-28 16:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (198 votes), past polls