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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With