Friday 4 March 2011

Installing a UDF in MySQL in Mac OS X Snow Leopard

I was having a bit of trouble getting a MySQL user-defined function (UDF) running in MySQL in Snow Leopard on my Mac. In the end I came across this page and its comments, which helped a lot. So here's the procedure:

1. Move the C++ UDF file into /usr/local/mysql/bin on your Mac. You should know how to do this in terminal, if not, use the "mv" or "cp" command.

2. Compile the C++ UDF file using g++ - here's the command I used:

> sudo g++ -Wall -bundle -bundle_loader /usr/local/mysql/bin/mysqld -o udf_name.so `/usr/local/mysql/bin/mysql_config --cflags` udf_name.cc

This should give you a file called udf.so in the /usr/local/mysql/bin directory. Note that when you compile on Mac OS X the compiler automatically creates an associated directory called ufo.so.dSYM - you can find out why here.

3. Move the udf.so file and the udf.so.dSYM directory into the MySQL plugin directory in /usr/local/mysql/lib/plugin:


> sudo mv udf.so ../lib/plugin/
> sudo mv udf.so.dSYM/ ../lib/plugin/


4. Install the function in MySQL, making sure you're running a MySQL user with full privileges:


mysql> CREATE FUNCTION udf_name RETURNS REAL SONAME "udf.so";
Query OK, 0 rows affected (0.02 sec)


Once you've done this you should be able to use the UDF in your MySQL queries.