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
[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
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
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
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 —写入数据
[root@cache01 ~]# printf “get fengfyu 0 0 5\r\n12345\r\n”|nc 10.0.0.21 11211 —读取数据
VALUE fengfyu 0 5
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
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下Memcached的安装步骤是什么呢?
一、检查libevent首先检查系统中是否安装了libeventShell代码afe59b9ee7adrpm -qa|grep libevent如果安装了则查看libevent的安装路径,后续安装时需要用到Shell代码rpm -ql libevent如果没有安装,则先安装libevent安装libevent:1.首先下载libevent安装包Shell代码wget~provos/2.解压缩安装包Shell代码tar zxvf -C /usr/local/3.进入解压后的目录Shell代码cd libevent-1.4.12-stable/4.配置编译、安装Shell代码./configure -prefix=/usr/libeventmakemake install安装完libevent之后为了让动态链接库为系统所共享,需要执行以下动态链接库的管理命令ldconfig不过在执行ldconfig命令之前需要注意以下libevent的安装目录。 具体参见:安装完libevent后正式开始安装memcached1.首先是下载memcached的安装包Shell代码wget修改安装包的执行操作权限Shell代码chmod 777 3.解压安装包到指定目录Shell代码tar zxvf -C /usr/local4.进入解压后的目录Shell代码cd /usr/local/memcached-1.4.15/5.配置、编译、安装注:configure时需要指定libevent的安装路径Shell代码./configure -with-libevent=/usr/libevent/ -prefix=/usr/local/memcachedmakemake install6.安装成功后启动一下看安装是否成功Shell代码/usr/local/memcached/bin/memcached -d -m 10m -p -u root启动参数介绍如下:和上面的命令不对应-d选项是启动一个守护进程,-m是分配给Memcache使用的内存数量,单位是MB,这里是10MB,-u是运行Memcache的用户,这里是root,-l是监听的服务器IP地址,如果有多个地址的话,这里指定了服务器的IP地址192.168.0.200,-p是设置Memcache监听的端口,这里设置了,最好是1024以上的端口,-c选项是最大运行的并发连接数,默认是1024,这里设置了256,按照服务器的负载量来设定,-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/,也可以启动多个守护进程,不过端口不能重复。 如果要给memcached上配置日志,参见:附上一个memcached启动的shell脚本Shell代码#!/bin/shecho Start to start memcached server $(date)=/usr/local/memcached/bin/memcachedusage(){ echo usage: `basename $0` port}if [ -n $1 ]then{ pid=`ps aux|grep memcached|grep $1 |grep -v grep|awk {print $2}` if [ -n $pid ] then {sleep 2echo kill memcached which port is $1 beginecho pid:$pidkill -9 $pidecho kill memcached which port is $1 endsleep 2 } fi echo begin to start memcached in port $1 LOG_FILE=/var/log/memcached/memcached_$ rm -f $LOG_FILE $MEMCACHED -d -m 2048 -p $1 -u root -vv >> $LOG_FILE 2>&1 echo start memcached end tail -f $LOG_FILE}else{ usage exit 1}fi把该脚本命名为执行:./ 其中为memcached的启动端口,也可以通过自定义的端口来启动查看是否启动:ps aux|grep memcached查看版本 : cd /etc/init.d/memcached -h或者:[nihaoya@SHANGH-39-DX-APP ~]# telnet 127.0.0.1 Trying to character is ^]
memcached可以持久化吗
memcached 是缓存系统,通过名字就可以看出来,官网也明确说了(Free & open source, high-performance, distributed memory object caching system),之所以是缓存系统,就说明它不会作为可靠的数据存储,所以并不支持持久化。 另一个是redis,他是一个存储系统,官网也说了。 只不过redis是在内存中存储的,所以速度快,因为是存储系统,所以可以作为一个可靠的数据存储系统。 支持持久化。
win10怎么安装memcache缓存服务
首先打开 win10 运行命令,可以在开始菜单中打开,也可以直接使用组合快捷键 「win + r」快速打开,如图所示。 打开运行命令窗后,在打开后面键入 cmd 并点击确定进入。 之后可以进入 cmd 命令操作窗口,此时我们键入dns缓存清理命令 ipconfig/flushdns 并按键盘上的 「enter」回车键,确认运行dns缓存清理命令。 运行后,看到“已成功刷新 dns 解析缓存”提示,则说明电脑已经成功刷新了dns缓存,如图。 另外大家还可以在以上命令框中,输入ipconfig /displaydns这个命令。 6点击确定来查看一下本机已经缓存了哪些dns信息。
发表评论