我有以下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'的访问(使用密码:是)
我是Perl的新手,我想有些错误了。请告诉我我的代码有什么问题。
提前感谢!
P.S。我在Windows机器上运行此程序,尽管我在其中指定套接字的路径,但即使在我的Ubuntu机器上也遇到相同的错误。
我也遇到过类似的问题。我正在使用下面的连接字符串;
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});
在Mysql中,用户名包括连接主机。
例如,根据连接的位置,同一用户可以具有两个不同的密码。
示例:
root @ localhost是与[email protected]不同的用户
尝试重设密码?在mysql控制台中,运行以下两个命令。(将“明文密码”更改为您的密码)
为'root'@'%'设置密码= PASSWORD('明文密码');
为'root'@'localhost'设置密码= PASSWORD('明文密码');
这将允许两个root用户使用相同的密码