博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UDF 编写自定函数
阅读量:5339 次
发布时间:2019-06-15

本文共 13193 字,大约阅读时间需要 43 分钟。

在Mysql的官方文档中有说明如何给Mysql添加新的函数。添加的函数可分为3种:1.自行编写函数的源码,将源码编译成动态库,然后使用Mysql的CREATE FUNCTION语句来将函数添加进Mysql。2.在源码等级上加入自行编写的函数,这样编译之后,新加入的函数就和Native函数一样,永久使用。3.通过SQL语句脚本来完成函数的功能。就这3中方式,第三种明显不足以完成我们所需要的业务工作,因为SQL语句不存在连接缓存的功能。第二种方法看似美好,但是对于需要更换缓存方法(从redis更换成memcached),重新编译数据库源码,这种不符合工程化商务化。所以采用第一种方法自行编写函数代码,动态导入函数到mysql之中。
[root@server-mysql ~]# vi test_add.cpp#include 
extern "C" long long testadd(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error){ int a = *((long long *)args->args[0]); int b = *((long long *)args->args[1]); return a + b;}extern "C" my_bool testadd_init(UDF_INIT *initid, UDF_ARGS *args, char *message){ return 0;}
[root@server-mysql ~]# g++ -shared -fPIC -I /usr/local/mysql56/include -otest_add.so test_add.cpp [root@server-mysql ~]# cp test_add.so /usr/local/mysql56/lib/plugin
my.cnfplugin-load=test_add.so
mysql> use mysql;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> CREATE FUNCTION testadd RETURNS INTEGER SONAME 'test_add.so';Query OK, 0 rows affected (0.16 sec) mysql> select testadd(1,2);+--------------+| testadd(1,2) |+--------------+|            3 |+--------------+1 row in set (0.00 sec)
mysql> select * from func;+---------+-----+-------------+----------+| name    | ret | dl          | type     |+---------+-----+-------------+----------+| testadd |   2 | test_add.so | function |+---------+-----+-------------+----------+1 row in set (0.07 sec)
func表格记录了所有UDF函数的信息,表格中ret代表的是返回值类型,之所以这里记录的是2,是因为我们返回的是整型数据,可以在mysql的源码中sql目录下的mysql_com.h找到类型的枚举定义。enum Item_result {STRING_RESULT=0, REAL_RESULT, INT_RESULT, ROW_RESULT, DECIMAL_RESULT};

 

udf安全性:

#include 
#include
extern "C" long long testadd(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) { int a = *((long long *)args->args[0]); int b = *((long long *)args->args[1]); int *p = (int *)malloc(sizeof(int)); free(p); free(p); return a + b; } extern "C" my_bool testadd_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { return 0; }

 

mysql> select testadd(1,2);ERROR 2013 (HY000): Lost connection to MySQL server during query
[root@server-mysql log]# *** glibc detected *** /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld: double free or corruption (fasttop): 0x00007ff00c05cf00 ***======= Backtrace: =========/lib64/libc.so.6[0x36bb075366]/usr/local/mysql56/lib/plugin/test_add.so(testadd+0x64)[0x7ff02ab5a6e0]/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_ZN17Item_func_udf_int7val_intEv+0x66)[0x5f4606]/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_ZN4Item4sendEP8ProtocolP6String+0x1a1)[0x59bf41]/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_ZN8Protocol19send_result_set_rowEP4ListI4ItemE+0x92)[0x66edf2]/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_ZN11select_send9send_dataER4ListI4ItemE+0x6c)[0x6b7bbc]/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_ZN4JOIN4execEv+0x608)[0x6d1488]/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld[0x718039]/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_Z12mysql_selectP3THDP10TABLE_LISTjR4ListI4ItemEPS4_P10SQL_I_ListI8st_orderESB_S7_yP13select_resultP18st_select_lex_unitP13st_select_lex+0xbc)[0x718afc]/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_Z13handle_selectP3THDP13select_resultm+0x175)[0x718d05]/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld[0x6f34d9]/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_Z21mysql_execute_commandP3THD+0x3575)[0x6f7c95]/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_Z11mysql_parseP3THDPcjP12Parser_state+0x338)[0x6fb7c8]/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_Z16dispatch_command19enum_server_commandP3THDPcj+0xc60)[0x6fcba0]/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_Z10do_commandP3THD+0xd7)[0x6fe997]/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(_Z24do_handle_one_connectionP3THD+0x116)[0x6c5906]/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(handle_one_connection+0x45)[0x6c59e5]/usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld(pfs_spawn_thread+0x126)[0xae0596]/lib64/libpthread.so.0[0x36bb807851]/lib64/libc.so.6(clone+0x6d)[0x36bb0e767d]======= Memory map: ========00400000-00f9f000 r-xp 00000000 08:03 423182                             /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld0119e000-0134c000 rw-p 00b9e000 08:03 423182                             /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/bin/mysqld0134c000-0139d000 rw-p 00000000 00:00 0 01518000-0198e000 rw-p 00000000 00:00 0                                  [heap]36ba800000-36ba820000 r-xp 00000000 08:03 824743                         /lib64/ld-2.12.so36baa1f000-36baa20000 r--p 0001f000 08:03 824743                         /lib64/ld-2.12.so36baa20000-36baa21000 rw-p 00020000 08:03 824743                         /lib64/ld-2.12.so36baa21000-36baa22000 rw-p 00000000 00:00 0 36bac00000-36bac01000 r-xp 00000000 08:03 806064                         /lib64/libaio.so.1.0.136bac01000-36bae00000 ---p 00001000 08:03 806064                         /lib64/libaio.so.1.0.136bae00000-36bae01000 rw-p 00000000 08:03 806064                         /lib64/libaio.so.1.0.136bb000000-36bb189000 r-xp 00000000 08:03 824744                         /lib64/libc-2.12.so36bb189000-36bb388000 ---p 00189000 08:03 824744                         /lib64/libc-2.12.so36bb388000-36bb38c000 r--p 00188000 08:03 824744                         /lib64/libc-2.12.so36bb38c000-36bb38d000 rw-p 0018c000 08:03 824744                         /lib64/libc-2.12.so36bb38d000-36bb392000 rw-p 00000000 00:00 0 36bb400000-36bb402000 r-xp 00000000 08:03 824750                         /lib64/libdl-2.12.so36bb402000-36bb602000 ---p 00002000 08:03 824750                         /lib64/libdl-2.12.so36bb602000-36bb603000 r--p 00002000 08:03 824750                         /lib64/libdl-2.12.so36bb603000-36bb604000 rw-p 00003000 08:03 824750                         /lib64/libdl-2.12.so36bb800000-36bb817000 r-xp 00000000 08:03 824745                         /lib64/libpthread-2.12.so36bb817000-36bba17000 ---p 00017000 08:03 824745                         /lib64/libpthread-2.12.so36bba17000-36bba18000 r--p 00017000 08:03 824745                         /lib64/libpthread-2.12.so36bba18000-36bba19000 rw-p 00018000 08:03 824745                         /lib64/libpthread-2.12.so36bba19000-36bba1d000 rw-p 00000000 00:00 0 36bc000000-36bc007000 r-xp 00000000 08:03 824746                         /lib64/librt-2.12.so36bc007000-36bc206000 ---p 00007000 08:03 824746                         /lib64/librt-2.12.so36bc206000-36bc207000 r--p 00006000 08:03 824746                         /lib64/librt-2.12.so36bc207000-36bc208000 rw-p 00007000 08:03 824746                         /lib64/librt-2.12.so36c2000000-36c2016000 r-xp 00000000 08:03 824759                         /lib64/libgcc_s-4.4.6-20120305.so.136c2016000-36c2215000 ---p 00016000 08:03 824759                         /lib64/libgcc_s-4.4.6-20120305.so.136c2215000-36c2216000 rw-p 00015000 08:03 824759                         /lib64/libgcc_s-4.4.6-20120305.so.136c5000000-36c50e8000 r-xp 00000000 08:03 270925                         /usr/lib64/libstdc++.so.6.0.1336c50e8000-36c52e8000 ---p 000e8000 08:03 270925                         /usr/lib64/libstdc++.so.6.0.1336c52e8000-36c52ef000 r--p 000e8000 08:03 270925                         /usr/lib64/libstdc++.so.6.0.1336c52ef000-36c52f1000 rw-p 000ef000 08:03 270925                         /usr/lib64/libstdc++.so.6.0.1336c52f1000-36c5306000 rw-p 00000000 00:00 0 36c9400000-36c945d000 r-xp 00000000 08:03 824769                         /lib64/libfreebl3.so36c945d000-36c965c000 ---p 0005d000 08:03 824769                         /lib64/libfreebl3.so36c965c000-36c965d000 r--p 0005c000 08:03 824769                         /lib64/libfreebl3.so36c965d000-36c965e000 rw-p 0005d000 08:03 824769                         /lib64/libfreebl3.so36c965e000-36c9662000 rw-p 00000000 00:00 0 36c9c00000-36c9c07000 r-xp 00000000 08:03 824770                         /lib64/libcrypt-2.12.so36c9c07000-36c9e07000 ---p 00007000 08:03 824770                         /lib64/libcrypt-2.12.so36c9e07000-36c9e08000 r--p 00007000 08:03 824770                         /lib64/libcrypt-2.12.so36c9e08000-36c9e09000 rw-p 00008000 08:03 824770                         /lib64/libcrypt-2.12.so36c9e09000-36c9e37000 rw-p 00000000 00:00 0 7ff00c000000-7ff00c05f000 rw-p 00000000 00:00 0 7ff00c05f000-7ff010000000 ---p 00000000 00:00 0 7ff014000000-7ff014021000 rw-p 00000000 00:00 0 7ff014021000-7ff018000000 ---p 00000000 00:00 0 7ff018000000-7ff018021000 rw-p 00000000 00:00 0 7ff018021000-7ff01c000000 ---p 00000000 00:00 0 7ff01c000000-7ff01c021000 rw-p 00000000 00:00 0 7ff01c021000-7ff020000000 ---p 00000000 00:00 0 7ff020dfb000-7ff020dfc000 ---p 00000000 00:00 0 7ff020dfc000-7ff0217fc000 rw-p 00000000 00:00 0 7ff0217fc000-7ff0217fd000 ---p 00000000 00:00 0 7ff0217fd000-7ff0221fd000 rw-p 00000000 00:00 0 7ff0221fd000-7ff0221fe000 ---p 00000000 00:00 0 7ff0221fe000-7ff022bfe000 rw-p 00000000 00:00 0 7ff022bfe000-7ff022bff000 ---p 00000000 00:00 0 7ff022bff000-7ff0235ff000 rw-p 00000000 00:00 0 7ff0235ff000-7ff023600000 ---p 00000000 00:00 0 7ff023600000-7ff024000000 rw-p 00000000 00:00 0 7ff024000000-7ff024085000 rw-p 00000000 00:00 0 7ff024085000-7ff028000000 ---p 00000000 00:00 0 7ff028109000-7ff02810a000 ---p 00000000 00:00 0 7ff02810a000-7ff028b0a000 rw-p 00000000 00:00 0 7ff028b0a000-7ff028b0b000 ---p 00000000 00:00 0 7ff028b0b000-7ff02950b000 rw-p 00000000 00:00 0 7ff02950b000-7ff02950c000 ---p 00000000 00:00 0 7ff02950c000-7ff029f0d000 rw-p 00000000 00:00 0 7ff029f0d000-7ff029f0e000 ---p 00000000 00:00 0 7ff029f0e000-7ff02ab13000 rw-p 00000000 00:00 0 7ff02ab19000-7ff02ab1a000 ---p 00000000 00:00 0 7ff02ab1a000-7ff02ab5a000 rw-p 00000000 00:00 0 7ff02ab5a000-7ff02ab5b000 r-xp 00000000 08:03 394342                     /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/lib/plugin/test_add.so7ff02ab5b000-7ff02ad5a000 ---p 00001000 08:03 394342                     /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/lib/plugin/test_add.so7ff02ad5a000-7ff02ad5b000 rw-p 00000000 08:03 394342                     /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/lib/plugin/test_add.so7ff02ad5b000-7ff02ad5c000 ---p 00000000 00:00 0 7ff02ad5c000-7ff02ad9c000 rw-p 00000000 00:00 0 7ff02ad9c000-7ff02ad9d000 ---p 00000000 00:00 0 7ff02ad9d000-7ff02b79d000 rw-p 00000000 00:00 0 7ff02b79d000-7ff02b79e000 ---p 00000000 00:00 0 7ff02b79e000-7ff02c19e000 rw-p 00000000 00:00 0 7ff02c19e000-7ff02c19f000 ---p 00000000 00:00 0 7ff02c19f000-7ff02cb9f000 rw-p 00000000 00:00 0 7ff02cb9f000-7ff02cba0000 ---p 00000000 00:00 0 7ff02cba0000-7ff02d5a0000 rw-p 00000000 00:00 0 7ff02d5a0000-7ff02d5a1000 ---p 00000000 00:00 0 7ff02d5a1000-7ff02dfa1000 rw-p 00000000 00:00 0 7ff02dfa1000-7ff02dfa2000 ---p 00000000 00:00 0 7ff02dfa2000-7ff02e9a2000 rw-p 00000000 00:00 0 7ff02e9a2000-7ff02e9a3000 ---p 00000000 00:00 0 7ff02e9a3000-7ff02f3a3000 rw-p 00000000 00:00 0 7ff02f3a3000-7ff02f3a4000 ---p 00000000 00:00 0 7ff02f3a4000-7ff02fda4000 rw-p 00000000 00:00 0 7ff02fda4000-7ff02fda5000 ---p 00000000 00:00 0 7ff02fda5000-7ff0307a5000 rw-p 00000000 00:00 0 7ff0307a5000-7ff0307a6000 ---p 00000000 00:00 0 7ff0307a6000-7ff033c5b000 rw-p 00000000 00:00 0 7ff033c5b000-7ff033c60000 r-xp 00000000 08:03 423032                     /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/lib/plugin/validate_password.so7ff033c60000-7ff033e60000 ---p 00005000 08:03 423032                     /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/lib/plugin/validate_password.so7ff033e60000-7ff033e61000 rw-p 00005000 08:03 423032                     /usr/local/mysql-5.6.27-linux-glibc2.5-x86_64/lib/plugin/validate_password.so7ff033e61000-7ff03475e000 rw-p 00000000 00:00 0 7ff03475e000-7ff03476a000 r-xp 00000000 08:03 786463                     /lib64/libnss_files-2.12.so7ff03476a000-7ff03496a000 ---p 0000c000 08:03 786463                     /lib64/libnss_files-2.12.so7ff03496a000-7ff03496b000 r--p 0000c000 08:03 786463                     /lib64/libnss_files-2.12.so7ff03496b000-7ff03496c000 rw-p 0000d000 08:03 786463                     /lib64/libnss_files-2.12.so7ff03498c000-7ff04dd0f000 rw-p 00000000 00:00 0 7ff04dd0f000-7ff04dd92000 r-xp 00000000 08:03 824757                     /lib64/libm-2.12.so7ff04dd92000-7ff04df91000 ---p 00083000 08:03 824757                     /lib64/libm-2.12.so7ff04df91000-7ff04df92000 r--p 00082000 08:03 824757                     /lib64/libm-2.12.so7ff04df92000-7ff04df93000 rw-p 00083000 08:03 824757                     /lib64/libm-2.12.so7ff04df93000-7ff04df95000 rw-p 00000000 00:00 0 7ff04dfaf000-7ff04dfb6000 rw-p 00000000 00:00 0 7fff0b6de000-7fff0b6f3000 rw-p 00000000 00:00 0                          [stack]7fff0b750000-7fff0b751000 r-xp 00000000 00:00 0                          [vdso]ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

 

转载于:https://www.cnblogs.com/zengkefu/p/5635350.html

你可能感兴趣的文章
环境配置
查看>>
Linux基础学习
查看>>
SQL Server数据库的存储过程中定义的临时表,真的有必要显式删除(drop table #tableName)吗?...
查看>>
Android 常见的工具类
查看>>
Linux的系统级性能剖析工具-perf
查看>>
改变FileUpload文件上传控件的显示方式,确认后上传
查看>>
二维码Java和Jquery生成方式
查看>>
分支结构
查看>>
Servlet 总结
查看>>
kotlin学习一:kotlin简介
查看>>
virut详细分析
查看>>
单片机八位时钟
查看>>
c# webrower 页面跳转事件
查看>>
后缀数组一·重复旋律
查看>>
基于jquery后台框架设计(自适应高度,iframe简易版)
查看>>
零门槛,包教会。让你在5分钟内使用以太坊ERC20智能合约发行属于自己的空气币...
查看>>
shimano 型号详解 (zz)
查看>>
PUSH的整体设计
查看>>
一个去除内容空白的正则
查看>>
小白:virtualBOx安装Ubuntu16.04->初始化->镜像备份
查看>>