共计 7013 个字符,预计需要花费 18 分钟才能阅读完成。
Repcached 介绍
Repchched 项目地址:http://repcached.sourceforge.net/
关于 repcached
“repcached”是一个补丁集合,它添加数据复制功能到 memcached 1.2.x。
主要目的
- 实现冗余 memcached 系统。
主要功能 - 多主复制。
- 异步数据复制。
- 支持所有的 memcached 命令(set, add, delete, incr/decr, flush_all, cas)。
Memcached 1.4.x 的 Repcached 补丁
地址:http://mdounin.ru/
最新补丁:http://mdounin.ru/files/repcached-2.3.1-1.4.13.patch.gz
工作原理
repcached 实现了 memcached 复制的功能,它是一个单 master 单 slave 的方案,但 master/slave 都是可读写的,而且可以相互同步,如果 master 坏掉 slave 侦测到连接断了,它会自动 listen 而成为 master,这时坏掉的 master 只能启用为 slave,它们之间互换角色,才能保持复制功能,换句话说,master 没有抢占功能;而如果 slave 坏掉,master 也会侦测到连接断,它就会重新 listen 等待新的 slave 加入。
应用场景
用 memcached 做 session 共享或其它服务时会存在 memcached 的单点故障,如果 memcached 宕机,那么整个系统用户无法登陆 (session)。
基于这种情况,采用 repcached 做 memcached 的主从冗余。
Repcached 下载地址
http://sourceforge.net/projects/repcached/files/repcached/
Repcached 安装方式
Repcached 有两种安装方式:
1. 补丁版本安装
先安装相应版本的 memcached,然后对应版本的 Repcached 补丁。
2. 整合版本安装
直接安装整合版本的 memcached
方式一:补丁版本安装
1. 安装 Memcache,相关安装方法可以参见博文:
http://ultrasql.blog.51cto.com/9591438/1632179
2. 下载对应的 repcached 版本补丁安装文件:
假设安装的 memcahced 版本为 1.2.8,下载针对该版本最新的补丁:
wget http://softlayer-dal.dl.sourceforge.net/project/repcached/repcached/2.2.1-1.2.8/repcached-2.2.1-1.2.8.patch.gz
gzip -cd ../repcached-2.2.1-1.2.8.patch.gz | patch -p1
./configure –enable-replication
make && make install
方式二:整合版本安装
1. 安装 libevent:
cd /tmp
wget http://downloads.sourceforge.net/levent/libevent-2.0.22-stable.tar.gz
tar zxvf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable
./configure –prefix=/usr/local/lib
make && make install
2. 将 libevent 的库文件添加到动态库中:
vi /etc/ld.so.conf
在最后添加如下行:
/usr/local/lib // 此处为要添加的 libevent 库目录
重新加载动态 lib 库
ldconfig
注意:如果无此步骤,在启动 memcached 时,会提示看不到 libevent 的库文件。
3. 测试 libevent 是否安装成功:
ls -al /usr/lib | grep libevent-
4. 创建启动帐号:
groupadd memcached
useradd -g memcached memcached
5. 创建 PID 进程目录并修改所有者:
mkdir /var/run/memcached
chown -R memcached.memcached /var/run/memcached
6. 安装整合 memcached-repcached 包:
cd /tmp
wget http://softlayer-dal.dl.sourceforge.net/project/repcached/repcached/2.2.1-1.2.8/memcached-1.2.8-repcached-2.2.1.tar.gz
cp memcached-1.2.8-repcached-2.2.1.tar.gz /usr/local
cd /usr/local
tar zxvf memcached-1.2.8-repcached-2.2.1.tar.gz
mv memcached-1.2.8-repcached-2.2.1 memcached
cd memcached
./configure –prefix=/usr/local/memcached –with-libevent=/usr/local/lib –enable-replication –enable-64bit
注意:默认 memcached 单个进程只支持到 2G 内存,需要更大内存支持的话,需要打开 64 位支持,编译的时候加参数:
–enable-64bit
make && make install
提示编译出错:
make all-recursive
make[1]: Entering directory `/usr/local/memcached’
Making all in doc
make[2]: Entering directory `/usr/local/memcached/doc’
make[2]: Nothing to be done for `all’.
make[2]: Leaving directory `/usr/local/memcached/doc’
make[2]: Entering directory `/usr/local/memcached’
gcc -DHAVE_CONFIG_H -I. -DNDEBUG -m64 -g -O2 -MT memcached-memcached.o -MD
MP -MF .deps/memcached-memcached.Tpo -c -o memcached-memcached.o `test -f
memcached.c’ || echo ‘./’`memcached.c
memcached.c: In function‘add_iov’:
memcached.c:697: error:‘IOV_MAX’undeclared (first use in this function)
memcached.c:697: error: (Each undeclared identifier is reported only once
memcached.c:697: error: for each function it appears in.)
make[2]: *** [memcached-memcached.o] Error 1
make[2]: Leaving directory `/usr/local/memcached’
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/usr/local/memcached’
make: *** [all] Error 2
解决方案:
vi memcached.c
将下面的几行
/* FreeBSD 4.x doesn’t have IOV_MAX exposed. */
#ifndef IOV_MAX
#if defined(__FreeBSD__) || defined(__APPLE__)
# define IOV_MAX 1024
#endif
#endif
修改为
/* FreeBSD 4.x doesn’t have IOV_MAX exposed. */
#ifndef IOV_MAX
# define IOV_MAX 1024
#endif
重新编译和安装:
make && make install
查看帮助:
./bin/memcached -h
memcached 1.2.8
repcached 2.2.1
-p <num> TCP port number to listen on (default: 11211)
-U <num> UDP port number to listen on (default: 11211, 0 is off)
-s <file> unix socket path to listen on (disables network support)
-a <mask> access mask for unix socket, in octal (default 0700)
-l <ip_addr> interface to listen on, default is INDRR_ANY
-d run as a daemon
-r maximize core file limit
-u <username> assume identity of <username> (only when run as root)
-m <num> max memory to use for items in megabytes, default is 64 MB
-M return error on memory exhausted (rather than removing items)
-c <num> max simultaneous connections, default is 1024
-k lock down all paged memory. Note that there is a
limit on how much memory you may lock. Trying to
allocate more than that would fail, so be sure you
set the limit correctly for the user you started
the daemon with (not for -u <username> user;
under sh this is done with ‘ulimit -S -l NUM_KB’).
-v verbose (print errors/warnings while in event loop)
-vv very verbose (also print client commands/reponses)
-h print this help and exit
-i print memcached and libevent license
-P <file> save PID in <file>, only used with -d option
-f <factor> chunk size growth factor, default 1.25
-n <bytes> minimum space allocated for key+value+flags, default 48
-R Maximum number of requests per event
limits the number of requests process for a given con nection
to prevent starvation. default 20
-b Set the backlog queue limit (default 1024)
-x <ip_addr> hostname or IP address of peer repcached
-X <num:num> TCP port number for replication. <listen:connect> (default: 11212)
修改 memcached 目录所有者:
cd ..
chown -R memcached.memcached memcached
配置主从复制
参数说明:
-x 设置从哪个 IP 上进行同步。
-X 指定数据同步的端口。
Memcached 默认服务端口是 11211,默认同步监听端口是 11212。
启动 Master:
/usr/local/memcached/bin/memcached -d -m 100 -l 192.168.11.51 -p 11211 -u memcached -c 1024 -x 192.168.11.52 -X 11212 -P /var/run/memcached/memcached-rep.pid
查看监听端口:
netstat -tupln | grep memcached
tcp 0 0 192.168.11.51:11211 0.0.0.0:* LISTEN 18634/memcached
tcp 0 0 192.168.11.51:11212 0.0.0.0:* LISTEN 18634/memcached
udp 0 0 192.168.11.51:11211 0.0.0.0:* 18634/memcached
启动 Slave:
/usr/local/memcached/bin/memcached -d -m 100 -l 192.168.11.52 -p 11211 -u memcached -c 1024 -x 192.168.11.51 -X 11212 -P /var/run/memcached/memcached-rep.pid
说明:-x 192.168.11.51 用于同步的 Master 的 IP 地址。
查看监听端口:
netstat -tupln | grep memcached
tcp 0 0 192.168.11.52:11211 0.0.0.0:* LISTEN 24262/memcached
udp 0 0 192.168.11.52:11211 0.0.0.0:* 24262/memcached
验证数据同步
在 Master 创建数据:
[root@test01 bin]# telnet 192.168.11.51 11211
Trying 192.168.11.51…
Connected to 192.168.11.51.
Escape character is ‘^]’.
get key1
END
set key1 0 0 2
aa
STORED
quit
Connection closed by foreign host.
在 Slave 获取数据:
[root@test02 bin]# telnet 192.168.11.52 11211
Trying 192.168.11.52…
Connected to 192.168.11.52.
Escape character is ‘^]’.
get key1
END
get key1
VALUE key1 0 2
aa
END
quit
Connection closed by foreign host.
在 Slave 创建数据:
[root@test02 bin]# telnet 192.168.11.52 11211
Trying 192.168.11.52…
Connected to 192.168.11.52.
Escape character is ‘^]’.
get key2
END
set key2 0 0 3
b
STORED
get key2
VALUE key2 0 3
b
END
quit
Connection closed by foreign host.
在 Master 获取数据:
[root@test01 bin]# telnet 192.168.11.51 11211
Trying 192.168.11.51…
Connected to 192.168.11.51.
Escape character is ‘^]’.
get key2
VALUE key2 0 3
b
END
quit
Connection closed by foreign host.
Memcached 高可用
启动 Master 和 Slave 时不要加 - l 参数指定监听地址,否则 keepalived 无法监听 VIP 的地址。
���后配置上 keepalived 就可以作为高可用了。
Memcached 安装及启动脚本 http://www.linuxidc.com/Linux/2013-07/87641.htm
PHP 中使用 Memcached 的性能问题 http://www.linuxidc.com/Linux/2013-06/85883.htm
Ubuntu 下安装 Memcached 及命令解释 http://www.linuxidc.com/Linux/2013-06/85832.htm
Memcached 的安装和应用 http://www.linuxidc.com/Linux/2013-08/89165.htm
使用 Nginx+Memcached 的小图片存储方案 http://www.linuxidc.com/Linux/2013-11/92390.htm
Memcached 使用入门 http://www.linuxidc.com/Linux/2011-12/49516p2.htm
Memcached 的详细介绍:请点这里
Memcached 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-04/116330.htm