How to install the Perl module DBI on Linux
By Phil Jones, July 00
Email: pjls16812 at blueyonder dot co dot uk
Index
-
About this document
-
Explanation of DBI and DBD
-
What this article doesn't do
-
Please correct my errors
-
Assumptions
-
Starting point
-
Overview
-
Checking for prerequisites
-
Explanation of the packages the shell script tests for
-
Installing prerequisites
-
Creating data in MySQL
-
Obtaining DBI and DBD
-
Install DBI
-
Install DBD
-
Testing
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.
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.
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.
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.
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.
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.
The outline of the procedure is:
-
Check required packages are installed
-
Install required packages that are not installed
-
Obtain Perl DBI and DBD for MySQL
-
Compile and install Perl DBI and DBD for MySQL
-
Create a sample database in MySQL
-
Create a sample table in that database
-
Create some sample data in that table
-
Create a MySQL user with access to the sample data
-
Write a simple Perl program that prints data from MySQL.
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.
-
Perl programming language
-
Make
-
Kernel headers
-
A C compiler, eg EGCS, GNU Compiler Collection, and
GNU C Compiler
-
C++ extensions for the C compiler
-
Glibc library
-
Glibc development library
-
MySQL server
-
MySQL client
-
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.
-
Perl is definitely required.
-
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.
-
Kernel headers are structures and constants needed for compiling most standard
programs. You very likely need this.
-
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. An alternative seems
to be the GNU Compiler Collection (gcc) with the GNU C Compiler (kgcc).
-
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.
-
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.
-
Glibc-devel contains header files and object files needed when compiling
programs that use the standard C libraries. Required.
-
MySQL is the database server. Required.
-
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.
-
MySQL-devel makes it possible for other programs, such as DBI, to work with
MySQL. Required.
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.
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.
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.
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: Error messages like 'can't find float.h, iso646.h, proto.h, stdarg.h'...
Meaning: The GNU C Compiler for kernel compilation (kgcc) RPM package is not installed.
Solution: Install the GNU C Compiler for kernel compilation RPM package, kgcc.
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.
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.
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.