[cached copy at OSCR. Original at http://www.lynxafrica.demon.co.uk/dbi]

How to install the Perl module DBI on Linux

By Phil Jones, July 00

Index

  1. About this document
  2. Explanation of DBI and DBD
  3. What this article doesn't do
  4. Please correct my errors
  5. Assumptions
  6. Starting point
  7. Overview
  8. Checking for prerequisites
  9. Explanation of the packages the shell script tests for
  10. Installing prerequisites
  11. Creating data in MySQL
  12. Obtaining DBI and DBD
  13. Install DBI
  14. Install DBD
  15. Testing
  16. Feedback

About this document

The purpose of this document is to help with setting up Linux in order to write a Perl program that gets data from MySQL. Hopefully it will relieve some of the traffic on the DBI mailing list. These instructions cover installing, testing and hopefully understanding more about the Perl module DBI, and its DBD for MySQL. This document is intended for those that wish to use RPM packages as much as possible.

Explanation of DBI and DBD

DBI stands for Data Base Interface. DBI is an add-on, or 'module' for Perl which gives Perl programs access to databases. Mysql-DBD is another module, a Data Base Driver for MySQL. DBI uses the Driver as a translator to talk to MySQL. So, both modules are required.

What this article doesn't do

This article is about making a working system in the easiest (ie, least secure) way possible. Use these instructions on a testing or learning only computer that isn't connected to the Internet.

Please correct my errors

I am no Linux, Unix, DBI or MySQL guru. These instructions are Red Hat oriented simply because that is the Linux I am using. Although this procedure has worked for me, I can't guarantee it will work on another platform. Also I'm not all that familiar with compiling Perl modules and programs in general. If you know better, and you see something wrong with this document, please let me know so I can fix it.

Assumptions

I assume you are using a recent Red Hat Linux distribution, or a distribution based on Red Hat which uses the Red Hat Package Manager (RPM) for installing and verifying programs. You must have root access to the system.

Starting point

I used a Red Hat Linux 6.0 Intel computer with Apache, Perl and development tools installed. The way I set this up was to select the Apache server, development tools and development libraries when installing Red Hat.

Overview

The outline of the procedure is:
  1. Check required packages are installed
  2. Install required packages that are not installed
  3. Obtain Perl DBI and DBD for MySQL
  4. Compile and install Perl DBI and DBD for MySQL
  5. Create a sample database in MySQL
  6. Create a sample table in that database
  7. Create some sample data in that table
  8. Create a MySQL user with access to the sample data
  9. Write a simple Perl program that prints data from MySQL.

Checking for prerequisites

Here are the basic requirements, as far as I know, for installing Perl DBI and the DBD for MySQL. This is not an exhaustive list. There may be other required packages which I don't know about.
  1. Perl programming language
  2. Make
  3. Kernel headers
  4. A C compiler, eg EGCS
  5. C++ extensions for the C compiler
  6. Glibc library
  7. Glibc development library
  8. MySQL server
  9. MySQL client
  10. MySQL development tools
For your convenience I have written a shell script which tests your system to see if you have these packages installed in RPM format. It only checks to see if the package exists. It doesn't verify the packages are completely installed, you can do that if you wish. Run this script, like this:
sh checkdbi.sh
If all is well, you should get output like this:
 If there are no errors in the following list, you should
 have everything you need to write a Perl program that gets
 data from MySQL.
 Checking to see which RPM packages are installed...
 perl-5.00503-2
 make-3.77-6
 kernel-headers-2.2.5-15
 egcs-1.1.2-12
 egcs-c++-1.1.2-12
 glibc-2.1.1-6
 glibc-devel-2.1.1-6
 MySQL-3.22.32-1
 MySQL-client-3.22.32-1
 MySQL-devel-3.22.32-1

The version numbers may vary according to which Linux you are using. If you see any '...is not installed' messages in the output, you may need to install that package. Read the next section to learn more about what you may be missing.

Explanation of the packages the shell script tests for

  1. Perl is definitely required.
  2. Make is a standard program used to simplify compiling new programs. DBI and DBD both use Make as part of the compiling process. You must have this.
  3. Kernel headers are structures and constants needed for compiling most standard programs. You very likely need this.
  4. EGCS is the C compiler supplied with Red Hat Linux 6.0. The compiler turns human-readable code into an executable program. If you don't have the EGCS package installed, perhaps you have another C compiler installed instead. You don't have to have EGCS, but you do need a C compiler.
  5. EGCS-C++ adds C++ support to the GNU C compiler. I don't know whether C++ extensions are required but it seems like a good idea to have it anyway. If you are using another C compiler, this package may be different also.
  6. Glibc contains standard libraries used by multiple programs on the system. This is one of the base packages in Linux so it is very likely to be installed on your system. Required.
  7. Glibc-devel contains header files and object files needed when compiling programs that use the standard C libraries. Required.
  8. MySQL is the database server. Required.
  9. MySQL-client provides a simple, command line interface to MySQL. This is needed so we can type some data into the database, and to see whether it's been entered correctly. Required.
  10. MySQL-devel makes it possible for other programs, such as DBI, to work with MySQL. Required.

Installing prerequisites

Many up to date Linux distributions come with all these packages on the installation media. Others, including Red Hat Linux 6.0, do not supply the MySQL packages. In this case you can download them in RPM format from http://www.mysql.com/downloads/index.html.

Creating data in MySQL

Read the online MySQL documentation. This explains how to connect to MySQL, create a database, create a table, add some data to a table, and create a MySQL user with access to that data.

Obtaining DBI and DBD

Download the latest DBI module for Perl from CPAN. Download it to a safe location, for example, your home directory.

Then download the latest DBD database Driver for MySQL also from CPAN. As before, download it to a safe location, like your home directory.

Install DBI

Become root, and enter the following commands:
tar xvzf DBI-1.14.tar.gz
cd DBI-1.14

Then read and follow the instructions for compiling DBI.

Quick help

On perl Makefile.PL: Unable to open MakeMaker.tmp ... permission denied
Meaning: You are not root.
Solution: Become root.

On make test or make install: Lots of error messages like 'can't find stdio.h, math.h'...
Meaning: The standard C development libraries are not installed.
Solution: Install the glibc-devel RPM package.

Install DBD

Enter the following commands:
tar xvzf Msql-Mysql-modules-1.2214.tar.gz
cd Msql-Mysql-modules-1.2214
Then read and follow the instructions for compiling the DBD for MySQL.

Quick help

Problem: You don't know the answer to the question about where MySQL is installed on your system. Specifically, it asks for an include directory, but it's not on your computer.
Meaning: MySQL-devel is not installed.
Solution: Install the MySQL-devel RPM package.

Testing

To test your DBI installation, try Jeffery Baker's DBI Examples. They are written for Oracle but to make them work with MySQL requires only a simple adjustment. There are other DBI documents at the DBI main page including examples specially for MySQL.

Feedback

I wrote this to help people. Please let me know if it does!
Did this help?
Comment?
 

Back to main page