JavaScript Editor Js editor     Website development 

Main Page

Previous Page
Next Page

Installing Perl Support for MySQL on Unix and Mac OS X

Like PHP, Perl is automatically installed on most Unix operating systems, including Mac OS X. Along with Perl you will need several modules to access MySQL, primarily DBI and DBD::mysql. A third, called Data-Dumper, is also necessary but may already be installed on your setup.

You have two options for installation:

  • Use CPAN.

  • Build and install each module manually.

    The former is much easier than the latter and is what I will do in the following steps. There are a couple of assumptions involved, though, so see the sidebar on CPAN.

Using CPAN

CPAN was created to provide a regulated system of code for performing certain tasks. CPAN itself is also able to install modules you might need (in other words, CPAN can upgrade its own capabilities).

To access CPAN, use the Terminal application and enter the following (pressing Return afterward):

perl -MCPAN -e shell

The first time you use CPAN, you'll need to answer a slew of questions, most of which will work with the default settings. Once you are in CPAN, you should install several packages, starting with Bundle::libnet, followed by DBI and DBD::mysql. To install any package with CPAN, enter install packagename.

For CPAN to build and install modules, it does require some common Unix utilities, like gcc and make. If you are using Mac OS X, you should install the Developer Tools prior to running CPAN, so that your computer has these utilities.

To install Perl support for MySQL:

Open a Terminal window.

Test that Perl is installed by entering the following and pressing Return (Figure 8.7).

Figure 8.7. Before going further, make sure that you have a good installation of Perl.

perl -v

The command perl -v will report on the version of Perl currently installed and running on your system, along with any other pertinent information. This is a simple test of the installation.

Access CPAN as a superuser (Figure 8.8).

Figure 8.8. Perl support for MySQL will be installed using CPAN. Invoke CPAN as a superuser.

sudo perl -MCPAN -e shell

For a problem-free installation, you should run CPAN as an administrative user (accomplished by sudo). Enter your system's administrative password at the prompt.

Install the Data::Dumper module by typing this at the prompt and pressing Return:

install Data::Dumper

If CPAN reports that Data::Dumper is up to date already, then you are good to continue.

Install the DBI module by typing this at the prompt and pressing Return:

install DBI

PPM will download and install all of the necessary files, reporting on its success (Figure 8.9).

Figure 8.9. If CPAN reports that the make install is OK, the package was successfully installed.

Install the MySQL module by typing this at the prompt and pressing Return:

force install DBD::mysql

Once the DBI package has been installed, you'll need to install the database-specific modules, such as DBD::mysql. The Perl scripts will use DBI and DBD::mysql together to connect to the databases.

The installation is preceded by force because some of the tests will fail if you've already established a root user password for MySQL (which you hopefully have). To ignore those failures, force the installation.

Type quit and press Return to leave CPAN.


  • To see if your Perl system is already configured to interact with MySQL, enter perldoc DBD::mysql at a command prompt (Figure 8.10).

    Figure 8.10. The documentation for DBD::mysql gives you the syntax for using the package.

  • You should update the DBD::mysql package any time you install a new version of MySQL. To do so, just repeat Step 3 and Step 6.

Previous Page
Next Page

JavaScript Editor Js editor     Website development