以下是memcached安装和配置的简单实验。
1. memcached安装
用前面的方法克隆一台虚拟机主机名改为memcache,ip地址设为192.168.122.33
直接用yum安装
yum install libevent libevent nc -y yum install memcached -y检查memcached是否安装好 [root@memcache ~]# which memcached /usr/bin/memcached [root@memcache ~]# 2.运行memcached服务 memcached -m 16m -p 11212 -d -u root -c 8192检查memcached是否运行 [root@memcache ~]# ps -ef |grep memcached |grep -v grep root 1178 1 0 16:15 ? 00:00:00 memcached -m 16m -p 11212 -d -u root -c 8192用nc和telnet访问memcached root@memcache ~]# printf "set key1 0 0 15\r\nhello memcached\r\n" |nc 127.0.0.1 11212 STORED [root@memcache ~]# printf "get key1\r\n" |nc 127.0.0.1 11212 VALUE key1 0 15 hello memcached END [root@memcache ~]# yum install telnet -y [root@memcache ~]# telnet 127.0.0.1 11212 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. get key1 VALUE key1 0 15 hello memcached END set key2 0 0 7 mydream STORED get key2 VALUE key2 0 7 mydream END quit Connection closed by foreign host. [root@memcache ~]# 停止memcached服务 ps -ef |grep memcached |grep -v grep |awk '{print $2'} |xargs kill3.配置php访问memcached
在虚拟机nginx上进行安装,ip为192.168.122.32
wget -q http://pecl.php.net/get/memcache-2.2.7.tgz tar -xvf memcache-2.2.7.tgz cd memcache-2.2.7 /usr/local/php/bin/phpize ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config make [root@nginx memcache-2.2.7]# make install Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/ [root@nginx memcache-2.2.7]#进入/usr/local/php/lib目录编辑php.ini文件,在文件末尾增加
extension_dir="/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/" extension=memcache.so重启php
[root@nginx lib]# /usr/local/php/sbin/php-fpm -t [26-Sep-2016 15:57:29] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful [root@nginx etc]# pkill php-fpm [root@nginx etc]# ps -ef |grep php-fpm |grep -v grep [root@nginx etc]# /usr/local/php/sbin/php-fpm [root@nginx etc]#编辑/usr/local/nginx/html/www/testing_mem.php文件进行验证
vi testing_mem.php <?php $memcache = new Memcache; $memcache->connect('192.168.122.33',121212) or die("Could not connect Mc server"); $memcache->set("key", "hello memcache"); $get = $memcache->get("key"); echo $get; ?>