1.下载包html
1.1.到http://memcached.org/下载linux下的memcached的安装包.java
1.2.到http://libevent.org/ 下载安装memcached前比须要的包libevent-2.0.22我安装的是这个版本.linux
2.安装libevent-2.0.22shell
[root@hnxz-vm-dg-s1 ~]# tar -zxvf libevent-2.0.22-stable.tar.gz [root@hnxz-vm-dg-s1 ~]# cd libevent-2.0.22-stable [root@hnxz-vm-dg-s1 libevent-2.0.22-stable]# pwd /root/libevent-2.0.22-stable [root@hnxz-vm-dg-s1 libevent-2.0.22-stable]# ./configure --prefix=/usr/local/libevent [root@hnxz-vm-dg-s1 libevent-2.0.22-stable]# make && make install
3.安装memcached数据库
[root@hnxz-vm-dg-s1 ~]# tar -zxvf memcached-1.4.25.tar.gz [root@hnxz-vm-dg-s1 ~]# cd memcached-1.4.25 [root@hnxz-vm-dg-s1 memcached-1.4.25]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent --enable-64bit [root@hnxz-vm-dg-s1 memcached-1.4.25]# make && make install
--with-libevent=??? 此路径可使用命令查找lib位置后端
whereis libevent
4.测试是否成功安装memcached:缓存
[root@hnxz-vm-dg-s1 memcached-1.4.25]# ls -al /usr/local/memcached/bin/ total 360 drwxr-xr-x 2 root root 4096 Jan 13 17:24 . drwxr-xr-x 5 root root 4096 Jan 13 17:24 .. -rwxr-xr-x 1 root root 360434 Jan 13 17:24 memcached [root@hnxz-vm-dg-s1 memcached-1.4.25]#
成功的话,会输出memcached 可执行程序服务器
5.启动memcached并发
[root@hnxz-vm-dg-s1 bin]# ll total 352 -rwxr-xr-x 1 root root 360434 Jan 13 17:24 memcached [root@hnxz-vm-dg-s1 bin]# ./memcached -d -m 246 -l 10.207.238.61 -p 11211 -u root
若是中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增长相应的库或者路径。less
我在启动过程当中报这个错误找不到lib包,错误以下:
memcached: error while loading shared libraries: libevent-2.0.so.5: cannot
使用命令,便可查看缺失的lib包位置以及错误的详细信息;
LD_DEBUG=libs ./memcached -v 2>&1 > /dev/null | less
[root@hnxz-vm-dg-s1 bin]# ll total 352 -rwxr-xr-x 1 root root 360434 Jan 13 17:24 memcached [root@hnxz-vm-dg-s1 bin]# LD_DEBUG=libs ./memcached -v 2>&1 > /dev/null | less [root@hnxz-vm-dg-s1 bin]#
最终确认为找不到libevent-2.0.so.5 使用ln -s软链接一个
ln -s /usr/local/lib/libevent.so /usr/lib64/libevent-2.0.so.5
6.给出memcached启动命令参数详解.
-d选项是启动一个守护进程, -m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB, -u是运行Memcache的用户,我这里是root, -l是监听的服务器IP地址,若是有多个地址的话,我这里指定了服务器的IP地址192.168.0.200, -p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口, -c选项是最大运行的并发链接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定, -P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid, -p 使用的TCP端口。默认为11211 -m 最大内存大小。默认为64M -vv 用very vrebose模式启动,调试信息和错误输出到控制台 -d 做为daemon在后台启动
若是要结束Memcache进程,执行:
# kill `cat /tmp/memcached.pid`
或是 经过ps -ef | grep memcached 找到pid ,而后kill
也能够启动多个守护进程,不过端口不能重复。
# ps -ef | grep memcached
root 28914 1 0 07:27 ? 00:00:00 memcached -d -m 128 -l localhost -p 11211 -u root (启动ok)
6.测试memcached
#telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
对memecached的管理能够参考这篇文章http://www.blogjava.net/hao446tian/archive/2012/04/11/373773.html
经常使用指令:
stats
memcached的做者给咱们提供查看运行状况的命令。主要是“stats”,使用方法为 “telnet ip 端口号”,登陆后使用“stats”命令。
如下是引用片断:
pid = process id uptime = number of seconds since the process was started time = current time version = memcached version rusage_user = seconds the cpu has devoted to the process as the user rusage_system = seconds the cpu has devoted to the process as the system curr_items = total number of items currently in memcache total_items = total number of items that have passed through the cache bytes = total number of bytes currently in use by curr_items curr_connections = total number of open connections to memcached connection_structures = ??? cmd_get = total GET commands issued to the server cmd_set = total SET commands issued to the server get_hits = total number of times a GET command was able to retrieve and return data get_misses = total number of times a GET command was unable to retrieve and return data bytes_read = total number of bytes input into the server bytes_written = total number of bytes written by the server limit_maxbytes = total storage bytes available to the server.
着重说一下几个对观测颇有用的项。 limit_maxbytes、bytes memcached在存储的时候是能够设置失效时间的,但若是存储已经满了,那旧数据即便没有到过时时间,也会被移除。因此须要观察memcached存储是否已经满了,同时这对扩容也是有意义的参考。limit_maxbytes即总的存储大小,而bytes 就是已经使用的大小,从这两个数据就能够看出在memcached启动时,咱们为它分配的内存是否足够使用。 cmd_get、cmd_set memcached启动后,咱们对它一共作了多少次读取操做呢?从这两个参数能够观察出来。 get_hits、get_misses 使用memcached后,咱们须要评估咱们使用的策略是否合理。不可以使用中间缓存后,后端的数据库仍是有较大的访问量,这样的话中间缓存就变得没有意义了。get_hits表示命中了多少次读取,即来memcached取到了多少有效数据;get_m isses表示没有命中的次数,即这次来取数据的时候,memcached并无你所查询的数据。若是没有清零统计数据的话,cmd_get = get_hits + get_misses。