memcached是一个高性能的分布式内存对象缓存系统,用于动态web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。memcached基于一个存储键/值对的hash map。其守护进程(daemon)是用 c 写的,但是客户端可以用任何语言来编写,并通过 memcached 协议与守护进程通信。
OK,简单介绍一下,下面开始部署!
1
2
3
4
5
6
7
|
环境:
CentOS 6.5 x86_64位 采用最小化安装,系统经过了基本优化
selinux 为关闭状态,iptables 为无限制模式
主机名:nolinux
源码包存放位置:
/usr/local/src
libevent版本:2.0.21
memcached版本:1.4.20
|
一、基础环境准备
1
|
[root@nolinux ~]
# yum -y install gcc gcc-c++
|
二、服务部署
1、安装libevent
libevent是个程序库,它将linux的epool、BSD类操作系统的kqueue等事件处理功能封装成统一的接口。即使对服务器的连接数增加,也能发挥出色的性能。
由于 memcached 使用 libevent 这个库负责 Socket 的处理,因此我们在安装 memcached 之前还需要安装libevent。目前,libevent的最新版本是libevent-2.0.21。如果之前你已经安装过libevent,那么这里可以忽略此步骤。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
[root@nolinux ~]
# cd /usr/local/src
[root@nolinux src]
# wget http://code.taobao.org/p/nolinux/src/memcached/src/libevent-2.0.21-stable.tar.gz?orig
[root@nolinux src]
# tar zxvf libevent-2.0.21-stable.tar.gz
[root@nolinux src]
# cd libevent-2.0.21-stable
[root@nolinux libevent-2.0.21-stable]
# ./configure --prefix=/usr
[root@nolinux libevent-2.0.21-stable]
# make
[root@nolinux libevent-2.0.21-stable]
# make install
[root@nolinux libevent-2.0.21-stable]
# ll /usr/lib/libevent* # libevent安装完后,会在/usr/lib目录下出现如下内容
lrwxrwxrwx 1 root root 21 Aug 11 13:49
/usr/lib/libevent-2
.0.so.5 -> libevent-2.0.so.5.1.9
-rwxr-xr-x 1 root root 968690 Aug 11 13:49
/usr/lib/libevent-2
.0.so.5.1.9
-rw-r--r-- 1 root root 1571802 Aug 11 13:49
/usr/lib/libevent
.a
lrwxrwxrwx 1 root root 26 Aug 11 13:49
/usr/lib/libevent_core-2
.0.so.5 -> libevent_core-2.0.so.5.1.9
-rwxr-xr-x 1 root root 585225 Aug 11 13:49
/usr/lib/libevent_core-2
.0.so.5.1.9
-rw-r--r-- 1 root root 978482 Aug 11 13:49
/usr/lib/libevent_core
.a
-rwxr-xr-x 1 root root 970 Aug 11 13:49
/usr/lib/libevent_core
.la
lrwxrwxrwx 1 root root 26 Aug 11 13:49
/usr/lib/libevent_core
.so -> libevent_core-2.0.so.5.1.9
lrwxrwxrwx 1 root root 27 Aug 11 13:49
/usr/lib/libevent_extra-2
.0.so.5 -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x 1 root root 404852 Aug 11 13:49
/usr/lib/libevent_extra-2
.0.so.5.1.9
-rw-r--r-- 1 root root 593392 Aug 11 13:49
/usr/lib/libevent_extra
.a
-rwxr-xr-x 1 root root 977 Aug 11 13:49
/usr/lib/libevent_extra
.la
lrwxrwxrwx 1 root root 27 Aug 11 13:49
/usr/lib/libevent_extra
.so -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x 1 root root 935 Aug 11 13:49
/usr/lib/libevent
.la
lrwxrwxrwx 1 root root 30 Aug 11 13:49
/usr/lib/libevent_pthreads-2
.0.so.5 -> libevent_pthreads-2.0.so.5.1.9
-rwxr-xr-x 1 root root 18430 Aug 11 13:49
/usr/lib/libevent_pthreads-2
.0.so.5.1.9
-rw-r--r-- 1 root root 18670 Aug 11 13:49
/usr/lib/libevent_pthreads
.a
-rwxr-xr-x 1 root root 998 Aug 11 13:49
/usr/lib/libevent_pthreads
.la
lrwxrwxrwx 1 root root 30 Aug 11 13:49
/usr/lib/libevent_pthreads
.so -> libevent_pthreads-2.0.so.5.1.9
lrwxrwxrwx 1 root root 21 Aug 11 13:49
/usr/lib/libevent
.so -> libevent-2.0.so.5.1.9
[root@nolinux libevent-2.0.21-stable]
# cd ..
|
2、安装memcached
1
2
3
4
5
6
7
8
9
|
[root@nolinux memcached-1.4.20]
# wget http://code.taobao.org/p/nolinux/src/memcached/src/memcached-1.4.20.tar.gz?orig
[root@nolinux src]
# tar zxvf memcached-1.4.20.tar.gz
[root@nolinux src]
# cd memcached-1.4.20
[root@nolinux memcached-1.4.20]
# ./configure --with-libevent=/usr
[root@nolinux memcached-1.4.20]
# make
[root@nolinux memcached-1.4.20]
# make install
[root@nolinux memcached-1.4.20]
# cd
[root@nolinux ~]
# ll /usr/local/bin/memcached # 安装完成后会把memcached 放到 /usr/local/bin/memcached
-rwxr-xr-x 1 root root 341907 Aug 11 13:52
/usr/local/bin/memcached
|
注意:
如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径
三、启动配置
1、启动Memcached服务
1
2
3
4
|
[root@nolinux ~]
# /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.121 -p 11211 -c 256 -P /tmp/memcached.pid
[root@nolinux ~]
# lsof -i tcp:11211
COMMAND PID USER FD TYPE DEVICE SIZE
/OFF
NODE NAME
memcached 11470 root 26u IPv4 19581 0t0 TCP 192.168.0.121:memcache (LISTEN)
|
补充:关于memcached启动命令的介绍
1
2
3
4
5
6
7
8
9
|
-d 启动一个守护(daemon)进程
-m 分配给 memcache 使用的内存数量,单位是MB
-u 运行 memcache 的用户
-l 监听的服务器IP地址
-p 设置 memcache 监听的端口,端口最好大于1024
-c 设置 memcache 最大运行的并发连接数,默认是1024
-P 设置保存 memcache 的pid文件地址
设置pid文件的好处在于方便结束 memcache 进程
如果存在pid文件,那么我们就可以通过如下命令快速结束 memcached 进程
|
1
|
[root@nolinux ~]
# kill `cat /tmp/memcached.pid`
|
2、配置memcached开机自启
1
2
3
4
5
|
[root@nolinux ~]
# echo "# memcached service daemon by sun 201408022" >>/etc/rc.local
[root@nolinux ~]
# echo "/usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.121 -p 11211 -c 256 -P /tmp/memcached.pid" >> /etc/rc.local
[root@nolinux ~]
# tail -2 /etc/rc.local
# memcached service daemon by sun 201408022
/usr/local/bin/memcached
-d -m 10 -u root -l 192.168.0.121 -p 11211 -c 256 -P
/tmp/memcached
.pid
|
四、服务测试
1、连接测试
1
2
3
4
5
6
|
[root@nolinux src]
# telnet 192.168.0.121 11211
Trying 192.168.0.121...
Connected to 192.168.0.121.
Escape character is
'^]'
.
# 出现该提示就表示连接成功
quit
# 输入quit退出当前连接
Connection closed by foreign host.
|
2、插入键值测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
[root@nolinux ~]
# telnet 192.168.0.121 11211
Trying 192.168.0.121...
Connected to 192.168.0.121.
Escape character is
'^]'
.
set
key1 0 0 7
nolinux
STORED
set
key2 0 0 6
sunsky
STORED
quit
Connection closed by foreign host.
[root@nolinux ~]
# telnet 192.168.0.121 11211
Trying 192.168.0.121...
Connected to 192.168.0.121.
Escape character is
'^]'
.
get key1
VALUE key1 0 7
nolinux
END
get key2
VALUE key2 0 6
sunsky
END
quit
Connection closed by foreign host.
|
OK!我们的memcached已经安装成功了!
重点:memcached单实例/多实例
关于memcached的单实例和多实例,我们不需要像mysql一样配置那么麻烦,我们只需要再执行memcached的启动命令启动就可以再打开多个memcahed实例了。不过需要注意的是,一定不能让它们的端口相同!切记!
下面是memcached单机多实例的启动实例:
1
2
3
4
5
6
|
[root@nolinux ~]
# /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.121 -p 11211 -c 256 -P /tmp/memcached.pid
[root@nolinux ~]
# /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.121 -p 11212 -c 256 -P /tmp/memcached.pid
[root@nolinux ~]
# ps -ef |grep memcached
root 11880 1 0 14:14 ? 00:00:00
/usr/local/bin/memcached
-d -m 10 -u root -l 192.168.0.121 -p 11211 -c 256 -P
/tmp/memcached
.pid
root 11944 1 0 14:32 ? 00:00:00
/usr/local/bin/memcached
-d -m 10 -u root -l 192.168.0.121 -p 11212 -c 256 -P
/tmp/memcached
.pid
root 11951 2175 0 14:32 pts
/0
00:00:00
grep
memcached
|
OK,从操作结果可以看出,memcached的单机多实例已经OK!
本篇博文就到这里,关于 memcached 命令行的操作,我将放到下一篇博文进行分享。
希望能对大家有所帮助!