How to access MySQL database using Perl

by Vivek Gite on September 7, 2006 · 8 comments

Q. How do I access my MySQL database server using Perl programming language?

A. DBI is a generic interface for many databases. That means that you can write a script that works with many different database engines without change. You need a DataBase Driver (DBD) defined for each database type. For MySQL, this driver is called DBD::mysql.

So you can connect Perl to your MySQL database server using the DBI Perl module. Here is small program.

Sample Perl code

Make sure you replace database name, MySQL server hostname, username and password according to your setup.

#!/usr/bin/perl -w
 
use DBI;
 
print "Content-type: text/html\n\n";
 
## mysql user database name
$db ="mysql";
## mysql database user name
$user = "vivek";
 
## mysql database password
$pass = "myPassword";
 
## user hostname : This should be "localhost" but it can be diffrent too
$host="localhost";
 
## SQL query
$query = "show tables";
 
$dbh = DBI->connect("DBI:mysql:$db:$host", $user, $pass);
$sqlQuery  = $dbh->prepare($query)
or die "Can't prepare $query: $dbh->errstr\n";
 
$rv = $sqlQuery->execute
or die "can't execute the query: $sqlQuery->errstr";
 
print "
<h3>********** My Perl DBI Test ***************</h3>
 
";
print "
 
Here is a list of tables in the MySQL database $db.
 
";
while (@row= $sqlQuery->fetchrow_array()) {
my $tables = $row[0];
print "$tables\n<br>";
}
 
$rc = $sqlQuery->finish;
exit(0);

Save and upload above program.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 8 comments… read them below or add one }

1 Ron Lusk September 19, 2008

I’ve looked at a lot of sample code trying to learn how to successfully get extract any information from a database and this has been the first that actually worked! Kudos. Very helpful.

Reply

2 Stone Man March 10, 2009

Hi Nixcraft,

Thanks for this code but i got the following:

- I copy the code and paste it in this file checkdb.sh.
- And run chmod +x checkdb.sh
- Then run ./checkdb.sh

Then the following occured:

Name “main::rv” used only once: possible typo at /usr/bin/check.db line 25.
Name “main::rc” used only once: possible typo at /usr/bin/check.db line 42.
Content-type: text/html

********** My Perl DBI Test ***************

Here is a list of tables in the MySQL database mysql.

columns_priv
db
event
func
general_log
help_category
help_keyword
help_relation
help_topic
host
ndb_binlog_index
plugin
proc
procs_priv
servers
slow_log
tables_priv
time_zone
time_zone_leap_second
time_zone_name
time_zone_transition
time_zone_transition_type
user
[root@drbl-01 ~]#

Currently installed Perl Modules:

[root@drbl-01 DBD-mysql-4.010]# instmodsh
Available commands are:
l – List all installed modules
m – Select a module
q – Quit the program
cmd? l
Installed modules are:
DBD::mysql
DBI
Perl
cmd? q

ANy suggestion on how to fix this?
Thanks again!

Stone

Reply

3 medoix November 20, 2009

Stone

remove $rv = and $rc = they are not needed on those lines.

Reply

4 Anitha December 3, 2009

I have to synchronize bugzilla which uses mysql and HP quality centre which uses mssql .. i’m tryin to do this in perl.. As a fresher i’m not sure wat perl can do .. can anyone suggest me

Reply

5 Lina October 6, 2010

This really does work. After 3 days of searching this is the first script that works.

THANK YOU!!!!!!!

Reply

6 Abby December 1, 2010

Works beautifully! Thank you :)

Reply

7 ahmed September 27, 2011

It’s working very well
thank you.

Reply

8 UncaAlby October 27, 2011

Works like a champ!

My only significant modifications were “use strict” which means adding “my” to declare all the variables.

It would be nice if it also had an example of adding parameters to the prepared statement. I happened to look that up separately, which I’m adding to this comment in case someone else finds it useful.

Example: list every row where the user_id starts with “a”.

$sql = "select user_id from users where user_id like ?";
$sth = $dbh->prepare($sql) or die(DBI->errstr);
$sth->bind_param(1, "a%");
$rv = $sth->execute or die(DBI->errstr);
print "User ID's that start with 'a'\n";
while (my @row = $sth->fetchrow_array()) {
  my $tables = $row[0];
  print "$tables\n";
}
$rc = $sth->finish;

Results (from my table):

User ID’s that start with ‘a’

admin
alameda
arthur

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 4 + 13 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: