环境:
操作系统:window8.1
cygwin:x86_64
mysql:Generic Linux (Architecture Independent) 5.6.17
一、插件编写
1、创建目录
在mysql的插件源码目录(plugin)创建插件目录
2、编写代码
创建cc文件(c文件会导致编译错误),内容参考下面的源码
3、编写CMake文件
参考CMake文件
4、生成Make文件
回到mysql源码根目录,参考相关资料2中的生成makefile的操作
5、编译
进入1中目录,执行make命令
6、拷贝文件到mysql安装目录的lib/plugin
7、安装插件
INSTALL PLUGIN mytest SONAME 'TestJyx.dll';
其他操作:
1、展开宏定义,用于观察宏的定义
g++ -I/cygdrive/e/opensource/mysql/mysql-5.6.17-win/mysql-5.6.17/include -I/cygdrive/e/opensource/mysql/mysql-5.6.17-win/mysql-5.6.17/sql -DMYSQL_DYNAMIC_PLUGIN -E TestJyx.c > TestJyx.e
相关资料
1、http://www.hoterran.info/mysql-daemon-plugin-example
2、http://www.cnblogs.com/northhurricane/p/3665120.html
源码
/* This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/* follow mysql daemon_example.
Since GB code and UTF-8 code will lead conflict of displaying, I wiil use my poor English to comment.*/
#include "my_global.h"
#include "sql_priv.h"
#include "stdlib.h"
#include "ctype.h"
#include "mysql_version.h"
#include "mysql/plugin.h"
#include "my_dir.h"
#include "my_pthread.h" // pthread_handler_t
#include "my_sys.h" // my_write, my_malloc
#include "m_string.h" // strlen
#include "sql_plugin.h" // st_plugin_int. retrieve plugin runtime infomation
//record this plugin's infomation. Although we can use static variables, why not try this?
struct mytest_context
{
pthread_t mytest_thread; //thread of myself
};
char buffer[100];
char*
mytest_get_current_time()
{
time_t result;
struct tm tm_tmp;
result= time(NULL);
localtime_r(&result, &tm_tmp);
my_snprintf(buffer, sizeof(buffer),
"show me at %02d%02d%02d %2d:%02d:%02d\n",
tm_tmp.tm_year % 100,
tm_tmp.tm_mon+1,
tm_tmp.tm_mday,
tm_tmp.tm_hour,
tm_tmp.tm_min,
tm_tmp.tm_sec);
return buffer;
}
pthread_handler_t mytest_showme(void *p)
{
DBUG_ENTER("mytest_showme");
while(1)
{
sleep(5);
char *current_time = mytest_get_current_time();
fprintf(stderr, current_time);
}
DBUG_RETURN(0);
}
/*
Initialize this plugin at server start or plugin installation.
SYNOPSIS
mytest_plugin_init()
DESCRIPTION
Starts up mytest_showme thread
RETURN VALUE
0 success
1 failure (cannot happen)
*/
static int mytest_plugin_init(void *p)
{
DBUG_ENTER("mytest_plugin_init");
struct mytest_context *con;
pthread_attr_t attr; /* Thread attributes */
struct st_plugin_int *plugin= (struct st_plugin_int *)p;
con = (struct mytest_context *)
my_malloc(sizeof(struct mytest_context), MYF(0));
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,
PTHREAD_CREATE_JOINABLE);
/* now create the thread */
if (pthread_create(&con->mytest_thread, &attr, mytest_showme,
(void *)con) != 0)
{
fprintf(stderr,"Could not create heartbeat thread!\n");
DBUG_RETURN(1);
}
plugin->data= (void *)con;
fprintf(stderr,"Initialize success!\n");
DBUG_RETURN(0);
}
/*
Terminate this plugin at server shutdown or plugin deinstallation.
SYNOPSIS
mytest_plugin_deinit()
Does nothing.
RETURN VALUE
0 success
1 failure (cannot happen)
*/
static int mytest_plugin_deinit(void *p)
{
DBUG_ENTER("mytest_plugin_deinit");
struct st_plugin_int *plugin= (struct st_plugin_int *)p;
struct mytest_context *con=
(struct mytest_context *)plugin->data;
void *dummy_retval;
pthread_cancel(con->mytest_thread);
/*
Need to wait for the hearbeat thread to terminate before closing
the file it writes to and freeing the memory it uses
*/
pthread_join(con->mytest_thread, &dummy_retval);
my_free(con);
fprintf(stderr,"Deinitialize success!\n");
DBUG_RETURN(0);
}
struct st_mysql_daemon mytest_plugin=
{ MYSQL_DAEMON_INTERFACE_VERSION };
/*
Plugin library descriptor
*/
mysql_declare_plugin(daemon_example)
{
MYSQL_DAEMON_PLUGIN,
&mytest_plugin,
"mytest", /* the plugin name of sql "INSTALL PLUGIN plugin_name SONAME 'plugin_library'" */
"OldJ",
"Mytest, show info",
PLUGIN_LICENSE_GPL,
mytest_plugin_init, /* Plugin Init */
mytest_plugin_deinit, /* Plugin Deinit */
0x0100 /* 1.0 */,
NULL, /* status variables */
NULL, /* system variables */
NULL, /* config options */
0, /* flags */
}
mysql_declare_plugin_end;
CMakeLists.txt文件
MYSQL_ADD_PLUGIN(TestJyx TestJyx.cc
MODULE_ONLY MODULE_OUTPUT_NAME "TestJyx")