Perl support for MySQL is provided by means of the
DBI
/DBD
client interface.
The interface requires Perl 5.6.1 or later. It does not
work if you have an older version of Perl.
If you want to use transactions with Perl DBI, you need to have
DBD::mysql
version 1.2216 or newer.
DBD::mysql
2.9003 or newer is recommended.
If you are using the MySQL 4.1 or newer client library, you must
use DBD::mysql
2.9003 or newer.
Perl support is not included with MySQL distributions. You can obtain the necessary modules from http://search.cpan.org for Unix, or by using the ActiveState ppm program on Windows. The following sections describe how to do this.
Perl support for MySQL must be installed if you want to run the MySQL benchmark scripts. See Section 7.1.4, “The MySQL Benchmark Suite”.
MySQL Perl support requires that you have installed MySQL client programming support (libraries and header files). Most installation methods install the necessary files. However, if you installed MySQL from RPM files on Linux, be sure that you've installed the developer RPM. The client programs are in the client RPM, but client programming support is in the developer RPM.
If you want to install Perl support, the files you need can be obtained from the CPAN (Comprehensive Perl Archive Network) at http://search.cpan.org.
The easiest way to install Perl modules on Unix is to use the
CPAN
module. For example:
shell>perl -MCPAN -e shell
cpan>install DBI
cpan>install DBD::mysql
The DBD::mysql
installation runs a number of
tests. These tests attempt to connect to the local MySQL server
using the default username and password. (The default username
is your login name on Unix, and ODBC
on
Windows. The default password is “no password.”) If
you cannot connect to the server with those values (for example,
if your account has a password), the tests fail. You can use
force install DBD::mysql
to ignore the failed
tests.
DBI
requires the
Data::Dumper
module. It may be installed; if
not, you should install it before installing
DBI
.
It is also possible to download the module distributions in the form of compressed tar archives and build the modules manually. For example, to unpack and build a DBI distribution, use a procedure such as this:
-
Unpack the distribution into the current directory:
shell>
gunzip < DBI-
VERSION
.tar.gz | tar xvf -This command creates a directory named
DBI-
VERSION
. -
Change location into the top-level directory of the unpacked distribution:
shell>
cd DBI-
VERSION
-
Build the distribution and compile everything:
shell>
perl Makefile.PL
shell>make
shell>make test
shell>make install
The make test command is important because it
verifies that the module is working. Note that when you run that
command during the DBD::mysql
installation to
exercise the interface code, the MySQL server must be running or
the test fails.
It is a good idea to rebuild and reinstall the
DBD::mysql
distribution whenever you install
a new release of MySQL, particularly if you notice symptoms such
as that all your DBI
scripts fail after you
upgrade MySQL.
If you do not have access rights to install Perl modules in the system directory or if you want to install local Perl modules, the following reference may be useful: http://servers.digitaldaze.com/extensions/perl/modules.html#modules
Look under the heading “Installing New Modules that Require Locally Installed Modules.”
On Windows, you should do the following to install the MySQL
DBD
module with ActiveState Perl:
-
Get ActiveState Perl from http://www.activestate.com/Products/ActivePerl/ and install it.
-
Open a console window (a “DOS window”).
-
If necessary, set the
HTTP_proxy
variable. For example, you might try a setting like this:set HTTP_proxy=my.proxy.com:3128
-
Start the PPM program:
C:\>
C:\perl\bin\ppm.pl
-
If you have not previously done so, install
DBI
:ppm> install DBI
-
If this succeeds, run the following command:
ppm>
install \
ftp://ftp.de.uu.net/pub/CPAN/authors/id/JWIED/DBD-mysql-1.2212.x86.ppd
This procedure should work with ActiveState Perl 5.6 or newer.
If you cannot get the procedure to work, you should install the MyODBC driver instead and connect to the MySQL server through ODBC:
use DBI; $dbh= DBI->connect("DBI:ODBC:$dsn",$user,$password) || die "Got error $DBI::errstr when connecting to $dsn\n";
If Perl reports that it cannot find the
../mysql/mysql.so
module, the problem is
probably that Perl cannot locate the
libmysqlclient.so
shared library. You
should be able to fix this problem by one of the following
methods:
-
Compile the
DBD::mysql
distribution withperl Makefile.PL -static -config
rather thanperl Makefile.PL
. -
Copy
libmysqlclient.so
to the directory where your other shared libraries are located (probably/usr/lib
or/lib
). -
Modify the
-L
options used to compileDBD::mysql
to reflect the actual location oflibmysqlclient.so
. -
On Linux, you can add the pathname of the directory where
libmysqlclient.so
is located to the/etc/ld.so.conf
file. -
Add the pathname of the directory where
libmysqlclient.so
is located to theLD_RUN_PATH
environment variable. Some systems useLD_LIBRARY_PATH
instead.
Note that you may also need to modify the -L
options if there are other libraries that the linker fails to
find. For example, if the linker cannot find
libc
because it is in
/lib
and the link command specifies
-L/usr/lib
, change the -L
option to -L/lib
or add -L/lib
to the existing link command.
If you get the following errors from
DBD::mysql
, you are probably using
gcc (or using an old binary compiled with
gcc):
/usr/bin/perl: can't resolve symbol '__moddi3' /usr/bin/perl: can't resolve symbol '__divdi3'
Add -L/usr/lib/gcc-lib/... -lgcc
to the link
command when the mysql.so
library gets
built (check the output from make for
mysql.so
when you compile the Perl client).
The -L
option should specify the pathname of
the directory where libgcc.a
is located on
your system.
Another cause of this problem may be that Perl and MySQL are not both compiled with gcc. In this case, you can solve the mismatch by compiling both with gcc.
You may see the following error from
DBD::mysql
when you run the tests:
t/00base............install_driver(mysql) failed: Can't load '../blib/arch/auto/DBD/mysql/mysql.so' for module DBD::mysql: ../blib/arch/auto/DBD/mysql/mysql.so: undefined symbol: uncompress at /usr/lib/perl5/5.00503/i586-linux/DynaLoader.pm line 169.
This means that you need to include the -lz
compression library on the link line. That can be done by
changing the following line in the file
lib/DBD/mysql/Install.pm
:
$sysliblist .= " -lm";
Change that line to:
$sysliblist .= " -lm -lz";
After this, you must run make realclean and then proceed with the installation from the beginning.
If you want to install DBI on SCO, you have to edit the
Makefile
in
DBI-xxx
and each subdirectory. Note
that the following assumes gcc 2.95.2 or
newer:
OLD: NEW: CC = cc CC = gcc CCCDLFLAGS = -KPIC -W1,-Bexport CCCDLFLAGS = -fpic CCDLFLAGS = -wl,-Bexport CCDLFLAGS = LD = ld LD = gcc -G -fpic LDDLFLAGS = -G -L/usr/local/lib LDDLFLAGS = -L/usr/local/lib LDFLAGS = -belf -L/usr/local/lib LDFLAGS = -L/usr/local/lib LD = ld LD = gcc -G -fpic OPTIMISE = -Od OPTIMISE = -O1 OLD: CCCFLAGS = -belf -dy -w0 -U M_XENIX -DPERL_SCO5 -I/usr/local/include NEW: CCFLAGS = -U M_XENIX -DPERL_SCO5 -I/usr/local/include
These changes are necessary because the Perl dynaloader does not
load the DBI
modules if they were compiled
with icc or cc.
If you want to use the Perl module on a system that does not
support dynamic linking (such as SCO), you can generate a static
version of Perl that includes DBI
and
DBD::mysql
. The way this works is that you
generate a version of Perl with the DBI
code
linked in and install it on top of your current Perl. Then you
use that to build a version of Perl that additionally has the
DBD
code linked in, and install that.
On SCO, you must have the following environment variables set:
LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib:/usr/progressive/lib
Or:
LD_LIBRARY_PATH=/usr/lib:/lib:/usr/local/lib:/usr/ccs/lib:\ /usr/progressive/lib:/usr/skunk/lib LIBPATH=/usr/lib:/lib:/usr/local/lib:/usr/ccs/lib:\ /usr/progressive/lib:/usr/skunk/lib MANPATH=scohelp:/usr/man:/usr/local1/man:/usr/local/man:\ /usr/skunk/man:
First, create a Perl that includes a statically linked
DBI
module by running these commands in the
directory where your DBI
distribution is
located:
shell>perl Makefile.PL -static -config
shell>make
shell>make install
shell>make perl
Then you must install the new Perl. The output of make perl indicates the exact make command you need to execute to perform the installation. On SCO, this is make -f Makefile.aperl inst_perl MAP_TARGET=perl.
Next, use the just-created Perl to create another Perl that also
includes a statically linked DBD::mysql
by
running these commands in the directory where your
DBD::mysql
distribution is located:
shell>perl Makefile.PL -static -config
shell>make
shell>make install
shell>make perl
Finally, you should install this new Perl. Again, the output of make perl indicates the command to use.