时间:2021-07-01 10:21:17 帮助过:29人阅读
mysql memcached UDF 其实就是通过libmemcached来使用memcache的一系列函数,通过这些函数,你能 对memcache进行get, set, cas, append, prepend, delete, increment, decrement objects操作,如果我们通过mysql trigger来使用这些函数,那么就能通过mysql更好的,更自动的管理memcache!mysql:http://downloads.mysql.com/archives/2 安装1)安装libevent+memcache
libevent下载:wget http://www.monkey.org/~provos/libevent-1.2.tar.gz
libmemcached下载:http://download.tangent.org/
memcached下载:http://pecl.php.com/package/memcached
memcached_functions_mysql下载:http://download.tangent.org/
什么是 memcached ?顾名思义,就是内存缓存。按照其官网的介绍:memcached 是高性能、分布式的内存对象缓存系统,意图用来通过减轻数据库的负载来提高动态 WEB 应用的速度。读写内存的速度显然要比读写文件系统或数据库系统要快得多,将动态数据缓存在内存中,显然要比其他缓存,有效得多。添加内存能带来的性能提升,对于服务器来说,要比其他方式有效得多。对于大中型网站,部署应用 memcached 将收到很好的效果。Debian 系直接 $ sudo apt-get install memcached 都搞定,不然还需要分别安装libevent与memcached只是编译安装时注意64位的要增加64位选项 $ ./configure --enable-64bit 将编译 64 位的版本。2)安装libmemcached
$ wget http://download.tangent.org/libmemcached-0.34.tar.gz$ tar xvzf libmemcached-0.34.tar.gz $ cd libmemcached-0.34不要用libmemcached-0.42.tar.gz,会遇到servers.c:263:28: error: ‘memcached_st’ has no member named ‘hosts’servers.c:264:28: error: ‘memcached_st’ has no member named ‘hosts’但是libmemcached-0.34到0.37都没有这个问题这里要使用0.34 不然会遇到 ERROR 1126 (HY000) at line 38: Can't open shared library 'libmemcached_functions_mysql.so' (errno: 0 /usr/local/mysql/lib/plugin/libmemcached_functions_mysql.so: undefined symbol: memcached_string_append) 3)安装memcached_functions_mysql
$ ./configure --prefix=/usr/local/libmemcached34 --with-memcached=/usr/bin/memcached
$ sudo make$ sudo make install$ sudo chmod 777 /etc/ld.so.conf$ echo "/usr/local/libmemcached34" >> /etc/ld.so.conf$ ldconfig
$ wget http://download.tangent.org/memcached_functions_mysql-0.9.tar.gz$ tar zxvf memcached_functions_mysql-0.9.tar.gz4)拷贝lib文件到mysql的plugin下面
$ cd memcached_functions_mysql-0.9
$ ./configure --prefix=/usr/local/memcache_mysql --with-mysql=/usr/local/mysql/bin/mysql_config --with-libmemcached=/usr/local/libmemcached34如果缺少pkg-config安装一下$ sudo apt-get install pkg-config$ sudo make$ sudo make install
$ sudo cp /usr/local/memcache_mysql/lib/libmemcached_functions* /usr/local/mysql/lib/mysql/plugin5)添加memcache UDF 函数
mysql> source sql/install_functions.sql这样我们就可以使用mysql memcached UDF 了,我们可以通过下面语句查看是否已经正常安装
mysql> select * from mysql.func;3 测试
+------------------------------+-----+---------------------------------+----------+
| name | ret | dl | type |
+------------------------------+-----+---------------------------------+----------+
| memc_add | 2 | libmemcached_functions_mysql.so | function |
| memc_add_by_key | 2 | libmemcached_functions_mysql.so | function |
| memc_servers_set | 2 | libmemcached_functions_mysql.so | function |
32 rows in set (0.00 sec)
添加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如果懒得看就看我后边的例子
mysql> select memc_servers_set('127.0.0.1:11211')注意:如果mysql restart,需要重新运行这句以建立与memcached之间的关系mysql> select memc_servers_behavior_set('MEMCACHED_BEHAVIOR_NO_BLOCK','1');--EOF--作者:Buro#79xxd出处:http://www.cnblogs.com/buro79xxd/文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
+--------------------------------------------------------------+
| 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: 0mysql> 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 11211Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.get 1ENDget 2VALUE 2 0 5xxd79ENDget 3VALUE 3 0 9buro79xxdENDquitConnection closed by foreign host.