Perl DBD无法在64位机器上连接MySQL

use*_*746 5 mysql database linux perl rhel

我在RHEL 5.5 64 bit机器上。

我在机器上安装了ActivePerl 5.10 64位,升级了之前内置的Perl 5.8 64位。我已经启动并运行了 MySQL,并且我的 PHP 项目能够访问它。我的 Perl 文件需要使用 DBD 访问同一数据库,但它无法做到这一点。我已经核实:

  1. 我的 MySQL 服务已启动并正在运行。
  2. 我的用户存在,并且数据库和数据确实存在。
  3. 我可以通过 shell MySQL 客户端访问数据库中的数据。

以下是我的 Perl 脚本。

#!/usr/bin/perl

use DBI;


$dbh = DBI->connect( "DBI:mysql:go:super218:3306","root","NEWPASSWORD" ) or die "Couldn't connect to database: " . DBI->errstr;

my $sth = $dbh->prepare( "SELECT * FROM phones" )
      or die "Can't prepare SQL statement: $DBI::errstr\n";

$sth->execute or die "executing: $stmtA ", $dbh->errstr;

my @row;
while ( @row = $sth->fetchrow_array( ) ) {
      print "Row: @row\n";
  }
Run Code Online (Sandbox Code Playgroud)

使用正确的用户名和密码时,我收到以下错误:

DBI connect('go:super218:3306','root',...) failed: (no error string) at testdb.pl line 6
Couldn't connect to database:  at testdb.pl line 6.
Run Code Online (Sandbox Code Playgroud)

我收到以下错误,用户名或密码不正确:

DBI connect('go:super218:3306','root1',...) failed: Access denied for user 'root1'@'localhost' (using password: YES) at testdb.pl line 6
Couldn't connect to database: Access denied for user 'root1'@'localhost' (using password: YES) at testdb.pl line 6.
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?我猜问题出在MySQL的末端。

小智 0

这是我的猜测。

你是否正确安装了mysql的mysql连接器库?

你指定主机了吗?

试试这个:

  my $db        = 'database_name';
  my $srv       = 'localhost';
  my $user      = '***************';
  my $pass      = '***************';
  my $port      = '3306';
  my $dbh = DBI->connect("DBI:mysql:$db:$srv", $user, $pass, {'RaiseError' => 1, 'PrintError' => 1, 'AutoCommit' => 1 }) or die "Connection Failed: $db DB on $srv\n\t$DBI::errstr\n";
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,请尝试为您的服务器安装 ODBC 驱动程序并使用它。