Memcache安装使用 linux系统

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

sudo systemctl start memcached

启动memcached 服务,在终端输入

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

查看memcached 监听情况

 lsof -i tcp:11211   
输出
COMMAND    PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
memcached 5821 vagrant   26u  IPv6  42350      0t0  TCP localhost:memcache (LISTEN)
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

[Memcache]
extension=/usr/lib64/php/modules/memcached.so

然后重启php-fpm

sudo systemctl restart php-fpm

phpinfo()中能够看到memcached

测试:

<?php

$mem = new Memcached;

$mem->connect("127.0.0.1", 11211);

$mem->set('key', 'hello test!', 0, 60);

$val = $mem->get('key');

echo $val;

?>

猜你喜欢

转载自www.cnblogs.com/muscles/p/9992172.html