【问题标题】:Perl DBD can't connect to MySQL on a 64-bit machinePerl DBD 无法连接到 64 位机器上的 MySQL
【发布时间】:2012-04-04 15:21:18
【问题描述】:

我在RHEL 5.5 64 bit 机器上。

我在机器上安装了ActivePerl5.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";
  }

使用正确的用户名和密码出现以下错误:

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.

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

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.

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

【问题讨论】:

  • 你能把DBI:mysql:go:super218:3306这个改成DBI:mysql:go;host=super218吗?还将use strict; use warnings; 添加到您的脚本中。+
  • @Devendra :请您详细说明使用严格;并使用警告;我该如何使用它们?
  • #!/usr/bin/perl 之后添加,即在下一行。在use DBI; 之前参考link 以获得use strict;use warnings;

标签: mysql database linux perl rhel


【解决方案1】:

这是我的猜测。

您是否正确安装了 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";

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

【讨论】:

  • 如您所见,实际上连接已建立,因为 mysql 能够验证正确的用户...
【解决方案2】:

数据库连接字符串、密码字符串和sql查询中一般使用单引号,因为这些可能会给你双引号错误。因为双引号用于interpolation

我认为你应该这样写。

#!/usr/bin/perl

use strict;
use warnings;

use DBI;
use DBD::mysql;

my $dbh = DBI->connect( 'DBI:mysql:go;host=super218','root','NEWPASSWORD' ,{ RaiseError => 1 } )or die "Couldn't connect to database";
my $sth = $dbh->prepare( 'SELECT * FROM phones');

$sth->execute;

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

【讨论】:

  • 已尝试...问题保持不变。 :( 我在 32 位 RHEL 机器上尝试了上面的脚本。它在那里工作正常。
  • @user1263746-这是您提出的一些好问题或问题。我希望一些更有经验的人能够回答。我想一个解决方案是使用64 bit DBD::mysql and DBI。但我仍然不知道。看看这link 是否有帮助。
  • @user1263746 - 你是MySQL 64 bit吗?
  • = 是的,我的 MySQL 是 64 位的。我找到了一个解决方法,一旦我能够确认它的工作,我会发布它。
猜你喜欢
  • 2012-02-23
  • 2011-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-08
  • 2021-08-04
  • 1970-01-01
  • 2011-05-27
相关资源
最近更新 更多