CentOS 7部署memcached缓存服务器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS 7部署memcached缓存服务器相关的知识,希望对你有一定的参考价值。

1.简介


1.1Memcached

  Memcached是一款开源的、高性能的纯内存缓存服务软件。

  mysql数据库属于磁盘上的数据库,数据的读写较慢;而Memcached数据库属于内存中的数据库,读写速度快,但数据容易丢失。Memcached天生不支持分布式集群,只能通过程序支持分布式存储。使用Memcached数据库,提高用户访问网站速度,降低MySQL数据库服务器压力,提高网站的并发访问,因此工作中,MySQL+Memcached搭配使用。


1.2Memcached工作过程


技术分享图片


2.系统环境准备

[[email protected] ~]# cat /etc/re

redhat-release  resolv.conf     

[[email protected] ~]# cat /etc/re

redhat-release  resolv.conf     

[[email protected] ~]# cat /etc/redhat-release 

CentOS Linux release 7.2.1511 (Core) 

[[email protected] ~]# uname -r

3.10.0-327.el7.x86_64

[[email protected] ~]# getenforce

Disabled

[[email protected] ~]# systemctl status firewalld.service 

● firewalld.service - firewalld - dynamic firewall daemon

   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)

   Active: inactive (dead)

[[email protected] ~]# ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 10.0.0.21  netmask 255.255.255.0  broadcast 10.0.0.255

        inet6 fe80::20c:29ff:fee1:ad7  prefixlen 64  scopeid 0x20<link>

        ether 00:0c:29:e1:0a:d7  txqueuelen 1000  (Ethernet)

        RX packets 3228  bytes 815585 (796.4 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 419  bytes 52728 (51.4 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 172.16.1.21  netmask 255.255.255.0  broadcast 172.16.1.255

        inet6 fe80::20c:29ff:fee1:ae1  prefixlen 64  scopeid 0x20<link>

        ether 00:0c:29:e1:0a:e1  txqueuelen 1000  (Ethernet)

        RX packets 0  bytes 0 (0.0 B)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 23  bytes 1698 (1.6 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536

        inet 127.0.0.1  netmask 255.0.0.0

        inet6 ::1  prefixlen 128  scopeid 0x10<host>

        loop  txqueuelen 0  (Local Loopback)

        RX packets 0  bytes 0 (0.0 B)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 0  bytes 0 (0.0 B)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


3.服务端部署Memcached服务


3.1安装memcached

[[email protected] ~]# yum install -y memcached


3.2启动memcached服务

[[email protected] ~]# systemctl start memcached.service


3.3测试

[[email protected] ~]# printf "set fengfyu 0 0 5\r\n12345\r\n"|nc 10.0.0.21 11211      ---写入数据

STORED

[[email protected] ~]# printf "get fengfyu 0 0 5\r\n12345\r\n"|nc 10.0.0.21 11211     ---读取数据

VALUE fengfyu 0 5

12345

END

ERROR


4.web服务器客户端部署memcached


4.1编译安装memcached

[[email protected] tools]# tar xf memcache-2.2.5.tgz 

[[email protected] tools]# cd memcache-2.2.5/

[[email protected] memcache-2.2.5]# /application/php/bin/ph

[[email protected] memcache-2.2.5]# /application/php/bin/phpize 

Configuring for:

PHP Api Version:         20121113

Zend Module Api No:      20121212

Zend Extension Api No:   220121212

[[email protected] memcache-2.2.5]# ./configure --enable-memcache --with-php-config=/application/php/bin/php-config --with-zlib-dir && make && make install


4.2使php与memcached关联

sed -i '$a extension=memcache.so' /application/php/lib/php.ini


4.3启动php

/application/php/sbin/php-fpm


4.4客户端测试

[[email protected] www]# cat /application/nginx/html/www/mc.php 

<?php

$memcache = new Memcache;

$memcache->connect('10.0.0.21', 11211) or die ("Could not connect");

$memcache->set('fengyu', 'hello,world');

$get_value = $memcache->get('fengyu');

echo $get_value;

?>

[[email protected] www]# printf "get fengyu\r\n"|nc 10.0.0.21 11211

VALUE fengyu 0 11

hello,world

END


5.web界面管理maceched


5.1解压memadmin包到/application/nginx/html/www/目录

[[email protected] tools]#  tar xf memadmin-1.0.12.tar.gz -C /application/nginx/html/www/


5.2浏览器访问

http://10.0.0.7/memadmin

技术分享图片

6.Memcached Session共享


6.1通过程序实现,web01只需要往memcahce写session,web02从memcahce读session,当作普通数据读写(更具有通用性)


6.1通过php的配置文件,php默认将session存储在文件中,修改为存储在memcached中

sed -i 's#session.save_handler = files#session.save_handler = memcache#;$a session.save_path = "tcp://10.0.0.21:11211"' /application/php/lib/php.ini

以上是关于CentOS 7部署memcached缓存服务器的主要内容,如果未能解决你的问题,请参考以下文章

在CentOS7上部署Memcached高性能内存缓存对象

使用memcache作为中间缓存区的步骤

CentOS 7 上部署Memcached 主主复制 + keepalived 高可用架构

memcache缓存服务器(安装-配置-测试篇)

LNMP动静分离&&memcache缓存服务器

Centos 7.5安装部署redis 5.0.0集群