Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl + DBI + REMOTE MySQL Server

Tags:

perl

dbi

I am new writing Perl code and I need a help to figure out why I am not getting to connect from my linux desktop to a remote MySQL server using DBI in a Perl code. I have connection from my desktop to the remote MySQL server, I have identified the remove MySQL server using name or IP address in the DBI connect line, but, base on the error message below, it seems the DBI is trying to connect to a local database (that does not exist), not the remote one:

!/usr/bin/perl -w
use DBI;
use DBD::mysql;
use MySQL;

$DBHOST = "mysqlserver.example.com";
$DBNAME = "anydb";
$DBUSER = "userdb";
$DBPASS = "userdbpwd";
$dsn = "dbi:mysql:database=$DBNAME,host=$DBHOST, port=3306";

my $dbh = DBI->connect("DBI:mysql:database=$DBNAME,host=$DBHOST, port=3306", "$DBUSER", "$DBPASS", \%attr);

Running the code above I am getting the error:

DBI connect failed: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock

I have the following DBI drivers installed:

DBM ExampleP File Gofer Proxy SQLite Sponge mysql

like image 359
Emerson Avatar asked Jan 01 '26 05:01

Emerson


1 Answers

As you can see in the documentation for DBD::mysql's connect() method, DSN parts should be separated by a semicolon (;), not a comma (,):

use DBI;

$dsn = "DBI:mysql:$database";
$dsn = "DBI:mysql:database=$database;host=$hostname";
$dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";

$dbh = DBI->connect($dsn, $user, $password);
like image 181
Matt Jacob Avatar answered Jan 05 '26 00:01

Matt Jacob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!