memcached高缓存搭建

memcached高缓存搭建
搭建服务端和客户端,让服务端去链接客户端
web客户端:192.168.247.161
服务端:192.168.247.160

[[email protected] ~]# hostnamectl set-hostname client
[[email protected] ~]# su
[[email protected] ~]# 
[[email protected] ~]# hostnamectl set-hostname server
[[email protected] ~]# su
[[email protected] ~]# 

搭建服务端
部署libevent 事件通知库

[[email protected] ~]# mkdir /abc
mkdir: cannot create directory ‘/abc’: File exists
[[email protected] ~]# mount.cifs //192.168.254.10/linuxs /abc
Password for [email protected]//192.168.254.10/linuxs:
[[email protected] ~]# cd /abc
[[email protected] abc]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt
[[email protected] abc]# tar zxvf memcached-1.5.6.tar.gz -C /opt
[[email protected] abc]# cd /opt
[[email protected] opt]# ls
data  libevent-2.1.8-stable  memcached-1.5.6  nginx-1.12.2  rh
[[email protected] opt]# cd libevent-2.1.8-stable/
[[email protected] libevent-2.1.8-stable]# yum install gcc gcc-c++ make -y
[[email protected] libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
[[email protected] libevent-2.1.8-stable]# make && make install

libevent安装完毕,接下来安装memcached

[[email protected] libevent-2.1.8-stable]# cd /opt/memcached-1.5.6/
[[email protected] memcached-1.5.6]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
[[email protected] memcached-1.5.6]# make && make install
[[email protected] memcached-1.5.6]# ln -s /usr/local/memcached/bin/* /usr/local/bin/

命令注释:

  • -d 指定守护进程
  • -m 32m 指定缓存32兆
  • -p 11211 指定端口
  • -u root 指定用户管理
    [[email protected] memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root
    [[email protected] memcached-1.5.6]# netstat -natp | grep memc
    tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      115996/memcached
    tcp6       0      0 :::11211                :::*                    LISTEN      115996/memcached
    [[email protected] memcached-1.5.6]# systemctl stop firewalld
    [[email protected] memcached-1.5.6]# setenforce 0

    先在本地连接这个缓存数据库,看看是否可以正常连接

    [[email protected] memcached-1.5.6]# yum install telnet -y
    [[email protected] memcached-1.5.6]# telnet 127.0.0.1 11211
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    Escape character is ‘^]‘.

    add 添加
    键值名称 username
    0 代表不设置序列号
    0 无时间要求,不设置过期时间
    7 字节长度,接下来如果输入的字节长度不符合要求,就会报错

    add username 0 0 7
    1234567
    STORED
    add users 0 0 7
    123
    STORED
    ERROR

    查看输入的键值

    get username
    VALUE username 0 7
    1234567
    END

    value 后面的1 是更新因子,如果更新一次,就会自加一

    gets username
    VALUE username 0 7 1
    1234567
    END
    set username 0 0 8
    12345678
    STORED
    gets username
    VALUE username 0 8 3
    12345678
    END

    replace 是更新的意思,如果当前的键值名称不存在,就会更新失败
    set 也是更新的意思,如果当前的键值名称不存在,会新建一个键值

    replace school 0 0 2
    NOT_STORED
    get school
    END
    replace username 0 0 9
    123456789
    STORED
    gets username
    VALUE username 0 9 4
    123456789
    END

    cas 更新,依据更新因子进行更新,相同则更新

    cas username 0 0 7 4
    1234567
    STORED
    gets username
    VALUE username 0 7 5
    1234567
    END
    cas username 0 0 8 2
    12345678
    EXISTS
    gets username
    VALUE username 0 7 5
    1234567
    END

    键值后追加数据

    append username 0 0 4
    appe
    STORED
    get username
    VALUE username 0 11
    1234567appe
    END

    键值前追加数据

    prepend username 0 0 5
    prepe
    STORED
    get username
    VALUE username 0 16
    prepe1234567appe
    END

    删除键值

    delete username
    DELETED
    get username
    END

    stats 显示状态信息
    清除所有缓存数据 flush_all

    flush_all
    OK
    get users
    END

    退出:quit

    quit
    Connection closed by foreign host.
    [[email protected] memcached-1.5.6]# 

    下一步安装客户端,安装客户端首先要部署LAMP架构环境,LAMP架构部署详细过程可查看博客:https://blog.51cto.com/14557905/2458323

LAMP安装完毕,接下来安装memcacahe客户端

[[email protected] abc]# yum install autoconf -y
[[email protected] htdocs]# cd /abc
[[email protected] abc]# tar zxvf memcache-2.2.7.tgz -C /opt
[[email protected] abc]# cd /opt
[[email protected] opt]# ls
httpd-2.4.29  memcache-2.2.7  mysql-5.6.26  package.xml  php-5.6.11  rh
[[email protected] opt]# cd memcache-2.2.7/
[[email protected] memcache-2.2.7]# ls
config9.m4  example.php                 memcache.php        memcache_standard_hash.c
config.m4   memcache.c                  memcache_queue.c    php_memcache.h
config.w32  memcache_consistent_hash.c  memcache_queue.h    README
CREDITS     memcache.dsp                memcache_session.c
[[email protected] memcache-2.2.7]# 
[[email protected] memcache-2.2.7]# /usr/local/php5/bin/phpize
[[email protected] memcache-2.2.7]# ls
acinclude.m4    config.sub    Makefile.global             memcache_standard_hash.c
aclocal.m4      configure     memcache.c                  missing
autom4te.cache  configure.in  memcache_consistent_hash.c  mkinstalldirs
build           config.w32    memcache.dsp                php_memcache.h
config9.m4      CREDITS       memcache.php                README
config.guess    example.php   memcache_queue.c            run-tests.php
config.h.in     install-sh    memcache_queue.h
config.m4       ltmain.sh     memcache_session.c
[[email protected] memcache-2.2.7]# ./configure --enable-memcache --with-php-config=/usr/local/php5/bin/php-config
[[email protected] memcache-2.2.7]# make && make install
[[email protected] memcache-2.2.7]# vim /usr/local/php5/php.ini
 736 ; extension_dir = "ext"
 737 extension_dir="/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/"      #增加
 738 extension = memcache.so    #增加

在web客户端去检测服务端是否可以连接

[[email protected] memcache-2.2.7]# cd /usr/local/httpd/htdocs/
[[email protected] htdocs]# ls
index.php
[[email protected] htdocs]# vim gsy.php
<?php
$memcache = new Memcache();
$memcache->connect(‘192.168.247.160‘,11211);
$memcache->set(‘key‘,‘Memcache test Successfull‘,0,60);
$result = $memcache->get(‘key‘);
unset($memcache);
echo $result;
?>
[[email protected] htdocs]# service httpd restart

接下来就可以在浏览器上查看了

原文地址:https://blog.51cto.com/14557905/2480791

时间: 2025-01-21 22:13:35

memcached高缓存搭建的相关文章

每日一记:搭建Memcached + php 缓存系统

服务器环境,Centos6.5 1.安装Memcached服务端 Yum -y install memcached 2.配置Memcached服务端用户以及自动启动服务等 将服务配置成自启动 chkconfig --level 2345 memcached on 3.配置配置文件 vi /etc/sysconfig/memcached PORT=”11211″ 端口  USER=”root” 使用的用户名  MAXCONN=”1024″ 同时最大连接数 CACHESIZE=”64″ 使用的内存大

搭建Memcached + php 缓存系统

服务器环境,Centos6.5 1.安装Memcached服务端 Yum -y install memcached 2.配置Memcached服务端用户以及自动启动服务等 将服务配置成自启动 chkconfig --level 2345 memcached on 3.配置配置文件 vi /etc/sysconfig/memcached PORT=”11211″ 端口  USER=”root” 使用的用户名  MAXCONN=”1024″ 同时最大连接数 CACHESIZE=”64″ 使用的内存大

Memcached 主主复制+ Keepalived 实现 Memcached 高可用架构集群

Memcached +keepalived 高可用集群 Memcached 主主复制这种架构,在程序连接时不知道应该连接哪个主服务器,所以需要在前端增加 VIP 地址,实现高可用架构.这里用 Keepalived 实现,因而 Keepalived 的作用就是用来检测 Memcached 服务器的状态是否正常. Keepalived 不断检测 Memcached 主服务器的11211端口,如果检测到 Memcached 服务发生宕机或者死机等情况,就会将 VIP 从主服务器移至从服务器,从而实现

OpenStack Mitaka HA高可用搭建

OpenStack Mitaka HA高可用搭建mini试看版version 1.0环境虚拟机列表:10.1.1.120 controller1 controller1.test.com10.1.1.121 controller2 controller2.test.com10.1.1.122 controller3 controller3.test.com10.1.1.133 compute1 compute1.test.com10.1.1.134 glance-backend backend.

simple-spring-memcached缓存搭建

项目中使用的缓存经常是知道使用,没有试过搭建起它.刚好这次自己的毕业可以用来搭建缓存.其他不多说了,直接看操作吧.首先在pom.xml中依赖simple-spring-memcached的架包. 1 <dependency> 2 <groupId>com.google.code.simple-spring-memcached</groupId> 3 <artifactId>xmemcached-provider</artifactId> 4 &l

Memcached高可用方案收集(集群及分布式)

Memcached的集群方案有很多,不止magent一个,但是单靠集群软件去实现高可用感觉还是会缺少一步,最推荐的方案应该是软件加编码去实现高可用,至少能保证站点的99.5%的可运行行,以下是集群的方案收集: randy_shandong: memcached演练(1) 搭建memcached服务 memcached演练(2) 访问memcached服务 单台tomcat的session信息的2种持久化方式 memcached演练(3) 使用apache+搭建双节点tomcat集群 memcac

Memcached高可用群集

Memcached高可用群集 主服务器安装Memcached.magent.libevent 安装 libevent memcached magent (主服务器) [[email protected] asd] cd /opt/libevent-2.1.8-stable [[email protected] libevent-2.1.8-stable] ./configure --prefix=/usr/ [[email protected] asd] cd /opt/memcached-1.

领会Memcached高可用群集(Memcached主主复制+Keepalived)

10月3号我写了高性能内存缓存对象缓存Memcached原理 (Memcached核心概念,部署),本片将Memcached高可用群集(Memcached主主复制+Keepalived)掌握一下. memcached高可用集群原理及介绍: memcached在实现分布式群集部署时,memcached服务之间是不能进行通讯的,分布式也是通过客户端的算法吧数据保存在不同的memcached中,所以当我们做完群集客户端往一个memcached节点写入数据后,另外两个节点是查询不到的. 那么如何结局额这

Memcached数据库缓存

Memcached 一.Memcached简介 Memcached是一个开源的,支持高性能,高并发的分布式内存缓存系统,由C语言编写,总共2000多行代码.从软件名称上看,前3个字符"Mem"就是内存的意思,而接下来的后面5个字符"cache"就是缓存的意思,最后一个字符d,是daemon的意思,代表是服务器端守护进程模式服务. Memcached服务分为服务器端和客户端两部分,其中,服务器端软件的名字形如Memcached-1.4.24.tar.gz,客户端软件的