【问题标题】:Perl - DBI cannot connect to MySQL (Access Denied...)Perl - DBI 无法连接到 MySQL(拒绝访问...)
【发布时间】:2019-10-10 14:07:51
【问题描述】:

我有以下 Perl 脚本,我尝试通过它连接到我的本地 MySQL 服务器,

#!/usr/bin/perl

use DBI;
use strict;

my $driver = "mysql"; 
my $host = "localhost";
my $database = "test";
my $dsn = "DBI:$driver:database=$database,host=$host";
my $userid = "root";
my $password = "password";

my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;

my $sth = $dbh->prepare("select from_date,to_date from temp");
$sth->execute() or die $DBI::errstr;
print "Number of rows found :" + $sth->rows;
while (my @row = $sth->fetchrow_array()) {
   my ($from_date, $to_date ) = @row;
   print "From Date = $from_date, To Date = $to_date\n";
}
$sth->finish();

当我运行这个脚本时,虽然我给了这个 MySQL 用户所有的权限,但我每次都会收到以下错误,

拒绝用户'root'@'localhost'访问(使用密码:YES)

我是 Perl 的新手,我猜我犯了一些错误。请告诉我我的代码有什么问题。

提前致谢!!

附注我在 Windows 机器上运行它,即使在我的 Ubuntu 机器上我也会遇到同样的错误,尽管我在那里指定了套接字的路径。

【问题讨论】:

  • 您的密码好像错误。代码看起来没问题。
  • 没有..密码正确...
  • 你可以从控制台登录吗?使用相同的用途/密码?
  • 试试:$dsn = "dbi:mysql:$database:localhost:3306";
  • my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr; 更改为 my $dbh = DBI->connect($dsn, $userid, $password, {RaiseError=>1} ) or die $DBI::errstr; 并查看是否收到任何详细的错误消息

标签: mysql perl dbi


【解决方案1】:

我也遇到过类似的问题。 我使用的是下面的连接字符串;

my $dbh = DBI->connect("DBI:mysql:database=db_name;host=db_server_ip", "db_user", "db_password", {'RaiseError' => 1});

密码中包含“@”符号,将双引号 (") 替换为单引号 (') 为我解决了问题。

my $dbh = DBI->connect("DBI:mysql:database=db_name;host=db_server_ip", "db_user", 'db_password', {'RaiseError' => 1});

【讨论】:

    【解决方案2】:

    在 Mysql 中,用户名包括连接的主机。

    例如,同一用户可以有两个不同的密码,具体取决于他们连接的位置。

    例子:

    root@localhost 是不同于 root@192.168.1.1 的用户

    尝试重置您的密码? 在 mysql 控制台中运行以下两个命令。 (将“明文密码”更改为您的密码)

    SET PASSWORD FOR 'root'@'%' = PASSWORD('明文密码');

    SET PASSWORD FOR 'root'@'localhost' = PASSWORD('明文密码');

    这将允许两个 root 用户使用相同的密码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-11
      • 2021-03-16
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      • 1970-01-01
      • 2015-06-21
      相关资源
      最近更新 更多