Centos7下安装memcached

1.

which memcached                                //如果已经安装,会有“/usr/bin/memcached”类似的输出
memcached -h                                     //memcache帮助列表
php -m | grep memcache                     //如果已经安装,会显示memcache,否则没有输出

可以用上面的三个命令检查一下是不是已经安装过memcache了

2,yum search memcached                      //检查有没有安装包,有了可以执行第三部
3,yum -y install memcached
4,yum -y install php-pecl-memcache       //memcache关联php,如果已经安装了php7需要先卸载掉
5,可以用第一步的方法验证是否安装成功

6,启动memcache的服务器:

memcached -d -m 100 -u root -l 127.0.0.1 -p 11211 -c 512 -P /tmp/memcached.pid

参数说明:
-d选项是启动一个守护进程;
-m是分配给memcache使用的内存数量,单位是mB,我这里是100mB;
-u是运行memcache的用户,我这里是root;
-l是监听的服务器IP地址我这里指定了服务器的IP地址192.168.0.100;
-p是设置memcache监听的端口,我这里设置了11211,最好是1024以上的端口;
-c选项是最大运行的并发连接数,默认是1024,我这里设置了512,按照你服务器的负载量来设定;
-P是设置保存memcache的pid文件,我这里是保存在 /tmp/memcached.pid;
7,pecl install memcache          //如果需要php扩展,就用这个命令,不需要就不用安装了
8,第七部会提示“ERROR: `phpize' failed”这样的报错,再安装php-devel:

yum install php-devel

9,配置php.ini文件:
打开/etc/php.ini文件,添加一行来载入memcache扩展:

extension=memcache.so

如果执行php,有如下提示

[root@xxx ~]# php
PHP Warning:  Module 'memcache' already loaded in Unknown on line 0

说明已经载入过了,不需要再次添加,所以需要把上面添加的删掉

10,设置开机启动:

chkconfig memcached on

11,启动,停止,重启

service memcached start
service memcached stop
service memcached restart

12,运行phpinfo()的探针显示正确的结果应该有类似下图的memcache配置说明:

[root@xxx php-memcached]# phpinfo()
> ^C

13,Memcache环境测试:
运行下面的php文件,如果有输出This is a test!,就表示环境搭建成功。(确保memcached已经启动了)

<?php
$mem = new Memcache;
$mem->connect("127.0.0.1", 11211);
$mem->set("key", "This is a test!", 0, 60);
$val = $mem->get("key");
echo $val;
echo "
";
?>

原文地址:https://www.cnblogs.com/sea-stream/p/10370840.html