1.安装Memcached
1.下载Memcached及其依赖
下载memcached-1.4.24.tar.gz和libevent-2.0.22-stable.tar.gz文件并解压如下:
[lizhiwei@localhost libevent]$ tar -zxvf libevent-2.0.22-stable.tar.gz
[lizhiwei@localhost Memcached]$ tar -zxvf memcached-1.4.24.tar.gz
2.安装memcached
先安装libevent:
[lizhiwei@localhost libevent-2.0.22-stable]$ cd /home/lizhiwei/SoftWare/libevent/libevent-2.0.22-stable
[lizhiwei@localhost libevent-2.0.22-stable]$ ./configure -prefix=/home/lizhiwei/InstallSoft/libevent
[lizhiwei@localhost libevent-2.0.22-stable]$ make
[lizhiwei@localhost libevent-2.0.22-stable]$ make install
安装memcached,同时需要安装中指定libevent的安装位置:
[lizhiwei@localhost libevent-2.0.22-stable]$ cd /home/lizhiwei/SoftWare/Memcached/memcached-1.4.24
[lizhiwei@localhost memcached-1.4.24]$ ./configure -prefix=/home/lizhiwei/InstallSoft/memcached -with-libevent=/home/lizhiwei/InstallSoft/libevent/
[lizhiwei@localhost memcached-1.4.24]$ make
[lizhiwei@localhost memcached-1.4.24]$ make install
此时memcached安装完成,安装路径:/home/lizhiwei/InstallSoft/memcached
2.启动Memcached
1.启动memcached并参看服务
[lizhiwei@localhost memcached]$ cd /home/lizhiwei/InstallSoft/memcached/bin/
[lizhiwei@localhost bin]$ ll
total 356
-rwxr-xr-x. 1 lizhiwei lizhiwei 360894 Oct 1 20:52 memcached
[lizhiwei@localhost bin]$ ./memcached -d -m 10 -u lizhiwei -l 127.0.0.1 -p 12000 -c 256 -P /tmp/memcached.pid
[lizhiwei@localhost 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
[lizhiwei@localhost bin]$ cat /tmp/memcached.pid
24004
[lizhiwei@localhost 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服务
[lizhiwei@localhost 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
[lizhiwei@localhost bin]$ kill -9 24004
[lizhiwei@localhost bin]$ ps -ef | grep memcached
lizhiwei 24072 14869 0 21:07 pts/2 00:00:00 grep memcached
[lizhiwei@localhost bin]$
3.连接Memcache服务
[lizhiwei@localhost ~]$ 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.
[lizhiwei@localhost ~]$
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> :存储字节数,注意:设置了存储大小就必须存储一样大小的数据,否则会操作失败
-------------------------------------------------------------------------------------------------------------------------------