2012-03-20 120 views
3

我在RHEL 5.5 64 bit机器上。Perl DBD无法连接到64位机器上的MySQL

我安装了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"; 
    } 

我正在用正确的用户名和密码以下错误:

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的最后。

+1

你可以更改'DBI:mysql:go:super218:3306'这个'DBI:mysql:go; host = super218'。另外加上'use strict;使用警告;'给你的脚本+ – 2012-03-20 08:56:59

+0

@Devendra:请你详细说明使用严格;并使用警告;我如何使用它们? #'/ usr/bin/perl'后面加' – user1263746 2012-03-20 09:03:37

+0

'即在'use DBI'的下一行;''use strict;'参考[link](http://learnperl.scratchcomputing.com/tutorials/getting_started/)并使用警告; – 2012-03-20 09:39:12

回答

0

这是我的猜测。

你是否正确安装了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驱动程序并使用它。

+0

正如你所看到的,连接实际上是建立的,因为mysql能够验证一个正确的用户...... – user1263746 2012-03-20 10:50:18

2

使用单引号通常在数据库连接字符串,密码字符串和SQL查询,因为这可能会给你用双quotes.As双引号错误用于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"; 
    } 
+0

试过......问题保持不变。 :( 我在32位RHEL机器上试过上面的脚本,它在那里工作正常 – user1263746 2012-03-20 10:47:03

+0

@ user1263746-这是你提出的一些很好的问题或问题。我希望一些更有经验的人可能能够回答。猜测是使用64位DBD :: mysql和DBI,但我仍然不知道它。看看这个[链接](http://nxadm.wordpress.com/tag/dbdmysql/)是否有帮助。 – 2012-03-20 12:36:51

+0

@ user1263746 - 你的MySQL 64位? – 2012-03-20 13:10:06

相关问题