memcached安装
版本V1.0
时间2012-07-30
版权GPL
作者itnihao 邮箱 [email protected]
博客 http://itnihao.blog.51cto.com
如需重新发行,请注明以上信息,谢谢合作
- 第一部分 下载源码进行安装
- wget https://github.com/downloads/libevent/libevent/libevent-2.0.19-stable.tar.gz
- tar xvf libevent-2.0.19-stable.tar.gz
- cd libevent-2.0.19-stable
- ./configure
- make
- make install
- wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
- tar xvf memcached-1.4.13.tar.gz
- cd memcached-1.4.13
- ./configure
- make
- make install
- echo "/usr/local/lib" >> /etc/ld.so.conf
- ldconfig
- /usr/local/bin/memcached -m 32m -p 11211 -d -u root -P /var/run/memcached.pid -c 1024
- wget https://launchpad.net/libmemcached/1.0/0.34/+download/libmemcached-0.34.tar.gz
- tar xvf libmemcached-0.34.tar.gz
- cd libmemcached-0.34
- ./configure --with-memcached=/usr/local/bin/memcached
- i#./configure --prefix=/usr/local/libmemcached --with-memcached=/usr/local/bin/memcached
- make
- make install
- #wget https://launchpad.net/libmemcached/1.0/1.0.8/+download/libmemcached-1.0.8.tar.gz
- #wget https://launchpad.net/memcached-udfs/trunk/version-1.0/+download/memcached_functions_mysql-1.0.tar.gz
- wget https://launchpad.net/memcached-udfs/trunk/1.1/+download/memcached_functions_mysql-1.1.tar.gz
- tar xvf memcached_functions_mysql-1.1.tar.gz
- cd memcached_functions_mysql-1.1
- ./configure --with-mysql=/usr/local/mysql/bin/mysql_config --libdir=/usr/local/mysql/lib/mysql --with-libmemcached=/usr/local
- make
- make install
- cd utils/ #注意此处的my.cnf 必须为osocket=/var/lib/mysql/mysql.sock,否则,运行脚本会报错 #find /usr/ -name libmemcached_functions_mysql.so ln -s /usr/local/mysql/lib/mysql/libmemcached_functions_mysql.so /usr/local/mysql/lib/plugin/libmemcached_functions_mysql.so ./install.pl -s -u=root -p=itnihao
第二部分 在mysql进行设置memcached
- mysql> select name,dl from mysql.func;
- +------------------------------+---------------------------------+
- | name | dl |
- +------------------------------+---------------------------------+
- | memc_cas_by_key | libmemcached_functions_mysql.so |
- | memc_cas | libmemcached_functions_mysql.so |
- | memc_servers_set | libmemcached_functions_mysql.so |
- | memc_add | libmemcached_functions_mysql.so |
- | memc_libmemcached_version | libmemcached_functions_mysql.so |
- | memc_add_by_key | libmemcached_functions_mysql.so |
- | memc_server_count | libmemcached_functions_mysql.so |
- | memc_stat_get_keys | libmemcached_functions_mysql.so |
- | memc_append | libmemcached_functions_mysql.so |
- | memc_replace_by_key | libmemcached_functions_mysql.so |
- | memc_prepend | libmemcached_functions_mysql.so |
- | memc_behavior_get | libmemcached_functions_mysql.so |
- | memc_udf_version | libmemcached_functions_mysql.so |
- | memc_set_by_key | libmemcached_functions_mysql.so |
- | memc_get_by_key | libmemcached_functions_mysql.so |
- | memc_increment | libmemcached_functions_mysql.so |
- | memc_behavior_set | libmemcached_functions_mysql.so |
- | memc_stats | libmemcached_functions_mysql.so |
- | memc_list_distribution_types | libmemcached_functions_mysql.so |
- | memc_list_hash_types | libmemcached_functions_mysql.so |
- | memc_append_by_key | libmemcached_functions_mysql.so |
- | memc_servers_behavior_set | libmemcached_functions_mysql.so |
- | memc_replace | libmemcached_functions_mysql.so |
- | memc_set | libmemcached_functions_mysql.so |
- | memc_prepend_by_key | libmemcached_functions_mysql.so |
- | memc_get | libmemcached_functions_mysql.so |
- | memc_list_behaviors | libmemcached_functions_mysql.so |
- | memc_delete | libmemcached_functions_mysql.so |
- | memc_stat_get_value | libmemcached_functions_mysql.so |
- | memc_decrement | libmemcached_functions_mysql.so |
- | memc_delete_by_key | libmemcached_functions_mysql.so |
- | memc_servers_behavior_get | libmemcached_functions_mysql.so |
- +------------------------------+---------------------------------+
第三部分memcached的应用实例
- #对功能进行测试,源码包已经提供测试模块
- cat memcached_functions_mysql-1.1/sql/trigger_fun.sql
- mysql> use test
- drop table if exists urls;
- create table urls (
- id int(3) not null,
- url varchar(64) not null default '',
- primary key (id)
- );
- mysql> select memc_servers_set('localhost:11211');
- +-------------------------------------+
- | memc_servers_set('localhost:11211') |
- +-------------------------------------+
- | 0 |
- +-------------------------------------+
- mysql> select memc_set('urls:sequence', 0);
- +------------------------------+
- | memc_set('urls:sequence', 0) |
- +------------------------------+
- | 1 |
- +------------------------------+
- mysql>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(concat('urls:',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(concat('urls:',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(concat('urls:',OLD.id));
- END |
- DELIMITER ;
- insert into urls (url) values ('http://google.com');
- insert into urls (url) values ('http://lycos.com/');
- insert into urls (url) values ('http://tripod.com/');
- insert into urls (url) values ('http://microsoft.com/');
- insert into urls (url) values ('http://slashdot.org');
- insert into urls (url) values ('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');
- mysql> select memc_servers_set('192.168.16.21:11211');
- +-----------------------------------------+
- | memc_servers_set('192.168.16.21:11211') |
- +-----------------------------------------+
- | 0 |
- +-----------------------------------------+
- mysql> select memc_server_count();
- +---------------------+
- | memc_server_count() |
- +---------------------+
- | 1 |
- +---------------------+
- mysql> select memc_list_behaviors()\G
- *************************** 1. row ***************************
- memc_list_behaviors():
- MEMCACHED SERVER BEHAVIORS
- MEMCACHED_BEHAVIOR_SUPPORT_CAS
- MEMCACHED_BEHAVIOR_NO_BLOCK
- MEMCACHED_BEHAVIOR_TCP_NODELAY
- MEMCACHED_BEHAVIOR_HASH
- MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
- MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
- MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
- MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
- MEMCACHED_BEHAVIOR_KETAMA
- MEMCACHED_BEHAVIOR_POLL_TIMEOUT
- MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
- MEMCACHED_BEHAVIOR_DISTRIBUTION
- MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
- MEMCACHED_BEHAVIOR_USER_DATA
- MEMCACHED_BEHAVIOR_SORT_HOSTS
- MEMCACHED_BEHAVIOR_VERIFY_KEY
- MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
- MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
- MEMCACHED_BEHAVIOR_KETAMA_HASH
- MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
- MEMCACHED_BEHAVIOR_SND_TIMEOUT
- MEMCACHED_BEHAVIOR_RCV_TIMEOUT
- MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
- MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
- MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
- MEMCACHED_HASH_DEFAULT
- MEMCACHED_HASH_MD5
- MEMCA
- mysql> select memc_servers_behavior_set('MEMCACHED_BEHAVIOR_NO_BLOCK','1');
- +--------------------------------------------------------------+
- | memc_servers_behavior_set('MEMCACHED_BEHAVIOR_NO_BLOCK','1') |
- +--------------------------------------------------------------+
- | 0 |
- +--------------------------------------------------------------+
- mysql> select memc_servers_behavior_set('MEMCACHED_BEHAVIOR_TCP_NODELAY','1');
- +-----------------------------------------------------------------+
- | memc_servers_behavior_set('MEMCACHED_BEHAVIOR_TCP_NODELAY','1') |
- +-----------------------------------------------------------------+
- | 0 |
- +-----------------------------------------------------------------+
测试
- #向表urls中插入数据,然后查看memcached是否对数据执行set操作。
- mysql> insert into urls (url) values ('http://google.com');
- mysql> insert into urls (url) values ('http://lycos.com/');
- mysql> insert into urls (url) values ('http://tripod.com/');
- mysql> insert into urls (url) values ('http://microsoft.com/');
- mysql> insert into urls (url) values ('http://slashdot.org');
- mysql> insert into urls (url) values ('http://mysql.com');
- mysql> select * from urls;
- mysql> select memc_get('urls:8');
- +--------------------+
- | memc_get('urls:8') |
- +--------------------+
- | http://google.com |
- +--------------------+
- 1 row in set (0.00 sec)
- mysql> select memc_get('urls:9');
- +--------------------+
- | memc_get('urls:9') |
- +--------------------+
- | NULL |
- +--------------------+
- 1 row in set (0.05 sec)
- mysql> select memc_get('urls:10');
- +---------------------+
- | memc_get('urls:10') |
- +---------------------+
- | http://tripod.com/ |
- +---------------------+
- 1 row in set (0.00 sec)
- #在memcached上telent查看数据更新
- [root@localhost ~]# telnet 192.168.16.21 11211
- Trying 192.168.16.21...
- Connected to 192.168.16.21.
- Escape character is '^]'.
- get urls:8
- VALUE urls:8 0 17
- http://google.com
- END
- quit
- Connection closed by foreign host.
- mysql> update test.urls set url="http://www.baidu.com" where id=8;
- Query OK, 0 rows affected, 1 warning (0.00 sec)
- Rows matched: 1 Changed: 0 Warnings: 1
- #在mysql中更新数据,查看memcached中是否会更新呢
- mysql> select * from urls;
- +----+-----------------------+
- | id | url |
- +----+-----------------------+
- | 8 | http://www.baidu.com |
- | 9 | http://lycos.com/ |
- | 10 | http://tripod.com/ |
- | 11 | http://microsoft.com/ |
- | 12 | http://slashdot.org |
- | 13 | http://mysql.com |
- +----+-----------------------+
- 6 rows in set (0.00 sec)
- [root@localhost ~]# telnet 192.168.16.21 11211
- Trying 192.168.16.21...
- Connected to 192.168.16.21.
- Escape character is '^]'.
- get urls:8
- VALUE urls:8 0 20
- http://www.baidu.com
- END
- quit
- Connection closed by foreign host.
需要注意libmemcached-0.40以上的版本在memcached_functions_mysql编译会出现以下问题,
- servers.c: In function ‘memc_servers_set’:
- servers.c:121: error: ‘memcached_st’ has no member named ‘hosts’
- servers.c:122: error: ‘memcached_st’ has no member named ‘hosts’
- servers.c:123: error: ‘memcached_st’ has no member named ‘hosts’
源代码此处定义的问题,暂时没有找到解决方法,希望各位高手解决这个问题
- memcached_version(master_memc);
- if (master_memc->hosts[0].major_version >= 1 &&
- master_memc->hosts[0].minor_version >= 2 &&
- master_memc->hosts[0].micro_version >= 4)
- memcached_behavior_set(master_memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
- memcached_server_list_free(servers);
- pthread_mutex_unlock(&memc_servers_mutex);
- fprintf(stderr, "rc %d\n", rc);
- return ((long long) rc == MEMCACHED_SUCCESS ? 0 : rc);
- }
参考以下链接
http://asyty.iteye.com/blog/1442587
http://www.bluegecko.net/mysql/memcached-functions-for-mysql-1-1-released/
http://mjs123.iteye.com/blog/643557
https://jiwaisms.googlecode.com/svn/wiki/[%E7%AC%94%E8%AE%B0]memcache%E5%AE%89%E8%A3%85%E6%80%BB%E7%BB%93.txt
http://www.haogongju.net/art/1333032
http://www.cnblogs.com/sunss/archive/2011/06/07/2074435.html
http://www.mylearning.cc/htmlfile/ArticleList/2011/7/27/bb19f32f-38a7-43b4-acd3-0eb6d24edfff.html