1.安装Memcached
1.下载Memcached及其依赖
下载memcached-1.4.24.tar.gz和libevent-2.0.22-stable.tar.gz文件并解压如下:
[[email protected] libevent]$ tar -zxvf libevent-2.0.22-stable.tar.gz
[[email protected] Memcached]$ tar -zxvf memcached-1.4.24.tar.gz
2.安装memcached
先安装libevent:
[[email protected] libevent-2.0.22-stable]$ cd /home/lizhiwei/SoftWare/libevent/libevent-2.0.22-stable
[[email protected] libevent-2.0.22-stable]$ ./configure -prefix=/home/lizhiwei/InstallSoft/libevent
[[email protected] libevent-2.0.22-stable]$ make
[[email protected] libevent-2.0.22-stable]$ make install
安装memcached,同时需要安装中指定libevent的安装位置:
[[email protected] libevent-2.0.22-stable]$ cd /home/lizhiwei/SoftWare/Memcached/memcached-1.4.24
[[email protected] memcached-1.4.24]$ ./configure -prefix=/home/lizhiwei/InstallSoft/memcached -with-libevent=/home/lizhiwei/InstallSoft/libevent/
[[email protected] memcached-1.4.24]$ make
[[email protected] memcached-1.4.24]$ make install
此时memcached安装完成,安装路径:/home/lizhiwei/InstallSoft/memcached
2.启动Memcached
1.启动memcached并参看服务
[[email protected] memcached]$ cd /home/lizhiwei/InstallSoft/memcached/bin/
[[email protected] bin]$ ll
total 356
-rwxr-xr-x. 1 lizhiwei lizhiwei 360894 Oct 1 20:52 memcached
[[email protected] bin]$ ./memcached -d -m 10 -u lizhiwei -l 127.0.0.1 -p 12000 -c 256 -P /tmp/memcached.pid
[[email protected] bin]$ ps -ef | grep memcached
lizhiwei 24004 1 0 20:59 ? 00:00:00 ./memcached -d -m 10 -u lizhiwei -l 127.0.0.1 -p 12000 -c 256 -P /tmp/memcached.pid
lizhiwei 24018 14869 0 21:00 pts/2 00:00:00 grep memcached
[[email protected] bin]$ cat /tmp/memcached.pid
24004
[[email protected] bin]$
./memcached -d -m 10 -u lizhiwei -l 127.0.0.1 -p 12000 -c 256 -P /tmp/memcached.pid 启动命令说明:
-d 选项是启动一个守护进程
-m 是分配给Memcache使用的内存数量,单位是MB,我这里是10MB
-u 是运行Memcache的用户,我这里是lizhiwei
-l 是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址127.0.0.1
-p 是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口
-c 选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定
-P 是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid
2.停止Memcache服务
使用kill -9 进程PID 命令停止Memcache服务
[[email protected] bin]$ ps -ef | grep memcached
lizhiwei 24004 1 0 20:59 ? 00:00:00 ./memcached -d -m 10 -u lizhiwei -l 127.0.0.1 -p 12000 -c 256 -P /tmp/memcached.pid
lizhiwei 24069 14869 0 21:07 pts/2 00:00:00 grep memcached
[[email protected] bin]$ kill -9 24004
[[email protected] bin]$ ps -ef | grep memcached
lizhiwei 24072 14869 0 21:07 pts/2 00:00:00 grep memcached
[[email protected] bin]$
3.连接Memcache服务
[[email protected] ~]$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is ‘^]‘.
set name 0 64 5
abcde
STORED
get name
VALUE name 0 5
abcde
END
quit
Connection closed by foreign host.
[[email protected] ~]$
telnet 使用说明:
telnet 127.0.0.1 11211 连接到IP地址127.0.0.1,端口号11211
set name 0 64 5 格式:<command name> <key> <flags> <exptime> <bytes>
<command name> :set/add/replace
<key> :查找关键字
<flags> :整型参数,客户机使用它存储关于键值对的额外信息
<exptime> :该数据的存活时间(以秒为单位,0 表示永远)
<bytes> :存储字节数,注意:设置了存储大小就必须存储一样大小的数据,否则会操作失败
-------------------------------------------------------------------------------------------------------------------------------