安装遇到的问题:
对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://www.danga.com/memcached/
memcached_functions_mysql下载:http://download.tangent.org/
二,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考到开机启动的目录下面,并且重命名为mysqld
[root@BlackGhost mysql]# ./mysql.server stop
Shutting down MySQL.. SUCCESS!
[root@BlackGhost mysql]# ./mysql.server start
Starting MySQL. SUCCESS!
二,关于libevent和memcached的安装
1.deb
Debian 系直接 $ sudo apt-get install memcached 都搞定,不然还需要分别安装libevent与memcached
2.源码安装
wget http://www.monkey.org/~provos/libevent-1.2.tar.gz #下载libevent
1 .查看是否已经安装了libevent,一般情况下,装系统的时候libevent就会装进去的。
ls -al /usr/lib | grep libevent
[root@BlackGhost etc]# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 mysql mysql 21 Mar 25 2009 libevent-1.2.so.1 -> libevent-1.2.so.1.0.3
-rwxr-xr-x 1 mysql mysql 208300 Mar 25 2009 libevent-1.2.so.1.0.3
-rw-r--r-- 1 mysql mysql 268992 Mar 25 2009 libevent.a
-rwxr-xr-x 1 mysql mysql 820 Mar 25 2009 libevent.la
lrwxrwxrwx 1 mysql mysql 21 Mar 25 2009 libevent.so -> libevent-1.2.so.1.0.3
如果显示上面内容说明你已安装了,就可以跳过第二步。
2 . 先安装libevent:
第一种方法
tar zxvf libevent-1.2.tar.gz
cd libevent-1.2
./configure --prefix=/usr
make
make install
第二种方法
用系统自命的软件管理工具,archlinux用pacman,centos用yum ,大便用apt-get等
[root@BlackGhost etc]# pacman -Ss libevent
core/libevent 1.4.11-1
An event notification library
安装完后在进行第一步,进行测试
3 . 安装memcached服务器端:
tar zxvf memcached-1.2.0.tar.gz
cd memcached-1.2.0
./configure --with-libevent=/usr --prefix=/usr/local/memcahced
make
make install
如果有错,请确认一下,local下面有memcahced这个文件夹,以及libevent的安装路径是否正确
4 .启动memcached服务
/usr/local/bin/memcached -d -m 20 -u zhangy -p 11211 -P ./memcached.pid
-d #作为守护进程运行
-m#分配20M的内存
-u #用户是zhangy
-p #监听端口是12000
-P#进程PID存放的位置
[zhangy@BlackGhost ~]$ ps -e|grep memcache
17904 ? 00:00:00 memcached
到此memcached的服务器端已经安装。
三,libmemcached的安装
$ wget
http://download.tangent.org/libmemcached-0.34.tar.gz
$ tar xvzf libmemcached-0.34.tar.gz
$ cd libmemcached-0.34
$ ./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
注意路径不要错了。install_functions.sql定义了一些memcache的操作函数:如下到这儿安装基本上结束,下面我们来测试一下
1.当时我下载的是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.34没有这个问题
2. libmemcached-0.37会 遇到 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)
所以推荐安装 ibmemcached-0.34。
3.在安装libmemcached的时候,如果不是默认安装,需要指定memcached安装路径。
四,安装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=/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/libmemcached34 不然会报以下错误
checking for mysql_config... /usr/bin/mysql_config
checking for libmemcached >= 0.17... configure: error: libmemcached not found
如果是mysql 5.5 mysql插件的目录是/usr/local/mysql/lib/plugin
创建mysql的memcache操作函数
$ mysql < sql/install_functions.sql
mysql如果不是默认安装,需要指定路径。
也可以在mysql管理里面运行install_functions.sql的sql脚本。
mysql> source sql/install_functions.sql
这样我们就可以使用mysql memcached UDF 了,我们可以通过下面语句查看是否已经正常安装
mysql> select * from mysql.func;
+------------------------------+-----+---------------------------------+----------+
| 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
如果懒得看就看我后边的例子
五,测试
例子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;
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)
Escape character is '^]'.
quit
Connection closed by foreign host.
例子2:
-
- 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的管理,徘徊中 (http://blog.51yip.com/php/729.html)里面提到对单表进行缓存 ,可以完全配合这篇文章所说的东西。如果有多表联合查询的话,在用触器就比较麻烦了,还不如放到程序里去执行
引用原文地址:
1.http://blog.51yip.com/cache/76.html
2.http://archive.cnblogs.com/a/2022594/