Hi,


I have this program written in Perl and to be tested on Linux-Fedora.

#!/usr/bin/perl
use Mysql;


$host = "localhost";
$database = "DVDRentals";
$tablename = "Orders";
$user = "root";
$pw = "password";


# PERL MYSQL CONNECT()
$connect = Mysql->connect($host, $database, $user, $pw);


# SELECT DB
$connect->selectdb($database);


# DEFINE A MySQL QUERY
$myquery = "SELECT * FROM $tablename";


# EXECUTE THE QUERY FUNCTION
$execute = $connect->query($myquery);


# HTML TABLE
print "<table border='1'><tr>
<th>OrderID</th>
<th>CustID</th>
<th>EmpID</th></tr>";


# FETCHROW ARRAY


while (@results = $execute->fetchrow()) {
print "<tr><td>"
.$results[0]."</td><td>"
.$results[1]."</td><td>"
.$results[2]."</td></tr>";
}


print "</table>";
When I run the program at the command-line with the command perl mysqlquery.pl, here is what comes up as a result of it.
[root@localhost PracPerl]# perl mysqlquery.pl
Mysql connect('database=DVDRentals;host=localhost','root',...) failed: Access denied for user 'root'@'localhost' (using password: YES) at mysqlquery.pl line 12
Can't call method "selectdb" on an undefined value at mysqlquery.pl line 15.

I am the root user of the system and that is why I have put root as user in the program and kept the password field empty once and then run the program so see if it works but it does not. And even with the password as password it does not. So I tried to check all the installed packages that relate to Perl on the system as follows:

[root@localhost PracPerl]# rpm -qa | grep perl
perl-URI-1.35-2.2
mod_perl-2.0.2-5.1
perl-Net-DNS-0.55-1.1.2
perl-Digest-HMAC-1.01-14.2
perl-DBD-MySQL-3.0002-2.2.2
perl-libwww-perl-5.805-1.1
perl-XML-RegExp-0.03-2.fc5
perl-Digest-SHA1-2.11-1.2
perl-String-CRC32-1.3-3.FC5.2
perl-HTML-Parser-3.50-1
newt-perl-1.08-9.2.1
perl-DBD-Pg-1.43-2.2.2
perl-5.8.8-5
perl-DateManip-5.44-1.2
perl-DBI-1.50-2.2
perl-Compress-Zlib-1.41-1.2.2
perl-HTML-Tagset-3.10-2.1
perl-XML-XQL-0.68-2.fc5
perl-XML-Parser-2.34-6.1.2.2
perl-Parse-Yapp-1.05-35.fc5
perl-BSD-Resource-1.24-3.2.2
perl-Net-IP-1.24-2.2
perl-SGMLSpm-1.03ii-16.2
perl-XML-DOM-1.44-2.fc5

I just assume that here this package perl-DBD-MySQL-3.0002-2.2.2, is the one is required to make Perl talk to MySQL. Am I right here? How could I solve the problem here of connecting Perl and MySQL? Please guide me as I am a newbie. Thanks.

Dani AI

Generated

The error shown is a MySQL authentication failure (not a Perl runtime bug). As suggested, first confirm the MySQL credentials and grants for the MySQL account: OS root is not the same as MySQL root, and a blank or different password at the OS prompt does not mean the same for MySQL. From the server shell, try a direct login to verify the account and password.

mysql -u root -p

If that fails, inspect the user table and grants from a session that can connect:

SELECT user, host, plugin FROM mysql.user;
SHOW GRANTS FOR 'root'@'localhost';

Common causes: wrong password, no GRANT for 'root'@'localhost', host/authentication-plugin differences (some installs use auth_socket or unix_socket), or confusion between socket vs TCP. Note that many Perl MySQL drivers treat localhost as a socket connection; using 127.0.0.1 forces TCP and can change authentication behavior.

Prefer DBI/DBD::mysql over the older Mysql.pm. Example connection pattern (replace credentials and do not hardcode root in production):

use DBI;
my $dsn = "DBI:mysql:database=DVDRentals;host=127.0.0.1";
my $dbh = DBI->connect($dsn, 'appuser', 'secret', { RaiseError => 1, PrintError => 0, mysql_enable_utf8 => 1 });

Create a dedicated MySQL user with limited privileges rather than using root:

GRANT SELECT ON DVDRentals.* TO 'dvdruser'@'localhost' IDENTIFIED BY 'strongpass';
FLUSH PRIVILEGES;

Troubleshooting checklist: 1) Verify CLI login works; 2) check mysql.user plugin and SHOW GRANTS; 3) try 127.0.0.1 vs localhost in the DSN; 4) switch to DBI/DBD::mysql and enable RaiseError to get clearer diagnostics; 5) avoid running application code as MySQL root and never hardcode production credentials. If root access to MySQL is lost, follow the official MySQL password-reset procedure rather than ad hoc methods.

Have you checked for permissions in MySQL for user 'root'? It is necessary to 'grant' each user permissions.

As for DB access I am not familiar with the 'Mysql' module. I have used the DBI module for years will very few problems. You have both DBI and DBD-MySQL modules loaded.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.