Roland Bouman
http://rpbouman.blogspot.com/
1
Writing MySQL 5.1 Plugins
Roland Bouman
http://rpbouman.blogspot.com/
2
Tasks Before Implementing
Plugins
● Include header files:
– Standard headers: <stdlib.h>, <ctype.h>
– MySQL headers for Clean plugins:
<mysql/plugin.h>
– MySQL headers for Unclean plugins:
<mysql_version.h>
● Create a section for plugin declarations:
– mysql_declare_plugin(name)
– mysql_declare_plugin_end
Roland Bouman
http://rpbouman.blogspot.com/
3
Plug-in Declaration
#include <stdlib.h>
#include <ctype.h>
#include <mysql_version.h>
#include <mysql/plugin.h>
...
mysql_declare_plugin()
// your plugin descriptors go here...
{ ... plugin descriptor 1... }
, ...
, { ... plugin descriptor N... }
mysql_declare_plugin_end;
Roland Bouman
http://rpbouman.blogspot.com/
4
Generic Tasks to Implement an
Individual Plug-in
● General plug-in declaration
– st_mysql_plugin defined in plugin.h
● Plug-in type-specific descriptor
– defined in plugin.h
● init function
– Called on INSTALL PLUGIN or server start
● deinit function
– Called on UNINSTALL PLUGIN or shutdown
Roland Bouman
http://rpbouman.blogspot.com/
5
The Generic Plug-in Descriptor
struct st_mysql_plugin
{
int type; /* type constant */
void *info; /* type-specific descriptor */
const char *name; /* name */
const char *author; /* author */
const char *descr; /* description */
int license; /* license type constant */
int (*init)(void *); /* invoked when loaded */
int (*deinit)(void *); /* invoked when unloaded */
unsigned int version; /* plugin version */
struct st_mysql_show_var status_vars; /* status vars */
void * __reserved1; /* system variables */
void * __reserved2; /* config options */
};
Roland Bouman
http://rpbouman.blogspot.com/
6
Plug-in Descriptor Members (1)
● int type: a type constant
– MYSQL_STORAGE_ENGINE_PLUGIN
– MYSQL_FTPARSER_PLUGIN
– MYSQL_DAEMON_PLUGIN
– MYSQL_INFORMATION_SCHEMA_PLUGIN
● void *info: plug-in-type specific struct
– st_mysql_storage_engine
– st_mysql_ft_parser
– st_mysql_daemon
– st_mysql_information_schema
Roland Bouman
http://rpbouman.blogspot.com/
7
Plug-in Descriptor Members (2)
● const char *name: plug-in name
– INSTALL PLUGIN <name> ...
● Human readable metadata
– const char *author
– const char *descr
– int license
● All this is user visible in SHOW PLUGINS
Roland Bouman
http://rpbouman.blogspot.com/
8
Plug-in Descriptor Members (3)
● int (*init) (void *)
– Initialization function
– Invoked on INSTALL PLUGIN, server startup
● int (*deinit) (void *)
– Deinitalization function (cleanup)
– Invoked on UNINSTALL PLUGIN, shutdown
● Argument type is plugin type specific
Roland Bouman
http://rpbouman.blogspot.com/
9
Plug-in Descriptor Members (4)
● struct st_mysql_show_var *status_vars;
– Status variables
– Visible in SHOW STATUS
– Plug-in monitoring
● struct st_mysql_sys_var **system_vars;
– System variables
– Dynamic plug-in configuration
Roland Bouman
http://rpbouman.blogspot.com/
10
Minimal Daemon Plug-in:
Headers and Plug-in Declaration
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <mysql_version.h>
#include <mysql/plugin.h>
//... implementation goes here ...
mysql_declare_plugin(hello_world_daemon)
{
MYSQL_DAEMON_PLUGIN, /* type constant */
&hello_world_daemon_plugin, /* plugin-type specific */
"MYSQL_HELLO_DAEMON", /* Name */
"Roland Bouman (http://rpbouman.blogspot.com/)", /* Author */
"A 'Hello, World!' daemon plugin example.", /* Description */
PLUGIN_LICENSE_GPL, /* License */
hello_world_daemon_plugin_init, /* Init function */
hello_world_daemon_plugin_deinit, /* Deinit function */
0x0010, /* Version (1.0) */
NULL, /* status variables */
NULL, /* system variables */
NULL /* config options */
}
mysql_declare_plugin_end;
Roland Bouman
http://rpbouman.blogspot.com/
11
Minimal Daemon Plug-in:
Implementation
// Log initialization
static int hello_world_daemon_plugin_init(void *p)
{
fprintf(stderr, "hello_world_daemon_plugin_initn");
return 0;
}
// Log de-initialization
static int hello_world_daemon_plugin_deinit(void *p)
{
fprintf(stderr, "hello_world_daemon_plugin_deinitn");
return 0;
}
// plug-in type specific structure
struct st_mysql_daemon hello_world_daemon_plugin=
{ MYSQL_DAEMON_INTERFACE_VERSION };

2. writing MySql plugins general

  • 1.
  • 2.
    Roland Bouman http://rpbouman.blogspot.com/ 2 Tasks BeforeImplementing Plugins ● Include header files: – Standard headers: <stdlib.h>, <ctype.h> – MySQL headers for Clean plugins: <mysql/plugin.h> – MySQL headers for Unclean plugins: <mysql_version.h> ● Create a section for plugin declarations: – mysql_declare_plugin(name) – mysql_declare_plugin_end
  • 3.
    Roland Bouman http://rpbouman.blogspot.com/ 3 Plug-in Declaration #include<stdlib.h> #include <ctype.h> #include <mysql_version.h> #include <mysql/plugin.h> ... mysql_declare_plugin() // your plugin descriptors go here... { ... plugin descriptor 1... } , ... , { ... plugin descriptor N... } mysql_declare_plugin_end;
  • 4.
    Roland Bouman http://rpbouman.blogspot.com/ 4 Generic Tasksto Implement an Individual Plug-in ● General plug-in declaration – st_mysql_plugin defined in plugin.h ● Plug-in type-specific descriptor – defined in plugin.h ● init function – Called on INSTALL PLUGIN or server start ● deinit function – Called on UNINSTALL PLUGIN or shutdown
  • 5.
    Roland Bouman http://rpbouman.blogspot.com/ 5 The GenericPlug-in Descriptor struct st_mysql_plugin { int type; /* type constant */ void *info; /* type-specific descriptor */ const char *name; /* name */ const char *author; /* author */ const char *descr; /* description */ int license; /* license type constant */ int (*init)(void *); /* invoked when loaded */ int (*deinit)(void *); /* invoked when unloaded */ unsigned int version; /* plugin version */ struct st_mysql_show_var status_vars; /* status vars */ void * __reserved1; /* system variables */ void * __reserved2; /* config options */ };
  • 6.
    Roland Bouman http://rpbouman.blogspot.com/ 6 Plug-in DescriptorMembers (1) ● int type: a type constant – MYSQL_STORAGE_ENGINE_PLUGIN – MYSQL_FTPARSER_PLUGIN – MYSQL_DAEMON_PLUGIN – MYSQL_INFORMATION_SCHEMA_PLUGIN ● void *info: plug-in-type specific struct – st_mysql_storage_engine – st_mysql_ft_parser – st_mysql_daemon – st_mysql_information_schema
  • 7.
    Roland Bouman http://rpbouman.blogspot.com/ 7 Plug-in DescriptorMembers (2) ● const char *name: plug-in name – INSTALL PLUGIN <name> ... ● Human readable metadata – const char *author – const char *descr – int license ● All this is user visible in SHOW PLUGINS
  • 8.
    Roland Bouman http://rpbouman.blogspot.com/ 8 Plug-in DescriptorMembers (3) ● int (*init) (void *) – Initialization function – Invoked on INSTALL PLUGIN, server startup ● int (*deinit) (void *) – Deinitalization function (cleanup) – Invoked on UNINSTALL PLUGIN, shutdown ● Argument type is plugin type specific
  • 9.
    Roland Bouman http://rpbouman.blogspot.com/ 9 Plug-in DescriptorMembers (4) ● struct st_mysql_show_var *status_vars; – Status variables – Visible in SHOW STATUS – Plug-in monitoring ● struct st_mysql_sys_var **system_vars; – System variables – Dynamic plug-in configuration
  • 10.
    Roland Bouman http://rpbouman.blogspot.com/ 10 Minimal DaemonPlug-in: Headers and Plug-in Declaration #include <stdlib.h> #include <ctype.h> #include <stdio.h> #include <mysql_version.h> #include <mysql/plugin.h> //... implementation goes here ... mysql_declare_plugin(hello_world_daemon) { MYSQL_DAEMON_PLUGIN, /* type constant */ &hello_world_daemon_plugin, /* plugin-type specific */ "MYSQL_HELLO_DAEMON", /* Name */ "Roland Bouman (http://rpbouman.blogspot.com/)", /* Author */ "A 'Hello, World!' daemon plugin example.", /* Description */ PLUGIN_LICENSE_GPL, /* License */ hello_world_daemon_plugin_init, /* Init function */ hello_world_daemon_plugin_deinit, /* Deinit function */ 0x0010, /* Version (1.0) */ NULL, /* status variables */ NULL, /* system variables */ NULL /* config options */ } mysql_declare_plugin_end;
  • 11.
    Roland Bouman http://rpbouman.blogspot.com/ 11 Minimal DaemonPlug-in: Implementation // Log initialization static int hello_world_daemon_plugin_init(void *p) { fprintf(stderr, "hello_world_daemon_plugin_initn"); return 0; } // Log de-initialization static int hello_world_daemon_plugin_deinit(void *p) { fprintf(stderr, "hello_world_daemon_plugin_deinitn"); return 0; } // plug-in type specific structure struct st_mysql_daemon hello_world_daemon_plugin= { MYSQL_DAEMON_INTERFACE_VERSION };