2.12 Perl Installation Notes

The Perl 'DBI' module provides a generic interface for database access. You can write a 'DBI' script that works with many different database engines without change. To use 'DBI', you must install the 'DBI' module, as well as a DataBase Driver (DBD) module for each type of database server you want to access. For MySQL, this driver is the 'DBD::mysql' module.

Note:

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.

The 'DBI'/'DBD' interface requires Perl 5.6.0, and 5.6.1 or later is preferred. DBI does not work if you have an older version of Perl. You should use 'DBD::mysql' 4.009 or higher. Although earlier versions are available, they do not support the full functionality of MySQL 5.7.

 File: manual.info.tmp, Node: perl-installation, Next: activestate-perl, Prev: perl-support, Up: perl-support

2.12.1 Installing Perl on Unix

MySQL Perl support requires that you have installed MySQL client programming support (libraries and header files). Most installation methods install the necessary files. If you install MySQL from RPM files on Linux, be sure to install the developer RPM as well. The client programs are in the client RPM, but client programming support is in the developer RPM.

The files you need for Perl support 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:

 $> 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 user name and password. (The default user name 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:

  1. Unpack the distribution into the current directory:

      $> gunzip < DBI-VERSION.tar.gz | tar xvf -

    This command creates a directory named 'DBI-VERSION'.

  2. Change location into the top-level directory of the unpacked distribution:

      $> cd DBI-VERSION
  3. Build the distribution and compile everything:

      $> perl Makefile.PL
      $> make
      $> make test
      $> 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. This ensures that the latest versions of the MySQL client libraries are installed correctly.

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://learn.perl.org/faq/perlfaq8.html#How-do-I-keep-my-own-module-library-directory-

 File: manual.info.tmp, Node: activestate-perl, Next: perl-support-problems, Prev: perl-installation, Up: perl-support

2.12.2 Installing ActiveState Perl on Windows

On Windows, you should do the following to install the MySQL 'DBD' module with ActiveState Perl:

  1. Get ActiveState Perl from http://www.activestate.com/Products/ActivePerl/ and install it.

  2. Open a console window.

  3. If necessary, set the 'HTTP_proxy' variable. For example, you might try a setting like this:

      C:\> set HTTP_proxy=my.proxy.com:3128
  4. Start the PPM program:

      C:\> C:\perl\bin\ppm.pl
  5. If you have not previously done so, install 'DBI':

      ppm> install DBI
  6. If this succeeds, run the following command:

      ppm> install DBD-mysql

This procedure should work with ActiveState Perl 5.6 or higher.

If you cannot get the procedure to work, you should install the ODBC 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";

 File: manual.info.tmp, Node: perl-support-problems, Prev: activestate-perl, Up: perl-support

2.12.3 Problems Using the Perl DBI/DBD Interface

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:

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 path name 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'.

 File: manual.info.tmp, Node: tutorial, Next: programs, Prev: installing, Up: Top

3 Tutorial **********

This chapter provides a tutorial introduction to MySQL by showing how to use the note 'mysql': mysql. client program to create and use a simple database. note 'mysql': mysql. (sometimes referred to as the 'terminal monitor' or just 'monitor') is an interactive program that enables you to connect to a MySQL server, run queries, and view the results. note 'mysql': mysql. may also be used in batch mode: you place your queries in a file beforehand, then tell note 'mysql': mysql. to execute the contents of the file. Both ways of using *note 'mysql': mysql. are covered here.

To see a list of options provided by *note 'mysql': mysql, invoke it with the '--help' option:

 $> mysql --help

This chapter assumes that note 'mysql': mysql. is installed on your machine and that a MySQL server is available to which you can connect. If this is not true, contact your MySQL administrator. (If you are the administrator, you need to consult the relevant portions of this manual, such as note server-administration::.)

This chapter describes the entire process of setting up and using a database. If you are interested only in accessing an existing database, you may want to skip the sections that describe how to create the database and the tables it contains.

Because this chapter is tutorial in nature, many details are necessarily omitted. Consult the relevant sections of the manual for more information on the topics covered here.

 File: manual.info.tmp, Node: connecting-disconnecting, Next: entering-queries, Prev: tutorial, Up: tutorial