← Back to Archives

Getting perl's DBD::mysql module to install on Mac OS X

hacks

If you want perl programs to interface with a MySQL database, you will probably want to use the DBD::mysql module. On Mac OS X, there's a few extra things you have to do to get this module to compile correctly. Here's the skinny...

[More:]

I tried installing DBD::mysql via the CPAN shell, but nothing I tried would work (Essentially I wasn't sure how to specify a test database, user and password via the shell for the build).

Here's how to do it with a more traditional perl module install (this is mostly from the following post: "How to Install DBD::mysql on Mac OS X 10.3 (Panther)"):

  1. Make sure you already have MySQL installed itself and that you can get to the MySQL command line.

  2. DBD::mysql expects a test database to be present in your MySQL. To set one up, simply get to the MySQL command line via

    mysql -u user -p

    (where user is your favorite MySQL user) and type

    create database test;

  3. Download the latest DBD::mysql source from this directory. You're looking for something like DBD-mysql-2.9003.tar.gz with a number in the 3.0 range. Unpack the tarball with tar xvfz DBD-mysql-x.xxxx.tar.gz and change directories into the DBD-mysql-x.xxxx directory.

    Note: as commentor Randal Schwartz points out:

    You can skip the "download" and "unpack" steps from directly within the CPAN shell. Just say "look DBD::mysql" and the module will be downloaded from the usual places, unpacked, and then drops you into a shell in that directory.

    You can get to the CPAN shell via typing:

    perl -MCPAN -e shell

  4. You need to give the Makefile some special parameters... note your MySQL username, password and then type:

    perl Makefile.PL --testdb=test --testuser=username
    --testpassword=password --testhost=localhost

  5. Finally, you have to replace a few things in the Makefile... simply do this from the command-line:

    perl -pi -e's/MACOSX/env MACOSX/' Makefile

    Note: You don't need to do this using DBD-mysql-3.0002.

  6. Now build the dang thing and install it via:

    make make test sudo make install

  7. You should look at the output from the make test above carefully as that will give you a good idea if the install is going to work at all.

Please let me know if this didn't work for you or if this is no longer needed.

UPDATE [2005-12-13T14:01:59]: If you upgrade MySQL, you should re-install DBD::mysql or you will get the following error:

Can't connect to database: Client does not support authentication protocol requested by server; consider upgrading MySQL client

BONUS: To pass an object's method into a subroutine, do the following:

call_a_lot(10, $some_obj, "methname") sub call_a_lot { my ($count, $widget, $trick) = @_; for (my $i = 0; $i < $count; $i++) { $widget->$trick(); } }

This works great with the DBD's fetchrow_hashref() method.