简体   繁体   中英

Access denied error when connecting to remote MySQL server using Perl DBD-mysql

I attempt to connect to a remote MySQL server with Perl DBD-mysql module. The client is under Windows, so before connecting with Perl, I did some tests with Windos cmd line:

mysql -h xx.xx.xx.xx -u root -p database -P 3306

And it returned ERROR 1045 (28000): Access denied for user 'root'@'my-pc-name' . I did some research on the Internet and found running grant all privileges on *.* to root@"%" identified by "password" on the server side could sovle this problem. Then I succeeded in connecting to the server.

However, there's still such access problem when connecting using Perl DBD-mysql module. Here's my code:

#!/usr/bin/perl
use strict;
use warnings;
use DBI;

my $dbh = DBI->connect('DBI:mysql:database@xx.xx.xx.xx', 'root', 'password'
                   ) || die "Could not connect to database: $DBI::errstr";

It reported:

DBI connect('database@xx.xx.xx.xx','root',...) failed: Access denied for user 'root'@'localhost' (using password: YES)

Could you please give some hints?

On the DBI CPAN doc page , I found two alternatives of $data_source for DBI->connect . In contrast of my first attempt, the more detailed one could succeed in connecting to MySQL server. It said:

Examples of $data_source values are:

  dbi:DriverName:database_name
  dbi:DriverName:database_name@hostname:port
  dbi:DriverName:database=database_name;host=hostname;port=port

Although the second one seems not working for me, I turned to the third one, namely

my $dbh = DBI->connect('dbi:mysql:dbname=database;host=xx.xx.xx.xx','root', 'password'
                   ) || die "Could not connect to database: $DBI::errstr";

which worked like a charm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM