linux系统安装Memcache

Linux系统安装memcached 首先要先安装libevent库。

centos  下执行

yum install libevent libevent-devel

查看memcached 是否已经安装  

which  memcached    //如果已经安装  输出类似“/usr/bin/memcached”

安装memcached 执行:

yum install memcached  

安装php memcached 扩展 php-pecl-memcached 

yum -y install php72w-pecl-memcached  //我的PHP版本是php7.2的,如果版本不对会报错误:php72w-common conflicts with php-common-5.4.16-45.el7.x86_64错误

查看是否安装php-pecl-memcached 扩展

php  -m  |  grep  memcache     // 安装成功会输出memcached, 否则没有输

设置开机启动

sudo systemctl enable memcached

启动memcached

1
sudo systemctl start memcached

启动memcached 服务,在终端输入

1
# /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 -p 11211 -c 256 -P /tmp/memcached.pid

查看memcached 监听情况

1
lsof -i tcp:11211   <br>输出<br>COMMAND    PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME<br>memcached 5821 vagrant   26u  IPv6  42350      0t0  TCP localhost:memcache (LISTEN)<br>memcached 5821 vagrant   27u  IPv4  42351      0t0  TCP php-site:memcache (LISTEN)

说明监听11211端口成功

会在/usr/lib64/php/modules/ 下生成memcached.so

在php.ini中开启 extension=/usr/lib64/php/modules/memcached.so

1
[Memcache]<br>extension=/usr/lib64/php/modules/memcached.so

 在php.ini文件中添加memcache扩展文件后,在xshell中执行会报错,但不影响web浏览器端效果,

然后重启php-fpm

1
sudo systemctl restart php-fpm

phpinfo()中能够看到memcached

测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
 
$mem = new Memcached;
 
$mem->connect( "127.0.0.1" , 11211);
 
$mem-> set ( 'key' , 'hello test!' , 0, 60);
 
$val = $mem-> get ( 'key' );
 
echo $val;
 
?>

上面测试代码有问题,我的测试代码:

<?php

$mem = new Memcached;
 
$mem->addServer("127.0.0.1", 11211);
 
$mem->set('key', 'hello test!', 10);
 
$val = $mem->get('key');
 
echo $val;

 

 

原文地址:https://www.cnblogs.com/lxwphp/p/15453268.html