Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
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 :-)

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 (Chancellor) on Jul 25, 2012 at 12:25 UTC
    Do you set hostname and port in $db?

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?
[choroba]: It seems very slow. But the internet is weird here in Vietnam...
[choroba]: again, stopping it and trying for the second time fixed the problem
[marto]: good morning all

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (13)
As of 2017-02-22 09:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (325 votes). Check out past polls.