AlexPandiyan
AlexPandiyan

Reputation: 4655

How to collecte data from linux server in phpmysql using perl?

I am new to PERL script and I want to collect data from PHP/MySQL in a Linux server, using PERL script.

I have tried some sample PERL code and it works on a local environment.

But I need to get data from another Linux server:

#!/usr/bin/perl
use DBI;

# connect to MySQL...
$driver= "mysql";
$dsn = "DBI:$driver:database=mms;host=localhost";
$dbh = DBI->connect($dsn, "root", "");

# prepare and execute the SQL statement
$sth = $dbh->prepare("SELECT * FROM dailyproductions");
$sth->execute;

# retrieve the results
while(  my $ref = $sth->fetchrow_hashref() ) {
    print $ref->{'id'};
}
exit;

This code works locally (localhost), but when I switch to a different host:

$dsn = "DBI:$driver:database=mms;host=192.168.0.1";

I get this error:

DBI connect('database=mms;host=192.168.0.1,'root',...) failed: Host 'admin-

lap' is not allowed to connect to this MySQL server at D:\Confidential\Report\mysql.pl line 7 Can't call method "prepare" on an undefined value at D:\Confidential\Report\mysql.pl line 10.

How can I overcome this?

Upvotes: 1

Views: 81

Answers (1)

Dave Cross
Dave Cross

Reputation: 69284

You're not using "phpmysql", you're using MySQL. Try not to get the two confused.

Your program works. You've established that. The problem is with the MySQL connectivity between the machine where you're running the program and the machine with the MySQL database server. That's what your error message is saying.

Host 'admin-lap' is not allowed to connect to this MySQL server

You will need to get the MySQL configuration changed to allow your host to connect to the server. Your database administrator will be able to help with that.

One fix I would make to your program. You see the second error, about calling prepare on and undefined value? That's because your program assumes that it can always connect to the database. You should change that so that the program dies if it can't make the connection.

$dbh = DBI->connect($dsn, "root", "")
  or die $DBI::errstr;

Upvotes: 2

Related Questions