CentOS7部署Memcached缓存服务器

1.简介

创新互联建站是一家网站设计公司,集创意、互联网应用、软件技术为一体的创意网站建设服务商,主营产品:成都响应式网站建设公司品牌网站建设网络营销推广。我们专注企业品牌在网站中的整体树立,网络互动的体验,以及在手机等移动端的优质呈现。网站制作、网站建设、移动互联产品、网络运营、VI设计、云产品.运维为核心业务。为用户提供一站式解决方案,我们深知市场的竞争激烈,认真对待每位客户,为客户提供赏析悦目的作品,网站的价值服务。

1.1Memcached

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

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

1.2Memcached工作过程

2.系统环境准备

[root@cache01 ~]# cat /etc/re

RedHat-release  resolv.conf   

[root@cache01 ~]# cat /etc/re

redhat-release  resolv.conf   

[root@cache01 ~]# cat /etc/redhat-release

CentOS Linux release 7.2.1511 (Core)

[root@cache01 ~]# uname -r

3.10.0-327.el7.x86_64

[root@cache01 ~]# getenforce

Disabled

[root@cache01 ~]# 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)

[root@cache01 ~]# ifconfig

eth0: flags=4163  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

        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  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

        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  mtu 65536

        inet 127.0.0.1  netmask 255.0.0.0

        inet6 ::1  prefixlen 128  scopeid 0x10

        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

[root@cache01 ~]# yum install -y memcached

3.2启动memcached服务

[root@cache01 ~]# systemctl start memcached.service

3.3测试

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

STORED

[root@cache01 ~]# 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

[root@web01 tools]# tar xf memcache-2.2.5.tgz

[root@web01 tools]# cd memcache-2.2.5/

[root@web01 memcache-2.2.5]# /application/php/bin/ph

[root@web01 memcache-2.2.5]# /application/php/bin/phpize

Configuring for:

PHP Api Version:        20121113

Zend Module Api No:      20121212

Zend Extension Api No:  220121212

[root@web01 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客户端测试

[root@web01 www]# cat /application/nginx/html/www/mc.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;

?>

[root@web01 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/目录

[root@web01 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

文章名称:CentOS7部署Memcached缓存服务器
文章源于:http://www.zyruijie.cn/qtweb/news18/6118.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联