linux memcached 安装

jopen 8年前

1.下载包

1.1.到http://memcached.org/下载linux下的memcached的安装包.

1.2.到http://libevent.org/ 下载安装memcached前比需要的包libevent-2.0.22我安装的是这个版本.

2.安装libevent-2.0.22

[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/lib --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

如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径。

我在启动过程中报这个错误找不到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。


来自: http://my.oschina.net/ambari/blog/602005