2012-03-20 62 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

相關問題