MySQL源码中包含了插件开发的例子,在源码目录plugin/daemon_example,该例子实现的是每隔5秒向数据目录的mysql-heartbeat.log文件中写入心跳时间戳.以daemon_example为基础,开发自定义插件。
image.png拷贝daemon_example目录改名为my_plugin,依次修改my_plugin目录下的文件,把daemon_example修改为my_plugin
#CMakeLists.txt
MYSQL_ADD_PLUGIN(my_plugin
my_plugin.cc
MODULE_ONLY
MODULE_OUTPUT_NAME "libmy_plugin"
TEST_ONLY
)
INSTALL(FILES
my_plugin.ini
DESTINATION ${INSTALL_PLUGINDIR}
COMPONENT Test
)
#my_plugin.cc
fn_format(heartbeat_filename, "my_plugin", "", ".log",
MY_REPLACE_EXT | MY_UNPACK_FILENAME);
mysql_declare_plugin(daemon_example){
MYSQL_DAEMON_PLUGIN,
&daemon_example_plugin,
"my_plugin",
"jzh",
"Daemon example, creates a heartbeat beat file in my_plugin.log",
PLUGIN_LICENSE_GPL,
daemon_example_plugin_init, /* Plugin Init */
NULL, /* Plugin Check uninstall */
daemon_example_plugin_deinit, /* Plugin Deinit */
0x0100 /* 1.0 */,
NULL, /* status variables */
NULL, /* system variables */
NULL, /* config options */
0, /* flags */
} mysql_declare_plugin_end;
#my_plugin.ini
libmy_plugin
my_plugin
clion 编译插件生成 libdaemon_example.so 并放到插件目录下 ,授予可以执行权限
cp ./cmake-build-debug/plugin_output_directory/libdaemon_example.so /data1/mycode/mysql_debug/mysql-8.0.18/mysql_stuff/install/lib/plugin/
chown jzh:jzh /data1/mycode/mysql_debug/mysql-8.0.18/mysql_stuff/install/lib/plugin/libmy_plugin.so
安装
INSTALL PLUGIN my_plugin SONAME 'my_plugin.so';
卸载
UNINSTALL PLUGIN my_plugin;
查看
select * from mysql.plugin;
网友评论