Documentation Home
MySQL 5.6 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 31.0Mb
PDF (A4) - 31.0Mb
PDF (RPM) - 29.3Mb
HTML Download (TGZ) - 7.2Mb
HTML Download (Zip) - 7.2Mb
HTML Download (RPM) - 6.2Mb
Man Pages (TGZ) - 175.7Kb
Man Pages (Zip) - 286.0Kb
Info (Gzip) - 3.0Mb
Info (Zip) - 3.0Mb
Excerpts from this Manual UDF Compiling and Installing

Files implementing UDFs must be compiled and installed on the host where the server runs. This process is described below for the example UDF file sql/ that is included in MySQL source distributions.

If a UDF will be referred to in statements that will be replicated to slave servers, you must ensure that every slave also has the function available. Otherwise, replication will fail on the slaves when they attempt to invoke the function.

The immediately following instructions are for Unix. Instructions for Windows are given later in this section.

The file contains the following functions:

  • metaphon() returns a metaphon string of the string argument. This is something like a soundex string, but it is more tuned for English.

  • myfunc_double() returns the sum of the ASCII values of the characters in its arguments, divided by the sum of the length of its arguments.

  • myfunc_int() returns the sum of the length of its arguments.

  • sequence([const int]) returns a sequence starting from the given number or 1 if no number has been given.

  • lookup() returns the IP address for a host name.

  • reverse_lookup() returns the host name for an IP address. The function may be called either with a single string argument of the form '' or with four numbers.

  • avgcost() returns an average cost. This is an aggregate function.

A dynamically loadable file should be compiled as a sharable library file, using a command something like this:

shell> gcc -shared -o

If you are using gcc with CMake (which is how MySQL is configured), you should be able to create with a simpler command:

shell> make udf_example

After you compile a shared object containing UDFs, you must install it and tell MySQL about it. Compiling a shared object from using gcc directly produces a file named Copy the shared object to the server's plugin directory and name it This directory is given by the value of the plugin_dir system variable.

On some systems, the ldconfig program that configures the dynamic linker does not recognize a shared object unless its name begins with lib. In this case you should rename a file such as to

On Windows, you can compile user-defined functions by using the following procedure:

  1. Obtain a MySQL source distribution. See Section 2.1.2, “How to Get MySQL”.

  2. Obtain the CMake build utility, if necessary, from (Version 2.6 or later is required).

  3. In the source tree, look in the sql directory. There are files named udf_example.def there. Copy both files from this directory to your working directory.

  4. Create a CMake makefile (CMakeLists.txt) with these contents:

    # Path for MySQL include directory
    ADD_LIBRARY(udf_example MODULE udf_example.def)
    TARGET_LINK_LIBRARIES(udf_example wsock32)
  5. Create the VC project and solution files:

    cmake -G "<Generator>"

    Invoking cmake --help shows you a list of valid Generators.

  6. Create udf_example.dll:

    devenv udf_example.sln /build Release

After the shared library file has been installed, notify mysqld about the new functions with the following statements. If library files have a suffix different from .so on your system, substitute the correct suffix throughout (for example, .dll on Windows).

mysql> CREATE FUNCTION reverse_lookup
    ->        RETURNS STRING SONAME '';
    ->        RETURNS REAL SONAME '';

Once installed, a function remains installed until it is uninstalled.

To delete functions, use DROP FUNCTION:

mysql> DROP FUNCTION metaphon;
mysql> DROP FUNCTION myfunc_double;
mysql> DROP FUNCTION myfunc_int;
mysql> DROP FUNCTION sequence;
mysql> DROP FUNCTION lookup;
mysql> DROP FUNCTION reverse_lookup;
mysql> DROP FUNCTION avgcost;

The CREATE FUNCTION and DROP FUNCTION statements update the func system table in the mysql database. The function's name, type and shared library name are saved in the table. You must have the INSERT or DELETE privilege for the mysql database to create or drop functions, respectively.

You should not use CREATE FUNCTION to add a function that has previously been created. If you need to reinstall a function, you should remove it with DROP FUNCTION and then reinstall it with CREATE FUNCTION. You would need to do this, for example, if you recompile a new version of your function, so that mysqld gets the new version. Otherwise, the server continues to use the old version.

An active function is one that has been loaded with CREATE FUNCTION and not removed with DROP FUNCTION. All active functions are reloaded each time the server starts, unless you start mysqld with the --skip-grant-tables option. In this case, UDF initialization is skipped and UDFs are unavailable.

User Comments
  Posted by Anthony Ball on April 15, 2003
Helpful hint for debugging: fprintf(stderr,"Hello world"); anything printed to stderr will go to the mysql error log.
%lld is the proper format for printing long longs.
  Posted by Anthony Ball on April 21, 2003
After much time trying to track a problem on solaris(2.7, gcc 2.9-gnupro-99r1) I found that I needed -c -shared as compiler options. Almost everything worked without the -c, but strangely, any time I tried to use double quoted strings in the code they got turned into empty strings.
  Posted by Leonard Cuff on August 2, 2005
On Mac OS X 10.4 (Tiger), the following incantations were needed, in
order to build a "shared object" file.

gcc -c myfunction.c
gcc -bundle -o myfunction.o -undefined dynamic_lookup

  Posted by matthieu aubry on October 25, 2006
I wanted to install the UDF to compute the levenshtein distance between two strings.
I put here all the commands i used, from scratch (both on the server and my mind), to make it running.
I had several little problems that you can't really solve without knowing some linux tips.

This is different from what I saw in this doc.

Here is the command for a debian sarge stable server.

>apt-get source mysql-server
>locate udf_example.c
>cd /usr/share/doc/libmysqlclient14-dev/examples
>gcc -shared -o
>locate mysql.h
>gcc -shared -o -I/usr/include/mysql/
>mv /usr/lib/

#at this step i'm happy because the example from mysql works (note that i had to add the -I for the path and change the file extension to .cc)

# now i download the package from the mysql udf repository

>tar -xzvf dludf.cgi\?ckey\=28
>gcc -shared -o -I/usr/include/mysql/
>mv /usr/lib

#Then i launch mysql
>mysql -uroot -pPASS

# in mysql
mysql> use DATABASE

Database changed

mysql> select levenshtein(w1.word,w2.word) as dist from word w1, word w2 where ETC........... order by dist asc limit 0,10;

Enjoy :-)
  Posted by Adam Block on May 19, 2007
To compile mysqllevenshtein (as it happens, though I imagine this would be the case with other functions) on Ubuntu Dapper Drake, I needed to add the -fPIC compiler flag:

gcc -shared -o \
-I/usr/include/mysql/ -fPIC

  Posted by Matthias Fripp on April 5, 2008
To clarify the Mac OS X example slightly: I use the following commands to build and install the example UDF on OS X 10.4. These should be executed in the directory where udf_example.c is stored.

# On OS X, use "-bundle" instead of "-shared",
# and specify where the mysql include files are
# (they should be in your main mysql directory).
gcc -bundle -o udf_example.c -I/usr/local/mysql/include

# MySQL will look for the shared library in /usr/lib, so put it there:
sudo cp /usr/lib
  Posted by newbie newbie on July 14, 2008
If you're still in trouble with the levenshtein function, here you can find a manual concerning the installation of the levenshtein group function:
It's in German, but I hope it helps anyway.
  Posted by Juan Miguel Cejuela on November 25, 2011
Implementation of levenshtein distance and k-bounded levenshtein distance (time O(kl), space(k))

Entirely written in C
Sign Up Login You must be logged in to post a comment.