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