Plugin libraries
Building and installing plugin libraries is very much like building and installing UDFs. The include and library paths are the same but some further build options are needed. This is slightly complicated by the fact that some plugin types (namely Information Schema and Storage Engine plugins) require the MySQL source to be downloaded for the version of the MySQL server you have installed. This is so that the plugin can have access to data and functions that are only "half-public" and are not declared in the installed C header files.
Linux
When compiling on Linux and using just the normal plugin API we can compile in the same way as with UDFs:
gcc -omy_plugin.so my_plugin.c `mysql_config --cflags` -shared -fPIC -DMYSQL_DYNAMIC_PLUGIN
Notice that the main difference here is -DMYSQL_DYNAMIC_PLUGIN
. This sets up the necessary environment for the plugin at compile time.
For plugins that require access to the MySQL server source, compiling is slightly different (suppose, the MySQL source tree is in /Sources/mysql‑5.1.35):
gcc omy_plugin.so my_plugin.cc `mysql_config cflags` —I/Sources/mysql 5.1.35/include/ I/Sources/mysql 5.1.35/regex —I/Sources/mysql 5.1.35/sql shared fPIC fno exceptions —fno rtti DMYSQL_DYNAMIC_PLUGIN
Typically, such a plugin will be in C++, not C. It is compiled exactly the same way the main server is—without exceptions or runtime type identification. Technically, it could use exceptions, but then it may need to use g++
instead of gcc
as a C++ compiler. Either way, it needs extra include paths that point to the include/, regex/
, and sql/
directories of the MySQL source tree.
Mac OS X
Just as in the UDF case, compiling plugins on Mac OS X is almost the same as on Linux. You can use the same command line and only replace ‑shared
‑fPIC with ‑bundle
or bundle ‑Wl, ‑undefined ‑Wl,dynamic_lookup
as explained before.
Windows
In Windows we can compile MySQL plugins that do not require the inclusion of the MySQL source code (everything except Information Schema and Storage Engine plugins) using a process very similar to compiling UDFs.
First, we need to create an empty project file to contain the source and build environment:
We can then add or create a .cpp
file containing the source for our plugin:
This project needs to be a .dll
, not an executable one. We can set this in the project's Property Pages dialog:
We now need to set up the C/C++ include paths so that the MySQL include path is in them:
This final step is different to compiling the UDFs. We need to add a C/C++ preprocessor definition so that the include files set up everything we need for a MySQL plugin. To do this we simply add MYSQL_DYNAMIC_PLUGIN
to the definitions list:
Installing a plugin
Just as with UDFs, our MySQL plugin needs to be in plugin_dir
before it can be added to MySQL. Once it is located there the syntax is very simple. All of the details about how to use the plugin are in the plugin itself. So we simply need:
INSTALL PLUGIN my_plugin SONAME 'my_plugin.so'