• redis


    一、redis简介

    1)基于key-value键值对的持久化数据库存储系统。
    2)支持的数据存储类型更丰富,包括string(字符串),list(链表),set(集合),zset(有序集合)等。
    3)这些数据类型都支持push/pop,add/remove及取交集、并集和差集及更丰富的操作,这些操作都是原子性的
    4)支持各种不同的方式的排序
    5)数据跟memcached一样,都是存储在内存中,,,但是redis持久化缓存服务还会周期性的把更新的数据写入到磁盘以及把修改的操作记录追加到文件里记录下来。
    6)redis还支持master-slave主从同步。

    二、redis应用场景

    传统的mysql+memcached的网站架构遇到的问题:
    1)需要不断的对mysql进行拆库拆表,memecached也需要不断跟着扩容,扩容和维护工作占据大量开发运维时间。
    2)memcached与mysql数据库一致性问题是个老大难
    3)memcached数据命中率低或down机,会导致大量访问直接穿透到数据库,导致mysql无法支撑访问。
    4)跨机房cache同步一致性问题。

    redis的最佳应用场景:
    1、redis最佳使用场景是全部数据在内存中。
    2、redis更多场景是作为memcached的替代品来使用。
    3、支持持久化
    4、当需要除key/value之外的更多数据类型支持时,使用redis更合适。
    5、需要负载均衡的场景(redis主从同步)

    redis作者谈redis应用场景http://blog.nosqlfan.com/html/2235.html

    三、redis安装部署

    [root@aliyun tools]# wget http://download.redis.io/releases/redis-4.0.2.tar.gz
    [root@aliyun tools]# tar -zxf redis-4.0.2.tar.gz 
    [root@aliyun tools]# cd redis-4.0.2
    [root@aliyun redis-4.0.2]# make MALLOC=jemalloc
    [root@aliyun redis-4.0.2]# make PREFIX=/application/redis-4.0.2 install
    
    
    redis-server:redis服务器的daemon启动程序
    redis-cli:redis命令行操作工具
    redis-benchmark:redis性能测试工具,测试redis在你的系统及你的配置下的读写性能。
    redis-check-aof:更新日志检查
    redis-check-dump:用于本地数据检查
    
    [root@aliyun bin]# export PATH=/application/redis/bin/:$PATH
    

    配置文件

    [root@aliyun redis-4.0.2]# mkdir -p /application/redis/conf
    [root@aliyun redis-4.0.2]# cp redis.conf /application/redis/conf/
    [root@aliyun redis-4.0.2]# redis-server /application/redis/conf/redis.conf &
    

    问题:
    刚开始执行的时候如果遇到内存过低的情况下会报警,所以执行如下操作。

    WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
    [root@aliyun redis-4.0.2]# sysctl vm.overcommit_memory=1
    vm.overcommit_memory = 1
    

    别的问题:

    WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    [root@aliyun redis-4.0.2]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
    
    WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    [root@aliyun redis-4.0.2]# echo 511 >/proc/sys/net/core/somaxconn
    

    执行:

    [root@aliyun redis-4.0.2]# redis-server /application/redis/conf/redis.conf &
    

    关闭redis:

    [root@aliyun redis-4.0.2]# redis-cli shutdown
    

    通过客户端测试redis服务

    [root@aliyun redis-4.0.2]# redis-cli 
    127.0.0.1:6379> set num002 nima
    OK
    127.0.0.1:6379> get num002
    "nima"
    
    [root@aliyun redis-4.0.2]# redis-cli -h 127.0.0.1 -p 6379 get num002
    "nima"
    
    [root@aliyun redis-4.0.2]# redis-cli del num002
    (integer) 1
    [root@aliyun redis-4.0.2]# redis-cli get num002
    (nil)
    

    关于键的模式要标准,方便理解

    [root@aliyun redis-4.0.2]# redis-cli set user:01:passwd 001
    OK
    [root@aliyun redis-4.0.2]# redis-cli get user:01:passwd    
    "001"
    

    字符串类型:

    这是最简单的redis类型,如果只用这种类型,redis就是一个可以持久化的memcached服务器
    (注:memcache的数据仅保存在内存中。服务器重启后,数据将丢失)
    
    计数的功能
    [root@aliyun redis-4.0.2]# redis-cli set counter 100
    [root@aliyun redis-4.0.2]# redis-cli incr counter    
    (integer) 101
    [root@aliyun redis-4.0.2]# redis-cli incr counter
    (integer) 102
    [root@aliyun redis-4.0.2]# redis-cli incr counter
    (integer) 103
    [root@aliyun redis-4.0.2]# redis-cli incr counter
    (integer) 104
    
    列表类型:
    
    此处自己研究,,DBA的活,,不管,了解
    

    redis,,php扩展

    [root@aliyun tools]# wget https://github.com/phpredis/phpredis/archive/master.zip
    [root@aliyun tools]# unzip master.zip 
    [root@aliyun tools]# cd phpredis-master/
    [root@aliyun phpredis-master]# /application/php/bin/phpize 
    [root@aliyun phpredis-master]# ./configure --with-php-config=/application/php/bin/php-config 
    [root@aliyun phpredis-master]# make && make install
    Installing shared extensions:     /application/php5.3.27/lib/php/extensions/no-debug-non-zts-20090626/
    
    [root@aliyun phpredis-master]# echo "extension = redis.so" >>/application/php/lib/php.ini
    [root@aliyun phpredis-master]# pkill php-fpm
    [root@aliyun phpredis-master]# /application/php/sbin/php-fpm 
    

    主从同步:
    主服务器不需要任何配置,,,,从服务器在配置文件里修改一处
    [root@aliyun phpredis-master]# vim /application/redis/conf/redis.conf
    slaveof remoteip remoteport

    =========================================================================
    因为redis主从同步了,所以redis可以做负载均衡

  • 相关阅读:
    Oracle 11g RAC到单实例ASM的物理Standby搭建
    Oracle"log file sync"等待分析
    RAC ASM到单机ASM的dataguard创建步骤
    Kafka学习之路 (五)Kafka在zookeeper中的存储
    A highly available WEB cluster based on HAProxy + KeepAlived
    postgresql 10数据库备份
    Oracle DG+sqlserver alwayson主节点切换时OGG源端目标端容灾实践
    Kafka学习之路 (三)Kafka的高可用
    趋势图
    火狐浏览器
  • 原文地址:https://www.cnblogs.com/bill2014/p/7611730.html
Copyright © 2020-2023  润新知