一、下载 lib_mysqludf_sys:

下载地址:https://github.com/mysqludf/repositories

二、配置与使用:

1、解压之后,已经有了我们需要的 lib_mysqludf_sys.so 文件,不过默认是32位的,所以最好自己重新编译一下:

gcc -Wall -fPIC -I/usr/include/mysql -I. -shared lib_mysqludf_sys.c -o lib_mysqludf_sys.so

2、把 lib_mysqludf_sys.so 复制到 /usr/local/mysql/lib/plugin/ 文件中

3、打开 mysql 客户端,执行以下 sql 语句:

DROP FUNCTION IF EXISTS lib_mysqludf_sys_info;
DROP FUNCTION IF EXISTS sys_get;
DROP FUNCTION IF EXISTS sys_set;
DROP FUNCTION IF EXISTS sys_exec;
DROP FUNCTION IF EXISTS sys_eval; CREATE FUNCTION lib_mysqludf_sys_info RETURNS STRING SONAME 'lib_mysqludf_sys.so';
CREATE FUNCTION sys_get RETURNS STRING SONAME 'lib_mysqludf_sys.so';
CREATE FUNCTION sys_set RETURNS INT SONAME 'lib_mysqludf_sys.so';
CREATE FUNCTION sys_exec RETURNS INT SONAME 'lib_mysqludf_sys.so';
CREATE FUNCTION sys_eval RETURNS STRING SONAME 'lib_mysqludf_sys.so';

4、找到要操作的库,新建触发器:

DELIMITER $$

USE `sailing`$$

DROP TRIGGER /*!50032 IF EXISTS */ `pay_check_trig`$$

CREATE
/*!50017 DEFINER = 'root'@'%' */
TRIGGER `pay_check_trig` AFTER INSERT ON `pay_record`
FOR EACH ROW BEGIN
DECLARE ret INT DEFAULT 999;
IF new.status = 1 THEN
SET ret = sys_exec(CONCAT('/mysql_udf/pay_ok.py ',new.playerId,' ',new.value));
END IF;
END;
$$ DELIMITER ;

5、在 linux 的 /mysql_udf/pay_ok.py 里写上如下代码:

#!/usr/bin/python
#coding:utf-8
import sys
import smtplib
from email.mime.text import MIMEText mailto_list=['123456789@qq.com']
mail_host="smtp.163.com" #设置服务器
mail_user="888888888@163.com" #用户名
mail_pass="" #口令
mail_postfix="163.com" #发件箱的后缀 def send_mail(to_list,sub,content):
me=u"sailing server"+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content,_subtype='plain',_charset='gb2312')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
server = smtplib.SMTP()
server.connect(mail_host)
server.login(mail_user,mail_pass)
server.sendmail(me, to_list, msg.as_string())
server.close()
return True
except Exception, e:
print str(e)
print 'error'
return False if __name__ == '__main__':
playerid = sys.argv[1]
value = sys.argv[2]
send_mail(mailto_list,u"成功充值通知",u"玩家ID:%s 刚刚成功购买了 %s 个金币!" % (playerid,value))

6、向相应表中插入一条数据,测试一下。

三、关于其它 mysqludf 相关学习

更多资料,参考:http://www.taki.com.tw/blog/mysql-udf/