5

I am on RHEL 5.5 64 bit machine.

I installed ActivePerl 5.10 64 bit on the machine, upgrading the previous inbuilt Perl 5.8 64 bit. I have MySQL up and running and my PHP project is able to access it. My Perl file needs to access the same database using DBD, but it's not able to do that. I have verified that:

  1. My MySQL service is up and running.
  2. My user is present and the database along with data do exist.
  3. I am able to access the data from the database via the shell MySQL client.

Following is my Perl script.

#!/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";
  }

I am getting the following error with correct user and password :

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.

I get the following error with incorrect user or password:

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.

How do I solve this? I guess the problem is at MySQL's end.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1263746
  • 5,788
  • 4
  • 24
  • 28
  • 1
    can you change `DBI:mysql:go:super218:3306` this to `DBI:mysql:go;host=super218`. Also add `use strict; use warnings;` to your script.+ –  Mar 20 '12 at 08:56
  • @Devendra : can you please elaborate on use strict; and use warnings; how do I use them? – user1263746 Mar 20 '12 at 09:03
  • add after `#!/usr/bin/perl` i.e. on next line.before `use DBI;` refer [link](http://learnperl.scratchcomputing.com/tutorials/getting_started/) for `use strict;` and `use warnings;` –  Mar 20 '12 at 09:39

2 Answers2

2

Use single quotes generally in database connection string, password string and sql query,because these might give you error with double quotes.As double quotes is used for interpolation.

Here's how I think you should write.

#!/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";
   }
  • Tried... issue stays the same. :( I tried the above script on a 32 bit RHEL machine. It works fine there. – user1263746 Mar 20 '12 at 10:47
  • @user1263746- that's some good question or issue raised by you.I hope some more experienced folks might be able to answer.One solution I guess is to use `64 bit DBD::mysql and DBI`. But still I am unaware of it.see if this [link](http://nxadm.wordpress.com/tag/dbdmysql/) helps. –  Mar 20 '12 at 12:36
  • @user1263746 - Is your `MySQL 64 bit`? –  Mar 20 '12 at 13:10
  • 1
    = Yes, my MySQL is 64 bit. I found a workaround and I will post it once I am able to confirm that its working. – user1263746 Mar 21 '12 at 05:11
0

Here is my guess.

Have you installed the mysql connector lib from mysql properly?

Have you specified the host?

Try this out:

  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";

If this is not working, try to install an ODBC driver for your server and use that.

dgw
  • 13,418
  • 11
  • 56
  • 54
user1126070
  • 5,059
  • 1
  • 16
  • 15