对memcache的操作一般都是放程序里面去操作的,新增,更新,删除什么的。如果能利用mysql来对memcache进行操作,那就更好,代码端就会简单一点。但是利用mysql来操作memcache,比较适合实现简单的方式。下面说一下安装的过程和遇到的问题,在看安装过程的之前,我觉得应当先看一下,我安装时候所遇到的问题,这样你可以避免掉,少走一点弯路。
一,安装所要的软件
mysql5.1以前版本:http://downloads.mysql.com/archives/
libevent下载:wget http://www.monkey.org/~provos/libevent-1.2.tar.gz
libmemcached下载:http://download.tangent.org/
memcached下载:http://pecl.php.net/package/memcached
memcached_functions_mysql下载:http://download.tangent.org/
下载东西的时候,也是有技巧的,下载的东西,不要太新,太新了不然这个不行,那个也不行,太老了也不行,太老了,这个装不了,那个也装不了,所以要找一个稳定版的。一般正規软件的话,会标识basic版,stable版之类的.
二,mysql的安装
mysql的安装方式有很多,你可以用系统的软件管理包来安装,不同的linux版本,软件管理工具是不一样的。
redhat,centos 有yum,ubuntu 有apt get ,arch有pacman等。用系统自带的管理工具安装比较简单。如果是自己下载怎么安装的呢,mysql的官方网站提供了三种mysql的安装源码,一种是.rpm的,一种是二进制的,一种是要自己编译的。
1,用rpm来按装
-
rpm -i MySQL-server-VERSION.i386.rpm
-
rpm -i MySQL-client-VERSION.i386.rpm
2,二进制包进行安装
二进制包安装有一个缺点,就是要安装到什么地方,都是死的。安装过程中有问题的,查看一下是不是装了glibc,以及版本是不是太低了。
-
groupadd mysql
-
useradd -g mysql mysql
-
tar zxvf /path/to/mysql-VERSION-OS.tar.gz -C /usr/local
-
cd /usr/local
-
mv mysql-VERSION-OS ./mysql
-
cd /usr/local/mysql
-
scripts/mysql_install_db --user=mysql
-
chown -R mysql:mysql /usr/local/mysql
-
bin/mysqld_safe --user=mysql &
3,源码自己编译
-
groupadd mysql
-
useradd -g mysql mysql
-
tar zxvf /path/to/mysql-VERSION-OS.tar.gz
-
cd /mysql-VERSION-OS
-
./configure --prefix=/usr/local/mysql
-
make && make install
-
cp support-files/my-medium.cnf /etc/my.cnf
-
cd /usr/local/mysql
-
bin/mysql_install_db --user=mysql
-
chown -R mysql:mysql /usr/local/mysql
-
bin/mysqld_safe --user=mysql &
安装遇难问题:安装mysql至少要5.1版本以上的,服务器端,还是客户端都要,装完memcached_functions_mysql后,调用libmemcached模块时会报错的。
[root@BlackGhost sql]# /usr/local/mysql/bin/mysql <install_functions.sql
ERROR 1126 (HY000) at line 1: Can't open shared library 'libmemcached_functions_mysql.so' (errno: 22 /usr/local/mysql/lib/mysql/plugin/libmemcached_functions_mysql.so: undefined symbol: memcached_string_append)
上面是用mysqld_safe来启动mysql的,也可以用mysql.server来启动,它在/usr/local/mysql/share/mysql下面,你也可以把mysql.server考到开机启动的目录下面,并且重命名为httpd
[root@BlackGhost mysql]# ./mysql.server stop
Shutting down MySQL.. SUCCESS!
[root@BlackGhost mysql]# ./mysql.server start
Starting MySQL. SUCCESS!
二,关于libevent和memcached的安装
请参考:linux memcached 安装
三,libmemcached的安装
-
tar zxvf libmemcached-0.37.tar.gz
-
cd libmemcached-0.37
-
./configure --prefix=/usr/local/libmemcached37 --with-memcached
-
make && make install
安装遇到的问题:当时我下载的是libmemcached-0.42.tar.gz,安装memcached_functions_mysql过程中遇到这样一个问题
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.37没有这个问题,搞得我很无语,难道不是向下兼容的吗?
四,安装memcached_functions_mysql
-
tar xzf memcached_functions_mysql-0.9.tar.gz
-
cd memcached_functions_mysql-0.9
-
./configure --prefix=/usr/local/memcache_mysql --with-mysql-config=/usr/local/mysql/bin/mysql_config --with-libmemcached=/usr/local/libmemcached37
-
make && make install
-
cp /usr/local/memcache_mysql/lib/libmemcached_functions* /usr/local/mysql/lib/mysql/plugin
别忘了加上--with-libmemcached=/usr/local/libmemcached37不然会报以下错误
checking for mysql_config... /usr/bin/mysql_config
checking for libmemcached >= 0.17... configure: error: libmemcached not found
创建mysql的memcache操作函数
mysql <memcached_functions_mysql-0.9/sql/install_functions.sql
注意路径不要错了。install_functions.sql定义了一些memcache的操作函数:如下
-
[root@BlackGhost zhangy]# cat memcached_functions_mysql-0.9/sql/install_functions.sql |awk '{if($0 !~ /^$/ ){print $3;} }
-
memc_add
-
memc_add_by_key
-
memc_servers_set
-
memc_servers_version
-
memc_server_count
-
memc_set
-
memc_set_by_key
-
memc_cas
-
memc_cas_by_key
-
memc_get
-
memc_get_by_key
-
memc_delete
-
memc_delete_by_key
-
memc_append
-
memc_append_by_key
-
memc_prepend
-
memc_prepend_by_key
-
memc_increment
-
memc_decrement
-
memc_replace
-
memc_replace_by_key
-
memc_servers_behavior_set
-
memc_servers_behavior_get
-
memc_behavior_set
-
memc_behavior_get
-
memc_list_behaviors
-
memc_list_hash_types
-
memc_list_distribution_types
-
memc_udf_version
-
memc_libmemcached_version
-
memc_stats
-
memc_stat_get_keys
-
memc_stat_get_value
到这儿安装基本上结束,下面我们来测试一下
五,测试
启动memcached:
/usr/local/bin/memcached -d -m 20 -u zhangy -p 12000 -P ./memcached.pid
/usr/local/bin/memcached -d -m 20 -u zhangy -p 13000 -P ./mem.pid
-
-
drop table if exists urls;
-
create table urls (
-
id int(3) not null,
-
url varchar(64) not null default '',
-
primary key (id)
-
);
-
-
-
select memc_servers_set('127.0.0.1:12000,127.0.0.1:13000');
-
-
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(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://baidu.com/');
-
insert into urls (url) values ('http://www.51yip.com/');
-
insert into urls (url) values ('http://blog.51yip.com/');
-
insert into urls (url) values ('http://51yip.com');
-
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://51yip.com';
-
select url from urls where url = 'http://51yip.com/manual';
-
select memc_get('urls:6');
-
-
delete from urls where url = 'http://blog.51yip.com/';
-
select * from urls where url='http://blog.51yip.com/';
-
select memc_get('urls:4');
我以前写过一篇:关于memcache的key的管理,徘徊中里面提到对单表进行缓存,可以完全配合这篇文章所说的东西。如果有多表联合查询的话,在用触器就比较麻烦了,还不如放到程序里去执行
(责任编辑:IT) |