0
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?

Gerhard
  • 22,678
  • 7
  • 27
  • 43
grjash
  • 176
  • 2
  • 8
  • Try to add `... 'user', 'password', {RaiseError=>1})` – Michael May 06 '18 at 19:22
  • @Michael: That has no effect on the behaviour of the `connect` call itself; only on subsequent calls on the resultant database handle (when the `connect` has worked). – Borodin May 06 '18 at 20:20
  • Are you able to manually connect to specifically to `localhost` running `MySQL-client` on this device? – Gerhard May 07 '18 at 08:01
  • As stated in the question, the connection with the mysql command succeeds. Adding '- h localhost' has no practical effect, as that is the default host for mysql. – grjash May 07 '18 at 18:51
  • still using DBI 1.641 (current verison), and updating DBD:mysql did not help – grjash Aug 09 '18 at 17:24

2 Answers2

0

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";
Daniel PC
  • 46
  • 4
  • Perl is generally free form. Any whitespace between connect and the opening parenthesis is irrelevant. The code block fails, but it's no surprise - for one thing, it has "DBNAME" as a literal, but also "DBname" as the value of $database in the $db string.. – grjash May 07 '18 at 18:37
0

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

nastaseion
  • 173
  • 1
  • 2
  • 9