> CentOS > CentOS教程 >

centos7安装配置memcached

1.memcached的安装

yum install memcached

memcached -h

vim /etc/sysconfig/memcached

PORT=”11211″

USER=”memcached”

MAXCONN=”1024″

CACHESIZE=”64″

OPTIONS=”"

systemctl restart memcached

systemctl start memcached

systemctl enable memcached

systemctl status memcached

systemctl stop memcached

memcached-tool 127.0.0.1:11211 stats

memcached-tool 127.0.0.1:11211

#  Item_Size  Max_age   Pages   Count   Full?  Evicted Evict_Time OOM

2     120B    727s     1     7     yes      0      0   0

4     192B    7243s    1     3     yes      0      0   0

5     240B    72693s   1     14     yes      0      0   0

9     600B     0s     1     0     yes       0      0   0

10    752B     79s     1     1     yes      0      0   0

#       slab class编号

Item_Size  chunk大小

Max_age   LUR内最旧的记录生存时间

Pages    分配给slab的页数

Count    slab内的记录数

Full?    slab内是否含有空闲chunk


2./etc/PHP.d/memcache.ini详解

; ----- Enable memcache extension module

extension=memcache.so

; ----- Options for the memcache module

; see http://www.php.NET/manual/en/memcache.ini.php

;  Whether to transparently failover to other servers on errors

;memcache.allow_failover=1    (连接出错时Memcache扩展是否故障转移到其他服务器上默认为1)

;  Data will be transferred in chunks of this size

;memcache.chunk_size=32768         (控制数据传输的大小)

;  Autocompress large data

;memcache.compress_threshold=20000

;  The default TCP port number to use when connecting to the memcached server

;memcache.default_port=11211       (设置连接Memcache所使用的TCP端口)

;  Hash function {crc32, fnv}

;memcache.hash_function=crc32

;  Hash strategy {standard, consistent}

;memcache.hash_strategy=consistent   (哈希策略)

;  Defines how many servers to try when setting and getting data.

;memcache.max_failover_attempts=20   (限制连接到持久性数据或检索数据的服务器数目)

;  The protocol {ascii, binary} : You need a memcached >= 1.3.0 to use the binary protocol

;  The binary protocol results in less traffic and is more efficient

;memcache.protocol=ascii

;  Redundancy : When enabled the client sends requests to N servers in parallel

;memcache.redundancy=1

;memcache.session_redundancy=2

;  Lock Timeout

;memcache.lock_timeout = 15

; ----- Options to use the memcache session handler

; RPM note : save_handler and save_path are defined

; for mod_php, in /etc/httpd/conf.d/php.conf

; for php-fpm, in /etc/php-fpm.d/*conf

;  Use memcache as a session handler

;session.save_handler=memcache

;  Defines a comma separated of server urls to use for session storage;session.save_path="tcp://localhost:11211?persistent=1&weight=1&timeout=1&retry_interval=15"


2.php操作memcache的使用测试

2.1创建KEY值数据

vi test1.php

<?php

$mem = new memcache;

$mem->connect("127.0.0.1", 11211);

$mem->set('key20', 'This is a test!', 0, 60);

$val = $mem->get('key20');

echo $val;

?>

2.2替换KEY值数据

vi test2.php

<?php

$mem = new memcache;

$mem->connect("127.0.0.1", 11211);

$mem->replace('key20', 'This is replace value', 0, 60); 

$val = $mem->get('key20');

echo $val;

?>

2.3删除KEY值数据

vi test3.php

<?php

$mem = new memcache;

$mem->connect("127.0.0.1", 11211);

$mem->delete('key20');

$val = $mem->get('key20');

echo $val;

?>

php -f test1.php(执行php -h看帮助)

命令帮助:

Memcache::add // 添加一个值,如果已经存在,则返回false

Memcache::addServer // 添加Memcache地址

Memcache::close // 关闭一个Memcache的连接

Memcache::connect // 打开一个到Memcache的连接

Memcache::decrement // 对保存的某个key中的值进行减法操作

Memcache::delete // 删除一个Memcache上的key值

Memcache::flush // 刷新所有Memcache上保存的项目(类似于删除所有的保存的项目)

Memcache::get // 从Memcache上获取一个key值

Memcache::getExtendedStats // 获取进程池中所有进程的运行系统统计

Memcache::getServerStatus // 获取运行服务器的参数

Memcache::getStats //获取当前Memcache服务器运行的状态

Memcache::getVersion // 返回运行的Memcache的版本信息

Memcache::increment // 对保存的某个key中的值进行加法操作

Memcache::pconnect // 打开一个到Memcache的长连接

Memcache::replace // 替换一个已经存在Memcache服务器上的项目(功能类似Memcache::set)

Memcache::set // 向Memcache添加一个值,如果已经存在,则覆写

Memcache::setCompressThreshold // 对大于某一大小的数据进行压缩

Memcache::setServerParams // 在运行时修改服务器的参数


2.查看memcached性能状态

2.1查看服务器性能信息

# telnet localhost 11211

stats(此处输入stats命令)

STAT pid 1277            (进程ID)

STAT uptime 176145         (服务器运行秒数,秒)

STAT time 1448592684       (服务器当前unix时间戳)

STAT version 1.4.15        (服务器版本)

STAT libevent 2.0.21-stable

STAT pointer_size 64       (操作系统字大小 32/64位)

STAT rusage_user 3.869494    (进程累计用户时间,秒)

STAT rusage_system 4.636292   (进程累计系统时间,秒)

STAT curr_connections 11     (当前打开连接数)

STAT total_connections 21    (曾打开的连接总数)

STAT connection_structures 12  (服务器分配的连接结构数)

STAT reserved_fds 20

STAT cmd_get 483          (执行get命令总数)

STAT cmd_set 144          (执行set命令总数)

STAT cmd_flush 0          (指向flush_all命令总数)

STAT cmd_touch 0

STAT get_hits 369         (get未命中次数)

STAT get_misses 114        (get未命中次数)

STAT delete_misses 0       (delete未命中次数)

STAT delete_hits 0        (delete命中次数)

STAT incr_misses 0        (incr未命中次数)

STAT incr_hits 0         (incr命中次数)

STAT decr_misses 0        (decr未命中次数)

STAT decr_hits 0         (decr命中次数)

STAT cas_misses 0         (cas未命中次数)

STAT cas_hits 0          (cas命中次数)

STAT cas_badval 0         (使用擦拭次数)

STAT touch_hits 0

STAT touch_misses 0

STAT auth_cmds 0

STAT auth_errors 0

STAT bytes_read 82819      (读取字节总数)

STAT bytes_written 104266    (写入字节总数)

STAT limit_maxbytes 67108864  (分配的内存数(字节))

STAT accepting_conns 1      (目前接受的连接数)

STAT listen_disabled_num 0

STAT threads 4           (线程数)

STAT conn_yields 0

STAT hash_power_level 16

STAT hash_bytes 524288

STAT hash_is_expanding 0

STAT bytes 4789           (存储item字节数)

STAT curr_items 20         (item个数)

STAT total_items 144       (item总数)

STAT expired_unfetched 15

STAT evicted_unfetched 0

STAT evictions 0          (获取空间删除item的总数)

STAT reclaimed 36

END

quit(此处输入quit退出memcached状态查看)

2.2查看每个slab中存储items的详细信息

# telnet localhost 11211

stats items(此处输入stats命令)

STAT items:4:number 3

STAT items:4:age 2140(当前数据中缓存最久的时间,一般小于evicted_time(不为0)时间)

STAT items:4:evicted 0(如果evicted不是0,说明slab开始提前剔除数据,需要注意)

STAT items:4:evicted_nonzero 0

STAT items:4:evicted_time 0(最后被剔除数据的缓存时间,不是发生LRU就memcached负载过量)

STAT items:4:outofmemory 0

STAT items:4:tailrepairs 0

STAT items:4:reclaimed 6

STAT items:4:expired_unfetched 1

STAT items:4:evicted_unfetched 0

STAT items:5:number 15

STAT items:5:age 67590

STAT items:5:evicted 0

STAT items:5:evicted_nonzero 0

STAT items:5:evicted_time 0

STAT items:5:outofmemory 0

STAT items:5:tailrepairs 0

STAT items:5:reclaimed 22

STAT items:5:expired_unfetched 10

STAT items:5:evicted_unfetched 0

STAT items:10:number 1

STAT items:10:age 283

STAT items:10:evicted 0

STAT items:10:evicted_nonzero 0

STAT items:10:evicted_time 0

STAT items:10:outofmemory 0

STAT items:10:tailrepairs 0

STAT items:10:reclaimed 13

STAT items:10:expired_unfetched 7

STAT items:10:evicted_unfetched 0

END

2.3查看slab内存分配信息

# telnet localhost 11211

stats slabs(此处输入stats命令)

STAT 4:chunk_size 192    (当前slab每个chunk的大小,固定值)

STAT 4:chunks_per_page 5461(每个page能够存放的chunk数,固定值)

STAT 4:total_pages 1     (分配给当前slab的page总数,单位M。slab剔除严重,该值会太少)

STAT 4:total_chunks 5461  (当前slab最多能够存放的chunk数,=chunks_per_page*total_pages)

STAT 4:used_chunks 3     (已经被占用的chunks总数)

STAT 4:free_chunks 5458   (过期数据空出的chunk里还没有被使用的chunk数)

STAT 4:free_chunks_end 0  (新分配但还没有被使用的chunk数)

STAT 4:mem_requested 555

STAT 4:get_hits 136

STAT 4:cmd_set 9

STAT 4:delete_hits 0

STAT 4:incr_hits 0

STAT 4:decr_hits 0

STAT 4:cas_hits 0

STAT 4:cas_badval 0

STAT 4:touch_hits 0

STAT 5:chunk_size 240

STAT 5:chunks_per_page 4369

STAT 5:total_pages 1

STAT 5:total_chunks 4369

STAT 5:used_chunks 14

STAT 5:free_chunks 4355

STAT 5:free_chunks_end 0

STAT 5:mem_requested 3158

STAT 5:get_hits 158

STAT 5:cmd_set 43

STAT 5:delete_hits 0

STAT 5:incr_hits 0

STAT 5:decr_hits 0

STAT 5:cas_hits 0

STAT 5:cas_badval 0

STAT 5:touch_hits 0

STAT 10:chunk_size 752

STAT 10:chunks_per_page 1394

STAT 10:total_pages 1

STAT 10:total_chunks 1394

STAT 10:used_chunks 1

STAT 10:free_chunks 1393

STAT 10:free_chunks_end 0

STAT 10:mem_requested 646

STAT 10:get_hits 104

STAT 10:cmd_set 105

STAT 10:delete_hits 0

STAT 10:incr_hits 0

STAT 10:decr_hits 0

STAT 10:cas_hits 0

STAT 10:cas_badval 0

STAT 10:touch_hits 0

STAT active_slabs 3

STAT total_malloced 3145360

END


报错1:执行memcached -vv报错can't run as root without the -u switch

解决1:memcached -vv -u root


3.增加memcache缓存日志

yum -y install php-pecl-memcache

如果是PHP56版本的应该运行

yum -y install php56w-pecl-memcache

防火墙放开11211

firewall-cmd --permanent --zone=public --add-port=11211/tcp

检查端口是否开放
echo stats | nc memcache_host_name_or_ip 11211

增加日志

/bin/memcached -d -m 512 -l 127.0.0.1 -p 10000 -u root -vv >> /tmp/memcached.log 2>&1

tail -f /tmp/memcached.log


4.memcache缓存MySQL的测试页面

vi mysql-memcache.php

<?php

$host = 'IP:3306';

$user = 'guowang';

$passwd = '123456';

$db = 'test';

$conn = mysql_connect($host,$user,$passwd);

mysql_select_db($db,$conn);

$sql = 'select * from t order by id desc';

$result = mysql_query($sql,$conn);

while ($row = mysql_fetch_array($result,MYSQL_ASSOC)){

    $test_key[] = $row;

}

$sql = md5($sql);

$mem = new Memcache;

$mem->connect("IP", 11211);

$mem->set($sql,$test_key, MEMCACHE_COMPRESSED, 600);

print_r($mem->get($sql));

?>


5.memcached禁止外网访问

lnmp默认安装的memcached是可以外网访问的,对于我这个小站来说外网不会用到,所以只允许内网用。修改文件vi /etc/init.d/memcached

PORT=11211

USER=root

MAXCONN=1024

CACHESIZE=32

OPTIONS=""

IP=127.0.0.1

RETVAL=0

prog="memcached"

start () {

    echo -n $"Starting $prog: "

    memcached -d -l $IP -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /var/run/memcached.pid $OPTIONS

    RETVAL=$?

    echo

    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached

}

如果有安装iptables的话可以用iptables来禁止外网访问


6.未完成



(责任编辑:IT)