Getting perl's DBD::mysql module to install on Mac OS X
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)"):
-
Make sure you already have MySQL installed itself and that you can get to the MySQL command line.
-
DBD::mysqlexpects a test database to be present in your MySQL. To set one up, simply get to the MySQL command line viamysql -u user -p
(where
useris your favorite MySQL user) and typecreate database test;
-
Download the latest
DBD::mysqlsource from this directory. You're looking for something likeDBD-mysql-2.9003.tar.gzwith a number in the 3.0 range. Unpack the tarball withtar xvfz DBD-mysql-x.xxxx.tar.gzand 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
-
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 -
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. -
Now build the dang thing and install it via:
make make test sudo make install
-
You should look at the output from the
make testabove 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.