linux 平台,
测试机环境如下
[root@m01 ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@m01 ~]# uname -r
2.6.32-696.el6.x86_64
需要先安装libevent, 连接memechched工具nc
yum install libevent libevent-devel nc -y
yum 安装版本比较低,但是不影响使用,要想跟高版本需要编译安装.
编译安装命令如下:
wegt +网址…版本.tar.gz
tar xf memechched1.1.1.tar.gz
cd memcached1.1.1.
./comfigure
make && make install
ps:.tar.gz的文件为服务器端的源代码软件,.tgz的为客户端源代码软件.
yum install memcached -y
启动一个实例
[root@m01 ~]# memcached -m 16m -p11211 -d -u root -c 8192
echo "memcached -m 16m -p11211 -d -u root -c 8192" >>/etc/rc.local
[root@m01 ~]# lsof -i:11211
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
memcached 25784 root 26u IPv4 161384 0t0 TCP *:memcache (LISTEN)
memcached 25784 root 27u IPv6 161385 0t0 TCP *:memcache (LISTEN)
memcached 25784 root 28u IPv4 161388 0t0 UDP *:memcache
memcached 25784 root 29u IPv6 161389 0t0 UDP *:memcache
[root@m01 ~]# ps -ef|grep memcached |grep -v grep
root 25784 1 0 09:08 ? 00:00:00 memcached -m 16m -p11211 -d -u root -c 8192
命令参数:memcache -h 查看
-d daemon 守护进程
-u 指定用户
-l 指定监听服务器IP,可以不设置
-p(小p) 指定tcp端口号,默认11211
-P(大p)设置保存pid到指定文件
-m 指定memcached 服务可以缓存数据的最大内存,默认64MB
-c 最大的并发连接数,默认是1024
向memcached 里面写入数据
[root@m01 ~]# printf "set key1 0 0 6\r\njieyimeimei\r\n" |nc 172.16.1.61 11211
CLIENT_ERROR bad data chunk
ERROR
[root@m01 ~]# printf "set key1 0 0 11\r\njieyimeimei\r\n" |nc 172.16.1.61 11211
STORED
后面的字节是11,填6就会报错
从memcached 里面读数据
[root@m01 ~]# printf "set key2 0 0 11\r\njieyimeimei\r\n" |nc 172.16.1.61 11211
STORED
[root@m01 ~]# printf "delete key1 \r\n" |nc 172.16.1.61 11211
DELETED
[root@m01 ~]# printf "get key1 \r\n" |nc 172.16.1.61 11211
END
[root@m01 ~]# printf "get key2 \r\n" |nc 172.16.1.61 11211
VALUE key2 0 11
jieyimeimei
END
通过yum telnet 向memcached 写入数据
[root@m01 ~]# rpm -qa telnet
telnet-0.17-48.el6.x86_64
[root@m01 ~]# telnet 172.16.1.61 11211
Trying 172.16.1.61...
Connected to 172.16.1.61.
Escape character is '^]'.
set user01 0 0 7
oldgirl
STORED
get user01
VALUE user01 0 7
oldgirl
END
delete user01
DELETED
get user01
END
quit
Connection closed by foreign host.
关闭memcached方法:
killall memcached 或 pkill memcached
配置客户端memcached
cd进入对应的存放工具目录
wget -q http://pecl.php.net/get/memcache-2.2.7.tgz
ls |grep memcache
cd memcache-2.2.7.tgz
tar xf memcache-2.2.7.tgz
cd memcache-2.2.7
/application/php/bin/phpize
./configure --enable-memcache --with-php-config=/application/php/bin/php-config
echo $?
make && make install
修改php配置文件php.ini,加入客户端的配置
cd /application/php/lib
vim php.ini
把下面两行加入末尾
extension_dir = "/application/php-5.5.32/lib/php/extensions/no-debug-non-zts-20121212/"
extension=memcache.so
重启php-fpm使php配置永久生效
[root@web01 lib]# /application/php/sbin/php-fpm -t
[19-Aug-2017 08:46:16] NOTICE: configuration file /application/php-5.5.32/etc/php-fpm.conf test is successful
重启fpm
pkill php-fpm
编写简单的php程序连接的memcached测试脚本
在blog站点目录下面创建一个
[root@web01 blog]# vim men.php
<?php
$memcache =new Memcache;
$memcache->connect('10.0.0.61', 11211) or die ("could not conne
ct Mc server");
$memcache->set ('key', 'jieyimeimei');
$get = $memcache->get ('key');
echo $get
?>
"men.php" 7L, 219C written
[root@web01 blog]# /application/php/bin/php men.php
jieyimeimei
说明成功
通过memadmin php 工具展示memcached状态信息
下载地址 http://www.junopen.com/memadmin/
[root@web01 blog]# cd /home/oldboy/tools/
[root@web01 tools]# rz -E
rz waiting to receive.
[root@web01 tools]# tar xf memadmin-1.0.12.tar.gz
[root@web01 tools]# mv memadmin /application/nginx/html/blog/
打开浏览器http://blog*.org/memadmin/就可以看到了