如题所述
ã对äºååé¢çé常快çæ°æ®æ¥è¯´ï¼å¦æè¿éæ©ä¼ ç»çéæç¼åæ¹å¼ï¼MemocachedãFile Systemçï¼å±ç¤ºæ°æ®ï¼å¯è½å¨ç¼åçååä¸ä¼æå¾å¤§çå¼éï¼å¹¶ä¸è½å¾å¥½ç满足éè¦ï¼èRedisè¿æ ·åºäºå
åçNoSQLæ°æ®åºï¼å°±é常éåæ
ä»»å®æ¶æ°æ®ç容å¨ã
ããä½æ¯å¾å¾åææ°æ®å¯é æ§çéæ±ï¼éç¨MySQLä½ä¸ºæ°æ®åå¨ï¼ä¸ä¼å 为å åé®é¢èå¼èµ·æ°æ®ä¸¢å¤±ï¼åæ¶ä¹å¯ä»¥å©ç¨å ³ç³»æ°æ®åºçç¹æ§å®ç°å¾å¤åè½ã
ããæ以就ä¼å¾èªç¶çæ³å°æ¯å¦å¯ä»¥éç¨MySQLä½ä¸ºæ°æ®åå¨å¼æï¼Redisåä½ä¸ºCacheãèè¿ç§éæ±ç®åè¿æ²¡æçå°æç¹å«æçç解å³æ¹æ¡æå·¥å ·ï¼å æ¤éç¨Gearman+PHP+MySQL UDFçç»åå¼æ¥å®ç°MySQLå°Redisçæ°æ®å¤å¶ã
ããMySQLå°Redisæ°æ®å¤å¶æ¹æ¡
ããæ 论MySQLè¿æ¯Redisï¼èªèº«é½å¸¦ææ°æ®åæ¥çæºå¶ï¼æ¯è¾å¸¸ç¨çMySQLçMaster/Slave模å¼ï¼å°±æ¯ç±Slave端åæMasterçbinlogæ¥å®ç°çï¼è¿æ ·çæ°æ®å¤å¶å ¶å®è¿æ¯ä¸ä¸ªå¼æ¥è¿ç¨ï¼åªä¸è¿å½æå¡å¨é½å¨åä¸å ç½æ¶ï¼å¼æ¥ç延è¿å ä¹å¯ä»¥å¿½ç¥ã
ããé£ä¹ç论ä¸ä¹å¯ä»¥ç¨åæ ·æ¹å¼ï¼åæMySQLçbinlogæ件并å°æ°æ®æå ¥Redisãä½æ¯è¿éè¦å¯¹binlogæ件以åMySQLæéå¸¸æ·±å ¥çç解ï¼åæ¶ç±äºbinlogåå¨Statement/Row/Mixedlevelå¤ç§å½¢å¼ï¼åæbinlogå®ç°åæ¥çå·¥ä½éæ¯é常大çã
ããå æ¤è¿ééæ©äºä¸ç§å¼åææ¬æ´å ä½å»çæ¹å¼ï¼åç¨å·²ç»æ¯è¾æççMySQL UDFï¼å°MySQLæ°æ®é¦å æ¾å ¥Gearmanä¸ï¼ç¶åéè¿ä¸ä¸ªèªå·±ç¼åçPHP Gearman Workerï¼å°æ°æ®åæ¥å°Redisãæ¯åæbinlogçæ¹å¼å¢å äºä¸å°æµç¨ï¼ä½æ¯å®ç°ææ¬æ´ä½ï¼æ´å®¹ææä½ã
ããGearmançå®è£ ä¸ä½¿ç¨
ããGearmanæ¯ä¸ä¸ªæ¯æåå¸å¼çä»»å¡ååæ¡æ¶ã设计ç®æ´ï¼è·å¾äºé常广æ³çæ¯æãä¸ä¸ªå ¸åçGearmanåºç¨å æ¬ä»¥ä¸è¿äºé¨åï¼
ããGearman Job Serverï¼Gearmanæ ¸å¿ç¨åºï¼éè¦ç¼è¯å®è£ 并以å®æ¤è¿ç¨å½¢å¼è¿è¡å¨åå°
ããGearman Clientï¼å¯ä»¥ç解为任å¡çæ¶ä»¶åï¼æ¯å¦å¨åå°æ§è¡ä¸ä¸ªåéé®ä»¶çä»»å¡ï¼å¯ä»¥å¨ç¨åºä¸è°ç¨ä¸ä¸ªGearman Clientå¹¶ä¼ å ¥é®ä»¶çä¿¡æ¯ï¼ç¶åå°±å¯ä»¥å°æ§è¡ç»æç«å³å±ç¤ºç»ç¨æ·ï¼èä»»å¡æ¬èº«ä¼æ ¢æ ¢å¨åå°è¿è¡ã
ããGearman Workerï¼ä»»å¡ççæ£æ§è¡è ï¼ä¸è¬éè¦èªå·±ç¼åå ·ä½é»è¾å¹¶éè¿å®æ¤è¿ç¨æ¹å¼è¿è¡ï¼Gearman Workeræ¥æ¶å°Gearman Clientä¼ éçä»»å¡å 容åï¼ä¼æ顺åºå¤çã
ãã以åæ¾ç»ä»ç»è¿ç±»ä¼¼çåå°ä»»å¡å¤ç项ç®Resqueã两è çè®¾è®¡å ¶å®é常æ¥è¿ï¼ç®åå¯ä»¥ç±»æ¯ä¸ºï¼
ããGearman Job Serverï¼å¯¹åºResqueçRedisé¨å
ããGearman Clientï¼å¯¹åºResqueçQueueæä½
ããGearman Workerï¼å¯¹åºResqueçWorkeråJob
ããè¿éä¹æ以éæ©Gearmanèä¸æ¯Resqueæ¯å 为Gearmanæä¾äºæ¯è¾å¥½ç¨çMySQL UDFï¼å·¥ä½éæ´å°ã
ããå®è£ GearmanåPHP Gearmanæ©å±
ãã以ä¸å以Ubuntu12.04为ä¾ã
ããapt-get install gearman gearman-server libgearman-dev
ããæ£æ¥Gearmançè¿è¡ç¶åµï¼
ãã/etc/init.d/gearman-job-server status
ãã* gearmand is running
ãã说æGearmanå·²ç»å®è£ æåã
ããPHPçGearmanæ©å±å¯ä»¥éè¿peclç´æ¥å®è£
ããpecl install gearman
ããecho "extension=gearman.so">/etc/php5/conf.d/gearman.ini
ããservice php5-fpm restart
ããä½æ¯å®æµåç°ubuntué»è®¤å®è£ çgearmançæ¬è¿ä½ï¼ç´æ¥è¿è¡pecl install gearmanä¼æ¥é
ããconfigure: error: libgearman version 1.1.0or later required
ããå æ¤Gearman + PHPæ©å±å»ºè®®éè¿ç¼è¯æ¹å¼å®è£ ï¼è¿é为äºç®å说æï¼éæ©å®è£ æ§çæ¬æ©å±ï¼
ããpecl install gearman-1.0.3
ããGearman + PHPå®ä¾
ãã为äºæ´å®¹æç解åæGearmançè¿è¡æµç¨ï¼è¿éä¸å¦¨ä»ä¸ä¸ªæç®åçGearmanå®ä¾æ¥è¯´æï¼æ¯å¦è¦è¿è¡ä¸ä¸ªæ件å¤ççæä½ï¼é¦å ç¼åä¸ä¸ªGearman Client并å½å为client.phpï¼
ãã<?php
ãã$client =newGearmanClient();
ãã$client->addServer();
ãã$client->doBackground('writeLog','Log content');
echo 'æ件已ç»å¨åå°æä½';
ããè¿è¡è¿ä¸ªæ件ï¼ç¸å½äºæ¨¡æç¨æ·è¯·æ±ä¸ä¸ªWeb页é¢åï¼å°å¤çç»æçä¿¡æ¯è¿åç¨æ·ï¼
ããphp client.php
ããæ¥çä¸ä¸Gearmançç¶åµï¼
ãã(echo status ; sleep 0.1)| netcat127.0.0.14730
ããå¯ä»¥çå°è¾åºä¸º
ããwriteLog 100.
ãã说æå·²ç»å¨Gearmanä¸å»ºç«äºä¸ä¸ªå为writeLogçä»»å¡ï¼å¹¶ä¸æ1个任å¡å¨éåçå¾ ä¸ã
èä¸é¢ç4ååå«ä»£è¡¨å½åçGearmançè¿è¡ç¶æï¼
ããä»»å¡å称
ããå¨çå¾ éåä¸çä»»å¡
ããæ£å¨è¿è¡çä»»å¡
ããæ£å¨è¿è¡çWorkerè¿ç¨
ããå¯ä»¥ä½¿ç¨watchè¿è¡å®æ¶çæ§ï¼
ããwatch -n 1"(echo status; sleep 0.1) | nc 127.0.0.1 4730"
ããç¶åæ们éè¦ç¼åä¸ä¸ªGearman Workerå½å为worker.phpï¼
ãã<?php
ãã$worker =newGearmanWorker();
ãã$worker->addServer();
ãã$worker->addFunction('writeLog','writeLog');while($worker->work());function ããwriteLog($job){
$log = $job->workload();file_put_contents(__DIR__ .'/gearman.log', $log ."\n", ããFILE_APPEND | LOCK_EX);}
ããWorker使ç¨ä¸ä¸ªwhileæ»å¾ªç¯å®ç°å®æ¤è¿ç¨ï¼è¿è¡
ããphp worker.php
ããå¯ä»¥çå°Gearmanç¶æå为ï¼
ããwriteLog 001
ããåæ¶æ¥çåç®å½ä¸gearman.logï¼å 容åºä¸ºä»Clientä¼ å ¥çå¼Log contentã
ããéè¿MySQL UDF + Triggeråæ¥æ°æ®å°Gearman
ããMySQLè¦å®ç°ä¸å¤é¨ç¨åºäºéçæ好æ¹å¼è¿æ¯éè¿MySQL UDFï¼MySQL user defined functionsï¼æ¥å®ç°ã为äºè®©MySQLè½å°æ°æ®ä¼ å ¥Gearmanï¼è¿é使ç¨äºlib_mysqludf_jsonågearman-mysql-udfçç»åã
ããå®è£ lib_mysqludf_json
ãã使ç¨lib_mysqludf_jsonçåå æ¯å 为Gearmanåªæ¥åå符串ä½ä¸ºå ¥å£åæ°ï¼å¯ä»¥éè¿lib_mysqludf_jsonå°MySQLä¸çæ°æ®ç¼ç 为JSONå符串
ããapt-get install libmysqlclient-dev
ããwget https://github.com/mysqludf/lib_mysqludf_json/archive/master.zip
ããunzip master.zip
ããcd lib_mysqludf_json-master/
ããrm lib_mysqludf_json.so
ããgcc $(mysql_config --cflags)-shared -fPIC -o lib_mysqludf_json.so ããlib_mysqludf_json.c
ããå¯ä»¥çå°éæ°ç¼è¯çæäº lib_mysqludf_json.so æ件ï¼æ¤æ¶éè¦æ¥çMySQLçæ件å®è£ è·¯å¾ï¼
ããmysql -u root -pPASSWORD --execute="show variables like '%plugin%';"+---------------+------------------------+|Variable_name|Value|+---------------+------------------------+| plugin_dir |/usr/lib/mysql/plugin/|+---------------+------------------------+
ããç¶åå° lib_mysqludf_json.so æ件å¤å¶å°å¯¹åºä½ç½®ï¼
ããcp lib_mysqludf_json.so /usr/lib/mysql/plugin/
ããæåç»å ¥MySQLè¿è¡è¯å¥æ³¨åUDFå½æ°ï¼
ããCREATE FUNCTION json_object RETURNS STRING SONAME 'lib_mysqludf_json.so';
ããå®è£ 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 && make install
ããç»å ¥MySQLè¿è¡è¯å¥æ³¨å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';
ããæåæå®Gearmanæå¡å¨çä¿¡æ¯ï¼
ããSELECT gman_servers_set('127.0.0.1:4730');
ããéè¿MySQL触åå¨å®ç°æ°æ®åæ¥
æç»åæ¥åªäºæ°æ®ï¼åæ¥çæ¡ä»¶ï¼è¿æ¯éè¦æ ¹æ®å®é æ åµå³å®ï¼æ¯å¦å°æ°æ®è¡¨dataçæ°æ®å¨æ¯æ¬¡æ´æ°æ¶åæ¥ï¼é£ä¹ç¼åTriggerå¦ä¸ï¼
ããDELIMITER $$
ããCREATE TRIGGER datatoredis AFTER UPDATE ON data
ããFOR EACH ROW BEGIN
ããSET @ret=gman_do_background('syncToRedis', json_object(NEW.id as`id`, ããNEW.volume as`volume`));END$$
ããDELIMITER ;
ããå°è¯å¨æ°æ®åºä¸æ´æ°ä¸æ¡æ°æ®æ¥çGearmanæ¯å¦çæã
ããGearman PHP Workerå°MySQLæ°æ®å¼æ¥å¤å¶å°Redis
Redisä½ä¸ºæ¶ä¸å½ççNoSQLç¼å解å³æ¹æ¡æ éè¿å¤ä»ç»ï¼å ¶å®è£ å使ç¨ä¹é常ç®åï¼
ããapt-get install redis-server
ããpecl install redis
ããecho "extension=redis.so">/etc/php5/conf.d/redis.ini
ããç¶åç¼åä¸ä¸ªGearman Workerï¼redis_worker.php
ãã#!/usr/bin/env php<?
ãã$worker =newGearmanWorker();
ãã$worker->addServer();
ãã$worker->addFunction('syncToRedis','syncToRedis');
ãã$redis =newRedis();
ãã$redis->connect('127.0.0.1',6379);while($worker->work());function ããsyncToRedis($job){global $redis;
$workString = $job->workload();
$work = json_decode($workString);if(!isset($work->id)){returnfalse;}
$redis->set($work->id, $workString);}
ããæåéè¦å°Workerå¨åå°è¿è¡ï¼
ããnohup php redis_worker.php &
ããéè¿è¿ç§æ¹å¼å°MySQLæ°æ®å¤å¶å°Redisï¼ç»æµè¯åWorkeråºæ¬å¯ä»¥ç¬æ¶å®æã
ããä½æ¯å¾å¾åææ°æ®å¯é æ§çéæ±ï¼éç¨MySQLä½ä¸ºæ°æ®åå¨ï¼ä¸ä¼å 为å åé®é¢èå¼èµ·æ°æ®ä¸¢å¤±ï¼åæ¶ä¹å¯ä»¥å©ç¨å ³ç³»æ°æ®åºçç¹æ§å®ç°å¾å¤åè½ã
ããæ以就ä¼å¾èªç¶çæ³å°æ¯å¦å¯ä»¥éç¨MySQLä½ä¸ºæ°æ®åå¨å¼æï¼Redisåä½ä¸ºCacheãèè¿ç§éæ±ç®åè¿æ²¡æçå°æç¹å«æçç解å³æ¹æ¡æå·¥å ·ï¼å æ¤éç¨Gearman+PHP+MySQL UDFçç»åå¼æ¥å®ç°MySQLå°Redisçæ°æ®å¤å¶ã
ããMySQLå°Redisæ°æ®å¤å¶æ¹æ¡
ããæ 论MySQLè¿æ¯Redisï¼èªèº«é½å¸¦ææ°æ®åæ¥çæºå¶ï¼æ¯è¾å¸¸ç¨çMySQLçMaster/Slave模å¼ï¼å°±æ¯ç±Slave端åæMasterçbinlogæ¥å®ç°çï¼è¿æ ·çæ°æ®å¤å¶å ¶å®è¿æ¯ä¸ä¸ªå¼æ¥è¿ç¨ï¼åªä¸è¿å½æå¡å¨é½å¨åä¸å ç½æ¶ï¼å¼æ¥ç延è¿å ä¹å¯ä»¥å¿½ç¥ã
ããé£ä¹ç论ä¸ä¹å¯ä»¥ç¨åæ ·æ¹å¼ï¼åæMySQLçbinlogæ件并å°æ°æ®æå ¥Redisãä½æ¯è¿éè¦å¯¹binlogæ件以åMySQLæéå¸¸æ·±å ¥çç解ï¼åæ¶ç±äºbinlogåå¨Statement/Row/Mixedlevelå¤ç§å½¢å¼ï¼åæbinlogå®ç°åæ¥çå·¥ä½éæ¯é常大çã
ããå æ¤è¿ééæ©äºä¸ç§å¼åææ¬æ´å ä½å»çæ¹å¼ï¼åç¨å·²ç»æ¯è¾æççMySQL UDFï¼å°MySQLæ°æ®é¦å æ¾å ¥Gearmanä¸ï¼ç¶åéè¿ä¸ä¸ªèªå·±ç¼åçPHP Gearman Workerï¼å°æ°æ®åæ¥å°Redisãæ¯åæbinlogçæ¹å¼å¢å äºä¸å°æµç¨ï¼ä½æ¯å®ç°ææ¬æ´ä½ï¼æ´å®¹ææä½ã
ããGearmançå®è£ ä¸ä½¿ç¨
ããGearmanæ¯ä¸ä¸ªæ¯æåå¸å¼çä»»å¡ååæ¡æ¶ã设计ç®æ´ï¼è·å¾äºé常广æ³çæ¯æãä¸ä¸ªå ¸åçGearmanåºç¨å æ¬ä»¥ä¸è¿äºé¨åï¼
ããGearman Job Serverï¼Gearmanæ ¸å¿ç¨åºï¼éè¦ç¼è¯å®è£ 并以å®æ¤è¿ç¨å½¢å¼è¿è¡å¨åå°
ããGearman Clientï¼å¯ä»¥ç解为任å¡çæ¶ä»¶åï¼æ¯å¦å¨åå°æ§è¡ä¸ä¸ªåéé®ä»¶çä»»å¡ï¼å¯ä»¥å¨ç¨åºä¸è°ç¨ä¸ä¸ªGearman Clientå¹¶ä¼ å ¥é®ä»¶çä¿¡æ¯ï¼ç¶åå°±å¯ä»¥å°æ§è¡ç»æç«å³å±ç¤ºç»ç¨æ·ï¼èä»»å¡æ¬èº«ä¼æ ¢æ ¢å¨åå°è¿è¡ã
ããGearman Workerï¼ä»»å¡ççæ£æ§è¡è ï¼ä¸è¬éè¦èªå·±ç¼åå ·ä½é»è¾å¹¶éè¿å®æ¤è¿ç¨æ¹å¼è¿è¡ï¼Gearman Workeræ¥æ¶å°Gearman Clientä¼ éçä»»å¡å 容åï¼ä¼æ顺åºå¤çã
ãã以åæ¾ç»ä»ç»è¿ç±»ä¼¼çåå°ä»»å¡å¤ç项ç®Resqueã两è çè®¾è®¡å ¶å®é常æ¥è¿ï¼ç®åå¯ä»¥ç±»æ¯ä¸ºï¼
ããGearman Job Serverï¼å¯¹åºResqueçRedisé¨å
ããGearman Clientï¼å¯¹åºResqueçQueueæä½
ããGearman Workerï¼å¯¹åºResqueçWorkeråJob
ããè¿éä¹æ以éæ©Gearmanèä¸æ¯Resqueæ¯å 为Gearmanæä¾äºæ¯è¾å¥½ç¨çMySQL UDFï¼å·¥ä½éæ´å°ã
ããå®è£ GearmanåPHP Gearmanæ©å±
ãã以ä¸å以Ubuntu12.04为ä¾ã
ããapt-get install gearman gearman-server libgearman-dev
ããæ£æ¥Gearmançè¿è¡ç¶åµï¼
ãã/etc/init.d/gearman-job-server status
ãã* gearmand is running
ãã说æGearmanå·²ç»å®è£ æåã
ããPHPçGearmanæ©å±å¯ä»¥éè¿peclç´æ¥å®è£
ããpecl install gearman
ããecho "extension=gearman.so">/etc/php5/conf.d/gearman.ini
ããservice php5-fpm restart
ããä½æ¯å®æµåç°ubuntué»è®¤å®è£ çgearmançæ¬è¿ä½ï¼ç´æ¥è¿è¡pecl install gearmanä¼æ¥é
ããconfigure: error: libgearman version 1.1.0or later required
ããå æ¤Gearman + PHPæ©å±å»ºè®®éè¿ç¼è¯æ¹å¼å®è£ ï¼è¿é为äºç®å说æï¼éæ©å®è£ æ§çæ¬æ©å±ï¼
ããpecl install gearman-1.0.3
ããGearman + PHPå®ä¾
ãã为äºæ´å®¹æç解åæGearmançè¿è¡æµç¨ï¼è¿éä¸å¦¨ä»ä¸ä¸ªæç®åçGearmanå®ä¾æ¥è¯´æï¼æ¯å¦è¦è¿è¡ä¸ä¸ªæ件å¤ççæä½ï¼é¦å ç¼åä¸ä¸ªGearman Client并å½å为client.phpï¼
ãã<?php
ãã$client =newGearmanClient();
ãã$client->addServer();
ãã$client->doBackground('writeLog','Log content');
echo 'æ件已ç»å¨åå°æä½';
ããè¿è¡è¿ä¸ªæ件ï¼ç¸å½äºæ¨¡æç¨æ·è¯·æ±ä¸ä¸ªWeb页é¢åï¼å°å¤çç»æçä¿¡æ¯è¿åç¨æ·ï¼
ããphp client.php
ããæ¥çä¸ä¸Gearmançç¶åµï¼
ãã(echo status ; sleep 0.1)| netcat127.0.0.14730
ããå¯ä»¥çå°è¾åºä¸º
ããwriteLog 100.
ãã说æå·²ç»å¨Gearmanä¸å»ºç«äºä¸ä¸ªå为writeLogçä»»å¡ï¼å¹¶ä¸æ1个任å¡å¨éåçå¾ ä¸ã
èä¸é¢ç4ååå«ä»£è¡¨å½åçGearmançè¿è¡ç¶æï¼
ããä»»å¡å称
ããå¨çå¾ éåä¸çä»»å¡
ããæ£å¨è¿è¡çä»»å¡
ããæ£å¨è¿è¡çWorkerè¿ç¨
ããå¯ä»¥ä½¿ç¨watchè¿è¡å®æ¶çæ§ï¼
ããwatch -n 1"(echo status; sleep 0.1) | nc 127.0.0.1 4730"
ããç¶åæ们éè¦ç¼åä¸ä¸ªGearman Workerå½å为worker.phpï¼
ãã<?php
ãã$worker =newGearmanWorker();
ãã$worker->addServer();
ãã$worker->addFunction('writeLog','writeLog');while($worker->work());function ããwriteLog($job){
$log = $job->workload();file_put_contents(__DIR__ .'/gearman.log', $log ."\n", ããFILE_APPEND | LOCK_EX);}
ããWorker使ç¨ä¸ä¸ªwhileæ»å¾ªç¯å®ç°å®æ¤è¿ç¨ï¼è¿è¡
ããphp worker.php
ããå¯ä»¥çå°Gearmanç¶æå为ï¼
ããwriteLog 001
ããåæ¶æ¥çåç®å½ä¸gearman.logï¼å 容åºä¸ºä»Clientä¼ å ¥çå¼Log contentã
ããéè¿MySQL UDF + Triggeråæ¥æ°æ®å°Gearman
ããMySQLè¦å®ç°ä¸å¤é¨ç¨åºäºéçæ好æ¹å¼è¿æ¯éè¿MySQL UDFï¼MySQL user defined functionsï¼æ¥å®ç°ã为äºè®©MySQLè½å°æ°æ®ä¼ å ¥Gearmanï¼è¿é使ç¨äºlib_mysqludf_jsonågearman-mysql-udfçç»åã
ããå®è£ lib_mysqludf_json
ãã使ç¨lib_mysqludf_jsonçåå æ¯å 为Gearmanåªæ¥åå符串ä½ä¸ºå ¥å£åæ°ï¼å¯ä»¥éè¿lib_mysqludf_jsonå°MySQLä¸çæ°æ®ç¼ç 为JSONå符串
ããapt-get install libmysqlclient-dev
ããwget https://github.com/mysqludf/lib_mysqludf_json/archive/master.zip
ããunzip master.zip
ããcd lib_mysqludf_json-master/
ããrm lib_mysqludf_json.so
ããgcc $(mysql_config --cflags)-shared -fPIC -o lib_mysqludf_json.so ããlib_mysqludf_json.c
ããå¯ä»¥çå°éæ°ç¼è¯çæäº lib_mysqludf_json.so æ件ï¼æ¤æ¶éè¦æ¥çMySQLçæ件å®è£ è·¯å¾ï¼
ããmysql -u root -pPASSWORD --execute="show variables like '%plugin%';"+---------------+------------------------+|Variable_name|Value|+---------------+------------------------+| plugin_dir |/usr/lib/mysql/plugin/|+---------------+------------------------+
ããç¶åå° lib_mysqludf_json.so æ件å¤å¶å°å¯¹åºä½ç½®ï¼
ããcp lib_mysqludf_json.so /usr/lib/mysql/plugin/
ããæåç»å ¥MySQLè¿è¡è¯å¥æ³¨åUDFå½æ°ï¼
ããCREATE FUNCTION json_object RETURNS STRING SONAME 'lib_mysqludf_json.so';
ããå®è£ 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 && make install
ããç»å ¥MySQLè¿è¡è¯å¥æ³¨å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';
ããæåæå®Gearmanæå¡å¨çä¿¡æ¯ï¼
ããSELECT gman_servers_set('127.0.0.1:4730');
ããéè¿MySQL触åå¨å®ç°æ°æ®åæ¥
æç»åæ¥åªäºæ°æ®ï¼åæ¥çæ¡ä»¶ï¼è¿æ¯éè¦æ ¹æ®å®é æ åµå³å®ï¼æ¯å¦å°æ°æ®è¡¨dataçæ°æ®å¨æ¯æ¬¡æ´æ°æ¶åæ¥ï¼é£ä¹ç¼åTriggerå¦ä¸ï¼
ããDELIMITER $$
ããCREATE TRIGGER datatoredis AFTER UPDATE ON data
ããFOR EACH ROW BEGIN
ããSET @ret=gman_do_background('syncToRedis', json_object(NEW.id as`id`, ããNEW.volume as`volume`));END$$
ããDELIMITER ;
ããå°è¯å¨æ°æ®åºä¸æ´æ°ä¸æ¡æ°æ®æ¥çGearmanæ¯å¦çæã
ããGearman PHP Workerå°MySQLæ°æ®å¼æ¥å¤å¶å°Redis
Redisä½ä¸ºæ¶ä¸å½ççNoSQLç¼å解å³æ¹æ¡æ éè¿å¤ä»ç»ï¼å ¶å®è£ å使ç¨ä¹é常ç®åï¼
ããapt-get install redis-server
ããpecl install redis
ããecho "extension=redis.so">/etc/php5/conf.d/redis.ini
ããç¶åç¼åä¸ä¸ªGearman Workerï¼redis_worker.php
ãã#!/usr/bin/env php<?
ãã$worker =newGearmanWorker();
ãã$worker->addServer();
ãã$worker->addFunction('syncToRedis','syncToRedis');
ãã$redis =newRedis();
ãã$redis->connect('127.0.0.1',6379);while($worker->work());function ããsyncToRedis($job){global $redis;
$workString = $job->workload();
$work = json_decode($workString);if(!isset($work->id)){returnfalse;}
$redis->set($work->id, $workString);}
ããæåéè¦å°Workerå¨åå°è¿è¡ï¼
ããnohup php redis_worker.php &
ããéè¿è¿ç§æ¹å¼å°MySQLæ°æ®å¤å¶å°Redisï¼ç»æµè¯åWorkeråºæ¬å¯ä»¥ç¬æ¶å®æã
温馨提示:答案为网友推荐,仅供参考
第1个回答 2016-09-12
这个只能自己写脚本来处理 或者用消息队列