1、測試環境在Ubuntu kylin 14.04 64bit 已經安裝Mysql、Redis、php、lib_mysqludf_json.so、Gearman。 點擊這裏查看測試數據庫及表參考 本文也有些基本操做,在以前文章裏有介紹。 1、安裝 安裝gearman-mysql-udf apt-get install libgearman-dev wget https://launchpad.net/gearman-mysql-udf/trunk/0.6/+download/gearman-mysql-udf-0.6.tar.gz tar -xzf gearman-mysql-udf-0.6.tar.gz cd gearman-mysql-udf-0.6 ./configure --with-mysql=/usr/bin/mysql_config --libdir=/usr/lib/mysql/plugin/ make sudo make install 註冊UDF函數: CREATE FUNCTION gman_do_background RETURNS STRING SONAME 'libgearman_mysql_udf.so'; CREATE FUNCTION gman_servers_set RETURNS STRING SONAME 'libgearman_mysql_udf.so'; CREATE FUNCTION gman_do RETURNS STRING SONAME "libgearman_mysql_udf.so"; CREATE FUNCTION gman_do_high RETURNS STRING SONAME "libgearman_mysql_udf.so"; CREATE FUNCTION gman_do_low RETURNS STRING SONAME "libgearman_mysql_udf.so"; CREATE FUNCTION gman_do_background RETURNS STRING SONAME "libgearman_mysql_udf.so"; CREATE FUNCTION gman_do_high_background RETURNS STRING SONAME "libgearman_mysql_udf.so"; CREATE FUNCTION gman_do_low_background RETURNS STRING SONAME "libgearman_mysql_udf.so"; CREATE AGGREGATE FUNCTION gman_sum RETURNS INTEGER SONAME "libgearman_mysql_udf.so"; CREATE FUNCTION gman_servers_set RETURNS STRING SONAME "libgearman_mysql_udf.so"; 指定Gearman服務器的信息: SELECT gman_servers_set('127.0.0.1:4730'); 使用示例: 參照http://blog.csdn.net/xundh/article/details/46287681 創建一個reverse.php的worker <?php $worker= new GearmanWorker(); $worker->addServer(); $worker->addFunction("reverse", "my_reverse_function"); while ($worker->work()); function my_reverse_function($job){ return strrev($job->workload()); } ?> 輸入命令php reverse.php運行。 到mysql裏,輸入: SELECT gman_do("reverse",'abcdef') AS test FROM Users; ---FROM Users也能夠不帶。 這裏寫圖片描述 SELECT gman_do("reverse", password) AS test FROM Users; 能夠看到輸出結果,其中password列已經被reverse的worker處理,mysql這時充當client端: 這裏寫圖片描述 還能夠輸入如下命令測試: SELECT gman_do_high("reverse", password) AS test FROM Users; --高優先權 這裏寫圖片描述 SELECT gman_do_background("reverse", password) AS test FROM Users; --後臺低優先權,返回主機和做業號。 這裏寫圖片描述 建立syncToRedis做業 中止前面的reverse worker,創建一個syncToRedis.php <?php ini_set('default_socket_timeout', -1); //很重要,不然可能報RedisException with message read error on connection ... $worker = new GearmanWorker(); $worker->addServer(); $worker->addFunction('syncToRedis', 'syncToRedis'); $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo("begin:\n"); while($worker->work()); function syncToRedis($job) { global $redis; $workString = $job->workload(); $work = json_decode($workString); echo('get value:'); echo($workString); echo("\n"); echo('json_decode:'); var_dump($work); echo("\n"); if(!isset($work->user_id)){ return false; } $redis->set($work->user_id, $workString); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 在mysql裏測試一下: SELECT gman_do("syncToRedis", json_object(user_id as user_id,password as password)) AS test FROM Users; 這裏寫圖片描述 若是redis監控是打開的,能夠看到redis已經收到了數據: 這裏寫圖片描述 redis查詢結果 這裏寫圖片描述 2、創建觸發器 DELIMITER $$ CREATE TRIGGER datatoredis AFTER UPDATE ON Users FOR EACH ROW BEGIN SET @ret=gman_do_background('syncToRedis', json_object(NEW.user_id as `user_id`, NEW.email as `email`,NEW.display_name as `display_name`,NEW.password as `password`)); END$$ DELIMITER ; 執行SQL語句測試: insert into Users values('8','new','3','hello'); update Users set email='new8@qq.com' where user_id=8; 這裏寫圖片描述 正常使用時,能夠把worker使用&設置爲後臺任務: nohup php syncToReids.php & 其它問題 有的時候,命令行執行php語句時可能會提示下面的錯誤, [root@iZ230296jm2Z redis]# php syncToRedis.php PHP Fatal error: Class ‘Redis’ not found in /home/wwwroot/default/youai/php/redis/syncToRedis.php on line 6 處理方法: 運行php命令和apache或ngnix可能使用了不一樣的php.ini,即便在php.ini裏看到加載了redis,還須要在php命令行使用php.ini裏也增長Redis模塊。 查看ngnix使用的php.ini位置使用