memcached服务器IP地址: 192.168.218.5
系统: CentOS7
源码包下载链接:链接:https://pan.baidu.com/s/17ZJvfKeX67zwKRYmYoFxjA
提取码:jfgr
安装环境依赖
yum -y install gcc gcc-c++ make
安装libevent
[root@localhost ~]# tar xf libevent-2.1.8-stable.tar.gz
[root@localhost ~]# cd libevent-2.1.8-stable/
[root@localhost libevent-2.1.8-stable]#
[root@localhost libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
[root@localhost libevent-2.1.8-stable]# make && make install
安装memcached
[root@localhost libevent-2.1.8-stable]# cd ..
[root@localhost ~]# tar -xf memcached-1.5.6.tar.gz
[root@localhost ~]# cd memcached-1.5.6/
[root@localhost memcached-1.5.6]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[root@localhost memcached-1.5.6]# make && make install
创建软链接&&开启memcached
[root@localhost ~]# ln -s /usr/local/memcached/bin/* /usr/local/bin/
[root@localhost ~]# memcached -d -m 32m -p 11211 -u root
11211是memcached的默认端口,故下面检查11211端口是否开启
[root@localhost ~]# netstat -antp| grep memcached
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 10040/memcached
tcp6 0 0 :::11211 :::* LISTEN 10040/memcached
连接本地memcached进行测试
[root@localhost ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
出现上面的输出,说明连接成功,使用quit退出
安装memcached客户端,客户端使用LAMP架构通过php访问memcached
故需先搭建LAMP环境,可参考我的博客:https://blog.csdn.net/weixin_43515220/article/details/103786092
安装autoconf
yum -y install autoconf
解压memcache客户端包
[root@localhost ~]# tar xf memcache-2.2.7.tgz
[root@localhost ~]# cd memcache-2.2.7/
进入目录,可以发现并每有configure文件,我们通过php来生成configure文件
[root@localhost memcache-2.2.7]# /usr/local/php5/bin/phpize
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
phpize的路径需要根据实际php安装的目录来指定,如果存在软链接或者环境变量能找到该命令,则不需要指定命令的绝对路径
下面编译安装
[root@localhost memcache-2.2.7]# ./configure --enable-memcache --with-php-config=/usr/local/php5/bin/php-config
[root@localhost memcache-2.2.7]# make && make install
查找memcache.so,查看是否存在/usr/local/php5/lib/php/extensions/no-debug-zts-20131226
[root@localhost ~]# cd /usr/local/php5/lib/php/extensions/no-debug-zts-20131226/
[root@localhost no-debug-zts-20131226]# ls
memcache.so opcache.so
若存在,则修改php.ini文件,添加memcache扩展,php.ini通常在php的安装目录下
extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-zts-20131226"
extension = memcache.so
添加测试php页面,将下面页面放到httpd的文件夹下,这里将LAMP中测试php页面index.php中的内容直接替换
<?php
$memcache = new Memcache();
$memcache->connect('192.168.218.5',11211);
$memcache->set('key','Memcache test successful!',0,60);
$result = $memcache->get('key');
unset($memcache);
echo $result;
?>
注意,将php页面中的memcached服务器的IP地址根据实际情况进行替换
重启httpd服务
systemctl restart httpd
使用浏览器访问客户端
http://192.168.218.4/index.php
测试成功!