![gm](https://i-blog.csdnimg.cn/blog_migrate/96e7047edab53aec47c849b2fd6aa692.png)
当向表插入数据的时候,触发执行某些任务 安装gearman参加:
http://www.ttlsa.com/html/663.html 一.安装gearman-mysql-udf # apt-get install libmysql++-dev # wget
https://launchpad.net/gearman-mysql-udf/trunk/0.6/+download/gearman-mysql-udf-0.6.tar.gz # tar zxvf gearman-mysql-udf-0.6.tar.gz # ./configure --with-mysql=/usr/bin/mysql_config --libdir=/usr/lib/mysql/plugin # make # make install # mysql [codesyntax lang="sql"] mysql> CREATE FUNCTION gman_do RETURNS STRING SONAME "libgearman_mysql_udf.so"; mysql> CREATE FUNCTION gman_do_high RETURNS STRING SONAME "libgearman_mysql_udf.so"; mysql> CREATE FUNCTION gman_do_low RETURNS STRING SONAME "libgearman_mysql_udf.so"; mysql> CREATE FUNCTION gman_do_background RETURNS STRING SONAME "libgearman_mysql_udf.so"; mysql> CREATE FUNCTION gman_do_high_background RETURNS STRING SONAME "libgearman_mysql_udf.so"; mysql> CREATE FUNCTION gman_do_low_background RETURNS STRING SONAME "libgearman_mysql_udf.so"; mysql> CREATE AGGREGATE FUNCTION gman_sum RETURNS INTEGER SONAME "libgearman_mysql_udf.so"; mysql> CREATE FUNCTION gman_servers_set RETURNS STRING SONAME "libgearman_mysql_udf.so"; mysql> SELECT gman_servers_set("192.168.1.60:4730,192.168.1.60:4731") as gman_servers; //设置gearman server +-------------------------------------+ | gman_servers | +-------------------------------------+ | 192.168.1.60:4730,192.168.1.60:4731 | +-------------------------------------+ mysql> create table udf_test( -> id int unsigned auto_increment primary key, -> val varchar(20) not null); //新建表 mysql> create trigger sendmail before insert on udf_test for each row set @return=gman_do_background('MAIL','undef'); //创建触发器,当向表udf_test插入数据时候,执行任务。 [/codesyntax] # perl -MCPAN -e shell cpan> install Gearman::Worker //安装Gearman::Worker模块 cpan> install Mail::SendEasy //安装Mail::SendEasy模块 # vi WORKER_SENDMAIL.pl //创建worker任务 [codesyntax lang="perl"] use strict; use Mail::SendEasy ; use v5.10; use Gearman::Worker; my $worker=new Gearman::Worker; $worker->job_servers('192.168.1.60:4730'); $worker->register_function(MAIL=>\&sendmail); $worker->work while 1; sub sendmail{ my $job=shift; my $date=localtime; my $mail = new Mail::SendEasy( smtp => 'smtp.ttlsa.com' , user =>
'service@ttlsa.com' , pass => '******', ); print "$date\n"; my $status = $mail->send( from =>
'service@ttlsa.com' , from_title => 'ttlsa' , to =>
'xxxxx@qq.com' , subject => "MAIL Test $date" , msg => "$date" , html => "<b>test $date</b>" , ); if (!$status) { print $mail->error ;} } [/codesyntax] # perl WORKER_SENDMAIL.pl & # perl WORKER_SENDMAIL.pl & //在后台运行两个worker 二.测试 1.向udf_test表插入数据 mysql> insert into udf_test value ('','a'); 2.查看是否收到邮件
三.Gearman server信息 # telnet 192.168.1.60 4730 Trying 192.168.1.60... Connected to 192.168.1.60. Escape character is '^]'. status MAIL 0 0 2 说明:第一列worker名称;第二列jobs执行数量;第三列jobs队列数量;第四列可用worker数量
![gm](https://i-blog.csdnimg.cn/blog_migrate/96e7047edab53aec47c849b2fd6aa692.png)
转载于:https://my.oschina.net/766/blog/211040