一.安裝 wget https://launchpad.net/libmemcached/1.0/0.34/+download/libmemcached-0.34.tar.gz yum install gcc44 gcc44-c++ libstdc++44-devel export CC=/usr/bin/gcc44 export CXX=/usr/bin/g++44 ./configure --prefix=/soft/libmemcached -disable-64bit CFLAGS="-O3 -march=i686" \ --with-memcached=/root/libmemcached-1.0.7/memcached make && make install wget https://launchpad.net/memcached-udfs/trunk/1.1/+download/memcached_functions_mysql-1.1.tar.gz ./configure --prefix=/soft/UDFS/memcache_mysql \ --with-mysql=/soft/mysql/bin/mysql_config \ --libdir=/soft/mysql/lib/plugin \ --with-libmemcached=/soft/UDFS/libmemcached make && make install mysql -uroot -pmysql < /sql/install_functions.sql mysql -uroot -pmysql -se "select name,dl from mysql.func" select memc_servers_set('127.0.0.1:11211');//若是mysql restart,須要從新運行這句以創建與memcached之間的關係 select memc_server_count(); select memc_set('urls:sequence', 0); select memc_list_behaviors()\G//修改memcached參數的行爲 select memc_servers_behavior_set('MEMCACHED_BEHAVIOR_NO_BLOCK','1'); select memc_servers_behavior_set('MEMCACHED_BEHAVIOR_TCP_NODELAY','1'); //設置MEMCACHED_BEHAVIOR_NO_BLOCK爲打開狀態,這樣在memcached出現問題時(不能鏈接時)數據繼續插入到 mysql中,報錯提示,若是不設置此值,若是memcached失敗,mysql須要等到timeout才能夠插入到表中。 二.測試: drop table if exists urls; create table urls ( id int(3) not null auto_increment, url varchar(64) not null default '', primary key (id) ); select memc_servers_set('localhost:11211'); select memc_set('urls:sequence', 0); DELIMITER | DROP TRIGGER IF EXISTS url_mem_insert | CREATE TRIGGER url_mem_insert BEFORE INSERT ON urls FOR EACH ROW BEGIN SET NEW.id= memc_increment('urls:sequence'); SET @mm= memc_set(NEW.id, NEW.url); END | DROP TRIGGER IF EXISTS url_mem_update | CREATE TRIGGER url_mem_update BEFORE UPDATE ON urls FOR EACH ROW BEGIN SET @mm= memc_replace(OLD.id, NEW.url); END | DROP TRIGGER IF EXISTS url_mem_delete | CREATE TRIGGER url_mem_delete BEFORE DELETE ON urls FOR EACH ROW BEGIN SET @mm= memc_delete(OLD.id); END | DELIMITER ; insert into urls (url) value ('http://google.com'); insert into urls (url) value ('http://lycos.com/'); insert into urls (url) value ('http://tripod.com/'); insert into urls (url) value ('http://microsoft.com/'); insert into urls (url) value('http://slashdot.org'); insert into urls (url) value ('http://mysql.com'); select * from urls; select memc_get('urls:1'); select memc_get('urls:2'); select memc_get('urls:3'); select memc_get('urls:4'); select memc_get('urls:5'); select memc_get('urls:6'); update urls set url= 'http://mysql.com/sun' where url = 'http://mysql.com'; select url from urls where url = 'http://mysql.com/sun'; select memc_get('urls:6'); delete from urls where url = 'http://microsoft.com/'; select * from urls where url='http://microsoft.com/'; select memc_get('urls:4');