mysql 插件开发

1.代码demo

#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <plugin.h>
#include <mysql_version.h>
#include <my_global.h>
#include <my_sys.h>
#include <pthread.h>
extern ulong        thread_id;
extern uint        thread_count;
extern ulong        max_connections;
static pthread_t    G_thread;
pthread_t ntid;

pthread_handler_t func(void *p)
{
    while(1) {
         sleep(5);
         fprintf(stderr, "Thread id [%ld]  Max_connections:%lu\n",
             thread_id, max_connections);
         }
}

static int monitor_plugin_init(void *p)
{
       
   if (pthread_create(&G_thread,NULL,func, NULL) != 0) {}
 
    fprintf(stderr, "%s", "Monitor plugin init\n");
    return 0;
}

static int monitor_plugin_deinit(void *p)
{
    pthread_cancel(G_thread);
    pthread_join(G_thread, NULL);
    fprintf(stderr, "%s", "Monitor_plugin deinit\n");
 
    return 0;
}
 
struct st_mysql_daemon monitor_plugin =
{
    MYSQL_DAEMON_INTERFACE_VERSION
};
 
mysql_declare_plugin(monitor_plugin)
{
    MYSQL_DAEMON_PLUGIN,
    &monitor_plugin,
    "monitor",
    "hoterran",
    "test",
    PLUGIN_LICENSE_GPL,
    monitor_plugin_init,
    monitor_plugin_deinit,
    0x0100,
    NULL,
    NULL,
    NULL,
}
mysql_declare_plugin_end;


编译前查看mysql函数库的路经。

/usr/local/mysql/bin/mysql_config --cflags
-I/usr/local/mysql/include  -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing
[root@localhost demo]#

编译:

#gcc -o monitor.o monitor.c -shared '/usr/local/mysql/bin/mysql_config --cflags'

gcc -g -Wall -I/usr/local/mysql/include/ -DMYSQL_DYNAMIC_PLUGIN   -c -o monitor.o monit
or.c

64位系统:

错误:/usr/bin/ld: monitor.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC

在编译时加-fPIC

gcc -g -Wall -I/usr/local/mysql/include/ -DMYSQL_DYNAMIC_PLUGIN   -c -fPIC -o monitor.o
 monitor.c

gcc -shared -o libmonitor.so monitor.o


拷贝monitor.o

cp monitor.o /usr/local/mysql/lib/plugin/

show variables like 'plugin_dir';


进入mysql:

加载:

install plugin monitor soname 'libmonitor.so';

show plugins;

select * from mysql.plugin




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值