共计 3474 个字符,预计需要花费 9 分钟才能阅读完成。
1. 简介
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<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
[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
<?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
: