grjash
grjash

Reputation: 196

Why won't this Perl DBI/DBD MySQL (MariaDB) connect to locahost work?

use DBI;

my $dbh = DBI->connect ('DBI:mysql:host=localhost;database=test', 'user', 'password') 
   or die "failed to connect\n";

Results in an error message:

DBI connect('host=localhost;database=test','user',...) failed: Can't connect to MySQL server on 'localhost' (10061) at connect.pl line 3.

using: DBI 1.641, perl v5.26.2 on Windows 10 and running MariaDB 10.2.14

mysqld is running on the computer, and the server can be connected to with the standard "mysql test -u user -p" command

On another PC running Windows 7 with a very similar setup - but with DBI 1.636 - the connect() succeeds with the same perl code. Is is possible that DBI:mysql and Windows 10 aren't compatible?

Upvotes: 0

Views: 1218

Answers (2)

nastaseion
nastaseion

Reputation: 173

It's because you're using the wrong driver name in the connection string. Even though MariaDb it's a fork of MySql, it's not the same

So you need to use:

'DBI:MariaDB:host=...'

Full code would be:

use DBI;

my $dbh = DBI->connect ('DBI:MariaDB:host=localhost;database=test', 'user', 'password') or die "failed to connect\n";

Source: https://metacpan.org/pod/DBD::MariaDB

Upvotes: 0

Daniel PC
Daniel PC

Reputation: 46

It seems you have a space after the word "connect", anyway...try this:

my $driver   = "mysql";
my $database = "DBname";
my $ip       = "localhost";
my $db       = "DBI:$driver:DBNAME:$ip:database=$database";
my $username = "mysqluser";
my $password = "mysqlpass";

my $cn = DBI->connect($db, $username, $password)
    or print "Couldn't connect to database: " . DBI->errstr . "\n\n";

Upvotes: 0

Related Questions