技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> MySQL --> 利用plugin更快的添加status variables

利用plugin更快的添加status variables

浏览:1588次  出处信息

在MySQL里添加一个system、status variables的比较复杂的,需要修改sql/sql_show.cc,sql/mysqld.cc, 还要修改sql/sql_yacc.yy,然后重新编译等等,前面的文章可见其复杂度,很容易出错。

daemon plugin 除了允许添加后台线程,也允许添加status,且不需要修改mysqld的代码。

#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>
 
static int get_value(MYSQL_THD thd, struct st_mysql_show_var* var, char *buff) {
	struct st_mysql_show_var *status = (struct st_mysql_show_var*) buff;
 
	var->type = SHOW_ARRAY;
	var->value= (char* )status;
 
	status->value = "aaa";
	status->type = SHOW_CHAR;
	status->name = "test1";
 
	status++;
	status->name = 0;
 
	return 0;
 
}
 
struct st_mysql_daemon sys_status = 
{
	MYSQL_DAEMON_INTERFACE_VERSION
};
 
static struct st_mysql_show_var sys_status_var[] = 
{
	{"plugin_test", (char *)&get_value, SHOW_FUNC},
	{0, 0, 0} 
};
 
mysql_declare_plugin(sys_status)
{
	MYSQL_DAEMON_PLUGIN,
	&sys_status,
	"sys_status",
	"hoterran",
	"test",
	PLUGIN_LICENSE_GPL,
	NULL,	
	NULL,	
	0x0100,
	sys_status_var,
	NULL,
	NULL,
}
mysql_declare_plugin_end;

上面添加了一个名字叫做plugin_test的status组,第一个status叫 plugin_test_test1,第二个status->name=0是个哨兵。

get_value(MYSQL_THD *thd, struct st_mysql_show_var* var, char* buff),其中thd为线程THD类, var就是你要填充的内容, buff是预先分配好的1024字节的一个变量,你不需要另外malloc了。先把status的内容填充到buff里,再把status赋值到var->value,就算结束了。

例子里status->value只是简单赋值为”aaaa”,你要加工获得想获得你想要的信息。

编译、安装、加载同上一篇文章。最后的效果如下

mysql> show status like '%plugin_test_test1%';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| plugin_test_test1 | aaa   |
+-------------------+-------+
1 row in set (0.00 sec)

建议继续学习:

  1. 根据status信息对MySQL服务器进行优化(一)    (阅读:2530)
  2. 根据status信息对MySQL服务器进行优化(二)    (阅读:2507)
  3. 动态加载Innodb Plugin    (阅读:2266)
  4. show engine innodb status显示信息不全?    (阅读:1970)
  5. MySQL daemon plugin example    (阅读:1938)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1