MySQL is not connecting using AGI


#1

I’m trying to connect MySQL in my AGI i.e. in perl, but unable to connect with my databases. The connection parameters are listed below;

use DBI;
#use Asterisk::AGI;
print “Content-type:text/html\n\n”;

Connect To Database

$database = ‘tap_cdr’;
$username = ‘tapuser’;
$password = ‘tapuser’;
$hostname = ‘localhost’;

$dbh = DBI->connect(“DBI:mysql:database=$database; host=$hostname; user=$username; password=$password”)
or die “Connecting from Perl to MySQL database failed: $DBI::errstr”;

$sSQL = “SELECT context, channel FROM cdr”;
$st = $dbh->prepare($sSQL) or die “Preparing MySQL query failed: $DBI::errstr”;

$st->execute() or die “The execution of the MySQL query failed: $DBI::errstr”;

while ($row = $st->fetchrow_hashref())
{
print " $row->{context} $row->{channel}";
#$agi -> verbose(“Context Value: $row->{context}\n”,1);
#$agi -> verbose(“Context Value: $row->{channel}\n”,1);
}

$dbh ->disconnect();

When I tried to run that script manually I’m getting Error message;

DBI connect(‘database=tap_cdr; host=localhost; user=tapuser; password=tapuser’,’’,…) failed: Access denied for user ‘root’@‘localhost’ (using password: NO) at ./test.pl line 10
Connecting from Perl to MySQL database failed: Access denied for user ‘root’@‘localhost’ (using password: NO) at ./test.pl line 10.

Please advice at earliest. Am I missing some parameters? Even I do installed DBI using CPAN and do have perl-DBD-MySQL package installed on my server.

Further added, I’m running as a root and also tried root credentials but getting same message. On other hand I can connect to MySQL via root and non-root on shell.


#2

Hi,

You need to give the privileges to your root user and IP address of your machine to access the MySQL db. Give the permission and then try it will work.

Check the MySQL privileges on MySQL web site.

Regards,

Ketan


#3

I think it has something to do with the spaces after the semicolons - try removing those.


#4

Thanks SGM, it worked.