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浏览器访问
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
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx