第1章 缓存数据库Memcache

1.1 为什么用缓存数据库

缓存数据库Memcache_第1张图片 

1.2 Memcached介绍

缓存数据库Memcache_第2张图片 

1.3 Memcached在企业中使用场景

1.3.1 应用场景一

缓存数据库Memcache_第3张图片

1.3.2 应用场景二

缓存数据库Memcache_第4张图片 

1.4 CookiesSession

缓存数据库Memcache_第5张图片 

1.5 Memcached分布式缓存集群

缓存数据库Memcache_第6张图片

1.5.1 普通哈希算法

缓存数据库Memcache_第7张图片

1.5.2 一致性哈希算法

缓存数据库Memcache_第8张图片

第2章 安装Memcached

2.1 服务端配置(Memcached

服务端环境

[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~]# hostname -I

10.0.0.21 172.16.1.21

 

Memcache用到了libevent这个库用于Socket的处理

yum install libevent libevent-devel nc -y


安装Memcached

yum -y install memcached


查看配置文件

cat /etc/sysconfig/memcached

 

[root@cache01 ~]# cat /etc/sysconfig/memcached

PORT="11211"

USER="memcached"                        

MAXCONN="1024"                        默认最大并发1024

CACHESIZE="64"                         内存用于以MB为单位的项目(默认为64 MB)

OPTIONS=""

查看启动文件

cat /usr/lib/systemd/system/memcached.service

 

启动服务

systemctl start memcached.service

 

[root@cache01 ~]# ss -lntup |grep 11211

udp    UNCONN     0      0         *:11211                 *:*                   users:(("memcached",pid=15119,fd=28))

udp    UNCONN     0      0        :::11211                :::*                   users:(("memcached",pid=15119,fd=29))

tcp    LISTEN     0      128       *:11211                 *:*                   users:(("memcached",pid=15119,fd=26))

tcp    LISTEN     0      128      :::11211                :::*                   users:(("memcached",pid=15119,fd=27))

 

注:memcached可以同时启动多个实例,端口不一致即可。

memcached -m 16m -p 11212 -d -u root -c 8192

参数说明:

- m           max内存用于以MB为单位的项目(默认为64 MB)

- p            监听TCP端口号(默认:11211)

- d            作为守护进程运行

- u            假设<用户名>的身份(只有在作为根运行时)

- c            最大并发连接(默认:1024)

2.2 Memcached使用

memcached存储方式:

key   <->   value

   name  <->   wuhaung

写入数据

printf "set key008 0 0 10\r\nwuhuang987\r\n"|nc 10.0.0.21 11211

[root@cache01 ~]# printf "set key008 0 5 10\r\nwuhuang987\r\n"|nc 10.0.0.21 11211

STORED

读取数据

printf "get key008\r\n"|nc 10.0.0.21 11211

[root@cache01 ~]# printf "get key008\r\n"|nc 10.0.0.21 11211

VALUE key008 0 10

wuhuang987

END

删除数据

printf "delete key008\r\n"|nc 10.0.0.21 11211

 

缓存数据库Memcache_第9张图片 

2.3 客户端部署(web服务器Memcache

web01wordpress准备好(可以访问),接下来在这台机器安装memcache客户端。

安装PHP memcache 扩展插件

cd /server/tools

wget http://pecl.php.net/get/memcache-2.2.5.tgz

tar zxvf memcache-2.2.5.tgz

cd memcache-2.2.5

/application/php/bin/phpize

./configure --enable-memcache --with-php-config=/application/php/bin/php-config --with-zlib-dir

make && make install

 

查看是否安装成功

[root@web01 memcache-2.2.5]# ls /application/php-5.5.32/lib/php/extensions/no-debug-non-zts-20121212/

memcache.so                                <--- memcache.so表示插件安装成功

 

配置memcache客户端使其生效

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

 

检测语法,重启php服务

pkill php

/application/php/sbin/php-fpm -t

/application/php/sbin/php-fpm

/application/php/bin/php -m|grep memcache          

参数说明:-m查看PHP支持哪些模块

 

编写测试Memcache文件(PHP代码测试)

[root@web01 blog]# cat >>/application/nginx/html/blog/mc.php<<'EOF'

$memcache = new Memcache;

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

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

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

echo $get_value;

?>

EOF

服务端验证成功

[root@cache01 ~]# printf "get key20180314\r\n"|nc 10.0.0.21 11211

VALUE key20180314 0 11

hello,world

END

2.4 web管理Memcached

2.4.1 配置web管理Memcached

下载pip网站程序

tar xf memadmin-1.0.12.tar.gz -C /application/nginx/html/blog/

浏览器访问http://blog.etiantian.org/memadmin

 

缓存数据库Memcache_第10张图片 

缓存数据库Memcache_第11张图片 

缓存数据库Memcache_第12张图片缓存数据库Memcache_第13张图片 

2.4.2 使用Memcache缓存WordPress博文数据

WordPress会自动检查在wp-content目录下是否有object-cache.php文件,如果有,直接调用它作为WordPress对象缓存机制。

注意:object-cache.php此类文件是由开发完成,并非运维的工作,其他的网站做缓存也是由开发写程序。

2.4.3 Memcached Session共享

方法1

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

方法2

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

使用这个功能,需要使用phpsession函数

修改php配置(设置session共享)

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

 

原配置:

1 session.save_handler = files

2 session.save_path = "/tmp"

 

修改为:

[root@web01 ~]# vim /application/php/lib/php.ini

1 session.save_handler = memcache

2 session.save_path = "tcp://10.0.0.21:11211"

 

修改完成之后要重启php服务

pkill php

/application/php/sbin/php-fpm -t

/application/php/sbin/php-fpm

2.5 Memcached在集群中session共享存储的优缺点

2.5.1 优点

1)读写速度上会比普通文件files速度快很多。

2)可以解决多个服务器共用session的难题。

2.5.2 缺点

1session数据都保存在memory中,持久化方面有所欠缺,但对session数据来说不是问题。

2)一般是单台,如果部署多台,多台之间数据无法同步。通过hash算法分配依然有session丢失的问题。

2.5.3 替代方案

1)可以用其他的持久化系统存储session,例如redisttserver来替代memcached.

2)高性能并发场景,cookies效率比session要好很多,因此,大网站都会用cookies解决会话共享的问题.

3)一些不好的方法:lvs-p,nginx  ip_hash,不推荐使用.

2.6 DedeCMS使用memcache问题

2.6.1 问题

配置session共享后访问不了DedeCMS后台。

原因:bbsblogsession是存在数据库的表中,而DedeCMSsession是存在一个目录下的文件中。

2.6.2 解决方法

运维的工作:准备环境

PHP默认把session存放在数据库(或缓存中),而不是存放在文件中

 

开发的工作:用环境

修改文件一:

[root@web01 include]# pwd

/application/nginx/html/www/include

[root@web01 include]# vim common.inc.php

135 //Session保存路径

136 $enkey = substr(md5(substr($cfg_cookie_encode,0,5)),0,10);

137 //$sessSavePath = DEDEDATA."/sessions_{$enkey}";

138 $sessSavePath = "tcp://10.0.0.21:11211";

139 if ( !is_dir($sessSavePath) ) mkdir($sessSavePath);

 

修改文件二:

[root@web01 include]# vim vdimgck.php

24 $enkey = substr(md5(substr($cfg_cookie_encode,0,5)),0,10);

25 //$sessSavePath = DEDEDATA."/sessions_{$enkey}";

26 $sessSavePath = "tcp://10.0.0.21:11211";

27 if ( !is_dir($sessSavePath) ) mkdir($sessSavePath);


DedeCMS直接使用memcache的共享,解决问题(即使用memcecha缓存DedeCMS的数据)