MemCache 安装手册
wget http://launchpad.net/libmemcached/1.0/0.34/+download/libmemcached-0.34.tar.gz
wget http://memcached.googlecode.com/files/memcached-1.4.1.tar.gz
wget http://www.monkey.org/~provos/libevent-1.2.tar.gz
1、安装libevent
tar zxvf libevent-1.2.tar.gz
cd libevent-1.2
./configure -prefix=/usr/libevent
make
make install
2、安装memcache
tar -zxvf memcached-1.4.1.tar.gz
cd memcached-1.4.1
./configure -with-libevent=/usr/libevent/ -prefix=/usr/local/memcached
make
make install
3、安装libmemcached
wget http://launchpad.net/libmemcached/1.0/0.34/+download/libmemcached-0.34.tar.gz
tar xvzf libmemcached-0.34.tar.gz
cd libmemcached-0.34
./configure --prefix=/usr/local/libmemcached --with-memcached=/usr/local/memcached
./configure的时候,如果你的Memcached不是默认安装路径的话,会报
configure: error: “could not find memcached binary”
错,如下的类似的命令可以解决这个问题。
./configure --prefix=/usr/local/libmemcached --with-memcached=/usr/local/memcached/bin/memcached
make
make install
chmod 777 /etc/ld.so.conf
echo "/usr/local/libmemcached" >> /etc/ld.so.conf
ldconfig
4、安装memcached_functions_mysql
wget http://download.tangent.org/memcached_functions_mysql-0.9.tar.gz
http://patg.net/downloads/memcached_functions_mysql-0.9. tar.gz
tar zxvf memcached_functions_mysql-0.9.tar.gz
cd memcached_functions_mysql-0.9
./configure --prefix=/usr/local/memcache_mysql --with-mysql=/usr/local/mysql/bin/mysql_config --with-libmemcached=/usr/local/libmemcached
make; make install
cp /usr/local/memcache_mysql/lib/libmemcached_functions* /usr/local/mysql/lib/mysql/plugin
5)添加memcache UDF 函数
mysql> source sql/install_functions.sql
这样我们就可以使用mysql memcached UDF 了,我们可以通过下面语句查看是否已经正常安装
mysql> select * from mysql.func;
添加trigger,就是向memcache内insert,update,deletel等,参照:
1)memcached_functions_mysql-0.9/sql 目录下的trigger_fun.sql
2)或者文档:http://dev.mysql.com/doc/refman/5.1/en/ha-memcached-interfaces-mysqludf.html
如果懒得看就看我后边的例子
3 测试
/usr/local/memcached/bin/memcached -m 110m -u root -p 11211 -d -P -t 200 /var/run/memcached1.pid
64位会报以下的错误:
/usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-1.2.so.1: cannot open shared object file: No such file or directory
解决:ln -s /usr/libevent/lib/libevent-1.2.so.1 /usr/lib64/libevent-1.2.so.1
mysql> select memc_servers_set('127.0.0.1:11211')
注意:如果mysql restart,需要重新运行这句以建立与memcached之间的关系
mysql> select memc_servers_behavior_set('MEMCACHED_BEHAVIOR_NO_BLOCK','1');
+--------------------------------------------------------------+
| memc_servers_behavior_set('MEMCACHED_BEHAVIOR_NO_BLOCK','1') |
+--------------------------------------------------------------+
| 0 |
+--------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select memc_servers_behavior_set('MEMCACHED_BEHAVIOR_TCP_NODELAY','1');
+-----------------------------------------------------------------+
| memc_servers_behavior_set('MEMCACHED_BEHAVIOR_TCP_NODELAY','1') |
+-----------------------------------------------------------------+
| 0 |
+-----------------------------------------------------------------+
1 row in set (0.00 sec)
注意:设置MEMCACHED_BEHAVIOR_NO_BLOCK为打开状态,这样在memcached出现问题时(不能连接时)
数据继续插入到mysql中,报错提示,如果不设置此值,如果memcached失败,mysql需要等到timeout
才可以插入到表中。
mysql> use test;
Database changed
mysql> create table xxd (id int, value varchar(100));
mysql> create trigger xxdmmci after insert on xxd for each row set @tmp = memc_set(NEW.id, NEW.value);
mysql> create trigger xxdmmcu after update on xxd for each row set @tmp = memc_set(NEW.id, NEW.value);
mysql> create trigger xxdmmcd before delete on xxd for each row set @tmp = memc_delete(OLD.id);
mysql> insert into xxd values(1, 'xxd'),(2,'xxd79'),(3, 'buro79xxd');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select memc_get('2');
+----------------------------+
| memc_get('2') |
+----------------------------+
| xxd79 |
+----------------------------+
1 row in set (0.00 sec)
mysql> update xxd set value='xxd_new' where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select memc_get('1');
+----------------------------+
| memc_get('1') |
+----------------------------+
| xxd_new |
+----------------------------+
1 row in set (0.00 sec)
mysql> delete from xxd where id=1;
Query OK, 1 row affected (0.00 sec)
mysql> select memc_get('1');
+----------------------------+
| memc_get('1') |
+----------------------------+
| NULL |
+----------------------------+
1 row in set (0.00 sec)
mysql> exit
$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get 1
END
get 2
VALUE 2 0 5
xxd79
END
get 3
VALUE 3 0 9
buro79xxd
END
quit
Connection closed by foreign host.
--EOF--
(1). 下载安装 lib_mysqludf_json 修改版:
以下安装包适合32位Linux操作系统:
wget http://mysql-udf-http.googlecode.com/files/lib_mysqludf_json-i386.tar.gz
tar zxvf lib_mysqludf_json-i386.tar.gz
cd lib_mysqludf_json-i386/
# 如果你的MySQL安装路径不是/usr/local/webserver/mysql/,请修改以下路径。
cp -f lib_mysqludf_json.so /usr/local/webserver/mysql/lib/mysql/plugin/lib_mysqludf_json.so
cd ../
以下安装包适合64位Linux操作系统:
wget http://mysql-udf-http.googlecode.com/files/lib_mysqludf_json-x86_64.tar.gz
tar zxvf lib_mysqludf_json-x86_64.tar.gz
cd lib_mysqludf_json-x86_64/
# 如果你的MySQL安装路径不是/usr/local/webserver/mysql/,请修改以下路径。
cp -f lib_mysqludf_json.so /usr/local/mysql/lib/mysql/plugin/lib_mysqludf_json.so
cd ../
通过命令行登陆进入MySQL:
/usr/local/webserver/mysql/bin/mysql -S /tmp/mysql.sock
mysql>
create function lib_mysqludf_json_info returns string soname 'lib_mysqludf_json.so';
create function json_array returns string soname 'lib_mysqludf_json.so';
create function json_members returns string soname 'lib_mysqludf_json.so';
create function json_object returns string soname 'lib_mysqludf_json.so';
create function json_values returns string soname 'lib_mysqludf_json.so';
lib_mysqludf_json的详细用法请访问:http://www.mysqludf.org/lib_mysqludf_json/
(2). 创建测试表
mysql>
SET NAMES UTF8;
USE test;
CREATE TABLE IF NOT EXISTS `mytable` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`addtime` int(10) NOT NULL,
`title` varchar(255) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
SET NAMES UTF8;
USE test;
CREATE TABLE IF NOT EXISTS `mytable` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`addtime` int(10) NOT NULL,
`title` varchar(255) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
(3). 为测试表创建触发器:
mysql>
/* INSERT插入操作的触发器 */
DELIMITER |
DROP TRIGGER IF EXISTS mytable_insert;
CREATE TRIGGER mytable_insert
AFTER INSERT ON mytable
FOR EACH ROW BEGIN
SET @tt_json = (SELECT json_object(id,addtime,title) FROM mytable WHERE id = NEW.id LIMIT 1);
SET @tt_resu = (SELECT http_put(CONCAT('http://127.0.0.1:11211/', NEW.id), @tt_json));
END |
DELIMITER ;
/* UPDATE更新操作的触发器 */
DELIMITER |
DROP TRIGGER IF EXISTS mytable_update;
CREATE TRIGGER mytable_update
AFTER UPDATE ON mytable
FOR EACH ROW BEGIN
SET @tt_json = (SELECT json_object(id,addtime,title) FROM mytable WHERE id = OLD.id LIMIT 1);
SET @tt_resu = (SELECT http_put(CONCAT('http://127.0.0.1:11211/', OLD.id), @tt_json));
END |
DELIMITER ;
/* DELETE删除操作的触发器 */
DELIMITER |
DROP TRIGGER IF EXISTS mytable_delete;
CREATE TRIGGER mytable_delete
AFTER DELETE ON mytable
FOR EACH ROW BEGIN
SET @tt_resu = (SELECT http_delete(CONCAT('http://127.0.0.1:11211/', OLD.id)));
END |
DELIMITER ;
/* INSERT插入操作的触发器 */
DELIMITER |
DROP TRIGGER IF EXISTS mytable_insert;
CREATE TRIGGER mytable_insert
AFTER INSERT ON mytable
FOR EACH ROW BEGIN
SET @tt_json = (SELECT json_object(id,addtime,title) FROM mytable WHERE id = NEW.id LIMIT 1);
SET @tt_resu = (SELECT http_put(CONCAT('http://127.0.0.1:11211/', NEW.id), @tt_json));
END |
DELIMITER ;
/* UPDATE更新操作的触发器 */
DELIMITER |
DROP TRIGGER IF EXISTS mytable_update;
CREATE TRIGGER mytable_update
AFTER UPDATE ON mytable
FOR EACH ROW BEGIN
SET @tt_json = (SELECT json_object(id,addtime,title) FROM mytable WHERE id = OLD.id LIMIT 1);
SET @tt_resu = (SELECT http_put(CONCAT('http://1127.0.0.1:11211/', OLD.id), @tt_json));
END |
DELIMITER ;
/* DELETE删除操作的触发器 */
DELIMITER |
DROP TRIGGER IF EXISTS mytable_delete;
CREATE TRIGGER mytable_delete
AFTER DELETE ON mytable
FOR EACH ROW BEGIN
SET @tt_resu = (SELECT http_delete(CONCAT('http://127.0.0.1:11211/', OLD.id)));
END |
DELIMITER ;
(4). 将 MySQL 表和 Tokyo Tyrant 关联进行查询:
mysql>
SELECT id,addtime,title,http_get(CONCAT('http://127.0.0.1:11211/',id)) AS tt FROM mytable ORDER BY id DESC LIMIT 0,5;
SELECT id,addtime,title,http_get(CONCAT('http://127.0.0.1:11211/',id)) AS tt FROM mytable ORDER BY id DESC LIMIT 0,5;
--------------------------------------------------------------------------------
5. 如何删除mysql-udf-http UDF函数:
mysql>
drop function http_get;
drop function http_post;
drop function http_put;
drop function http_delete;
drop function http_get;
drop function http_post;
drop function http_put;
drop function http_delete;
以上为详细安装!!!
memcache.php源文件下载
http://livebookmark.net/memcachephp/memcachephp.zip
或
http://pecl.php.net/get/memcache-2.2.6.tgz
如果安装memcache.php则要apache首先要支持php
即apache+php
然后旋转apache里面就可以。
测试例子:
五,测试
例子1:
mysql> select memc_servers_set('127.0.0.1:11211')
注意:如果mysql restart,需要重新运行这句以建立与memcached之间的关系
mysql> select memc_servers_behavior_set('MEMCACHED_BEHAVIOR_NO_BLOCK','1');
+--------------------------------------------------------------+
| memc_servers_behavior_set('MEMCACHED_BEHAVIOR_NO_BLOCK','1') |
+--------------------------------------------------------------+
| 0 |
+--------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select memc_servers_behavior_set('MEMCACHED_BEHAVIOR_TCP_NODELAY','1');
+-----------------------------------------------------------------+
| memc_servers_behavior_set('MEMCACHED_BEHAVIOR_TCP_NODELAY','1') |
+-----------------------------------------------------------------+
| 0 |
+-----------------------------------------------------------------+
1 row in set (0.00 sec)
注意:设置MEMCACHED_BEHAVIOR_NO_BLOCK为打开状态,这样在memcached出现问题时(不能连接时)
数据继续插入到mysql中,报错提示,如果不设置此值,如果memcached失败,mysql需要等到timeout
才可以插入到表中。
mysql> use test;
Database changed
mysql> create table xxd (id int, value varchar(100));
mysql> create trigger xxdmmci after insert on xxd for each row set @tmp = memc_set(NEW.id, NEW.value);
mysql> create trigger xxdmmcu after update on xxd for each row set @tmp = memc_set(NEW.id, NEW.value);
mysql> create trigger xxdmmcd before delete on xxd for each row set @tmp = memc_delete(OLD.id);
mysql> insert into xxd values(1, 'xxd'),(2,'xxd79'),(3, 'buro79xxd');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select memc_get('2');
+----------------------------+
| memc_get('2') |
+----------------------------+
| xxd79 |
+----------------------------+
1 row in set (0.00 sec)
mysql> update xxd set value='xxd_new' where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select memc_get('1');
+----------------------------+
| memc_get('1') |
+----------------------------+
| xxd_new |
+----------------------------+
1 row in set (0.00 sec)
mysql> delete from xxd where id=1;
Query OK, 1 row affected (0.00 sec)
mysql> select memc_get('1');
+----------------------------+
| memc_get('1') |
+----------------------------+
| NULL |
+----------------------------+
1 row in set (0.00 sec)
mysql> exit
$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get 1
END
get 2
VALUE 2 0 5
xxd79
END
get 3
VALUE 3 0 9
buro79xxd
END
quit
Connection closed by foreign host.
telnet localhost 11211
//保存
set good 32 0 10
helloworld
STORED
//取回
gets good
VALUE good 32 10 10
helloworld
END
//替换
replace good 32 0 10
worldhello
STORED
get good
VALUE good 32 10
worldhello
END
//尾部添加
append good 32 0 5
after
STORED
get good
VALUE good 32 15
worldhelloafter
END
//头部添加
prepend good 32 0 6
before
STORED
get good
VALUE good 32 21
beforeworldhelloafter
END
//删除
delete good
DELETED
get good
END
delete good
NOT_FOUND
cas good 32 0 10 hel
helloworld
EXISTS
gets good
VALUE good 32 10 10
helloworld
END
cas bad 32 0 10 good
worldhello
NOT_FOUND
//统计
stats items
STAT items:1:number 1
STAT items:1:age 24
STAT items:1:evicted 0
STAT items:1:outofmemory 0
END
stats sizes
96 1
END
stats slabs
STAT 1:chunk_size 88
STAT 1:chunks_per_page 11915
STAT 1:total_pages 1
STAT 1:total_chunks 11915
STAT 1:used_chunks 11914
STAT 1:free_chunks 1
STAT 1:free_chunks_end 11913
STAT 2:chunk_size 112
STAT 2:chunks_per_page 9362
STAT 2:total_pages 1
STAT 2:total_chunks 9362
STAT 2:used_chunks 9361
STAT 2:free_chunks 1
STAT 2:free_chunks_end 9361
STAT 5:chunk_size 232
STAT 5:chunks_per_page 4519
STAT 5:total_pages 1
STAT 5:total_chunks 4519
STAT 5:used_chunks 4518
STAT 5:free_chunks 1
STAT 5:free_chunks_end 4518
STAT active_slabs 3
STAT total_malloced 3145472
END
stats items
STAT items:1:number 1
STAT items:1:age 1768
STAT items:1:evicted 0
STAT items:1:outofmemory 0
END
stats
STAT pid 18261
STAT uptime 528593
STAT time 1237277383
STAT version 1.2.6
STAT pointer_size 32
STAT rusage_user 0.004999
STAT rusage_system 0.015997
STAT curr_items 1
STAT total_items 2
STAT bytes 66
STAT curr_connections 2
STAT total_connections 13
STAT connection_structures 3
STAT cmd_get 11
STAT cmd_set 8
STAT get_hits 2
STAT get_misses 9
STAT evictions 0
STAT bytes_read 1342
STAT bytes_written 8752
STAT limit_maxbytes 134217728
STAT threads 1
END
使用usr/bin/perl /root/memcached-1.2.6/scripts/memcached-tool localhost:11211
output
# Item_Size Max_age 1MB_pages Count Full?
1 88 B 1531 s 1 1 no
2 112 B 0 s 1 0 no
5 232 B 0 s 1 0 no
# slab class编号
Item_Size Chunk大小
Max_age LRU内最旧的记录的生存时间
1MB_pages 分配给Slab的页数
Count Slab内的记录数
Full? Slab内是否含有空闲chunk