Perl DBD cannot connect to MySQL on 64 bit machine

I am in a car RHEL 5.5 64 bit

.

I have installed ActivePerl 5.10 64 bit on the machine, updating the previous embedded Perl 5.8 64 bit. I have MySQL running and my PHP project has access to it. My Perl file needs to access one database using DBD, but it won't be able to. I have confirmed that:

  • My MySQL service is running.
  • My user is present and the database exists along with the data.
  • I can access data from the database through the shell MySQL client.

Below 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 the 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 am getting the following error with a wrong 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 can I solve this? I think the problem is at the end of MySQL.

+3


source to share


2 answers


Use single quotes generally in your database connection string, password string and sql query as they can give you double quotes error. Since for interpolation .

This is 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";
   }

      

+2


source


Here's my guess.

Have you installed the mysql lib connector from mysql correctly?

Have you specified a host?



Try the following:

  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 that doesn't work, try installing the ODBC driver for your server and using it.

0


source







All Articles