zoukankan      html  css  js  c++  java
  • redis

    redis

     

    简介

      redis是一个基于C写的且开源的基于内存的数据库软件。读写能力强;

    第一阶段:单机

    1. 安装的两种方式:

    • 包管理器yum安装
    • 源码安装

    2. 配置:

    ################################## NETWORK #####################################
    
    # By default, if no "bind" configuration directive is specified, Redis listens
    # for connections from all the network interfaces available on the server.
    # It is possible to listen to just one or multiple selected interfaces using
    # the "bind" configuration directive, followed by one or more IP addresses.
    #
    # Examples:
    #
    # bind 192.168.1.100 10.0.0.1
    # bind 127.0.0.1 ::1
    #
    # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
    # internet, binding to all the interfaces is dangerous and will expose the
    # instance to everybody on the internet. So by default we uncomment the
    # following bind directive, that will force Redis to listen only into
    # the IPv4 lookback interface address (this means Redis will be able to
    # accept connections only from clients running into the same computer it
    # is running).
    #
    # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
    # JUST COMMENT THE FOLLOWING LINE.
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    bind 0.0.0.0
    
    # Protected mode is a layer of security protection, in order to avoid that
    # Redis instances left open on the internet are accessed and exploited.
    #
    # When protected mode is on and if:
    #
    # 1) The server is not binding explicitly to a set of addresses using the
    #    "bind" directive.
    # 2) No password is configured.
    #
    # The server only accepts connections from clients connecting from the
    # IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
    # sockets.
    #
    # By default protected mode is enabled. You should disable it only if
    # you are sure you want clients from other hosts to connect to Redis
    # even if no authentication is configured, nor a specific set of interfaces
    # are explicitly listed using the "bind" directive.
    protected-mode yes
    
    # Accept connections on the specified port, default is 6379 (IANA #815344).
    # If port 0 is specified Redis will not listen on a TCP socket.
    port 6379
    
    # TCP listen() backlog.
    #
    # In high requests-per-second environments you need an high backlog in order
    # to avoid slow clients connections issues. Note that the Linux kernel
    # will silently truncate it to the value of /proc/sys/net/core/somaxconn so
    # make sure to raise both the value of somaxconn and tcp_max_syn_backlog
    # in order to get the desired effect.
    tcp-backlog 511
    
    # Unix socket.
    #
    # Specify the path for the Unix socket that will be used to listen for
    # incoming connections. There is no default, so Redis will not listen
    # on a unix socket when not specified.
    #
    # unixsocket /tmp/redis.sock
    # unixsocketperm 700
    
    # Close the connection after a client is idle for N seconds (0 to disable)
    timeout 0
    
    # TCP keepalive.
    #
    # If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
    # of communication. This is useful for two reasons:
    #
    # 1) Detect dead peers.
    # 2) Take the connection alive from the point of view of network
    #    equipment in the middle.
    #
    # On Linux, the specified value (in seconds) is the period used to send ACKs.
    # Note that to close the connection the double of the time is needed.
    # On other kernels the period depends on the kernel configuration.
    #
    # A reasonable value for this option is 300 seconds, which is the new
    # Redis default starting with Redis 3.2.1.
    tcp-keepalive 300
    ...
    ......
    .........
    ............

    3. 使用:

    1
    2
    3
    4
    5
    import redis
      
    r = redis.Redis(host='10.211.55.4', port=6379)
    r.set('foo', 'Bar')
    print r.get('foo')

    第二阶段:高可用 

    1. 安装方式

    • slaveof命令+keepalived
    • sentinel

    2. 配置

    方式一:

    port 6379
    其他配置...
    /etc/redis-6379.conf
    port 6380
    
    其他配置...
    /etc/redis-6380.conf
    在6380上执行命令:
        slaveof 10.211.55.20 6379
    或在6380上添加配置:
        slaveof 10.211.55.20 6379
    设置主从

    方式二:

    # Example sentinel.conf
    
    # *** IMPORTANT ***
    #
    # By default Sentinel will not be reachable from interfaces different than
    # localhost, either use the 'bind' directive to bind to a list of network
    # interfaces, or disable protected mode with "protected-mode no" by
    # adding it to this configuration file.
    #
    # Before doing that MAKE SURE the instance is protected from the outside
    # world via firewalling or other means.
    #
    # For example you may use one of the following:
    #
    # bind 127.0.0.1 192.168.1.1
    #
    # protected-mode no
    
    # port <sentinel-port>
    # The port that this sentinel instance will run on
    port 26379
    
    # sentinel announce-ip <ip>
    # sentinel announce-port <port>
    #
    # The above two configuration directives are useful in environments where,
    # because of NAT, Sentinel is reachable from outside via a non-local address.
    #
    # When announce-ip is provided, the Sentinel will claim the specified IP address
    # in HELLO messages used to gossip its presence, instead of auto-detecting the
    # local address as it usually does.
    #
    # Similarly when announce-port is provided and is valid and non-zero, Sentinel
    # will announce the specified TCP port.
    #
    # The two options don't need to be used together, if only announce-ip is
    # provided, the Sentinel will announce the specified IP and the server port
    # as specified by the "port" option. If only announce-port is provided, the
    # Sentinel will announce the auto-detected local IP and the specified port.
    #
    # Example:
    #
    # sentinel announce-ip 1.2.3.4
    
    # dir <working-directory>
    # Every long running process should have a well-defined working directory.
    # For Redis Sentinel to chdir to /tmp at startup is the simplest thing
    # for the process to don't interfere with administrative tasks such as
    # unmounting filesystems.
    dir "/tmp"
    
    # sentinel monitor <master-name> <ip> <redis-port> <quorum>
    #
    # Tells Sentinel to monitor this master, and to consider it in O_DOWN
    # (Objectively Down) state only if at least <quorum> sentinels agree.
    #
    # Note that whatever is the ODOWN quorum, a Sentinel will require to
    # be elected by the majority of the known Sentinels in order to
    # start a failover, so no failover can be performed in minority.
    #
    # Slaves are auto-discovered, so you don't need to specify slaves in
    # any way. Sentinel itself will rewrite this configuration file adding
    # the slaves using additional configuration options.
    # Also note that the configuration file is rewritten when a
    # slave is promoted to master.
    #
    # Note: master name should not include special characters or spaces.
    # The valid charset is A-z 0-9 and the three characters ".-_".
    sentinel myid f4043341d4f4e22727b75dbe256d374bc759d144
    
    # sentinel auth-pass <master-name> <password>
    #
    # Set the password to use to authenticate with the master and slaves.
    # Useful if there is a password set in the Redis instances to monitor.
    #
    # Note that the master password is also used for slaves, so it is not
    # possible to set a different password in masters and slaves instances
    # if you want to be able to monitor these instances with Sentinel.
    #
    # However you can have Redis instances without the authentication enabled
    # mixed with Redis instances requiring the authentication (as long as the
    # password set is the same for all the instances requiring the password) as
    # the AUTH command will have no effect in Redis instances with authentication
    # switched off.
    #
    # Example:
    #
    # sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
    
    # sentinel down-after-milliseconds <master-name> <milliseconds>
    #
    # Number of milliseconds the master (or any attached slave or sentinel) should
    # be unreachable (as in, not acceptable reply to PING, continuously, for the
    # specified period) in order to consider it in S_DOWN state (Subjectively
    # Down).
    #
    # Default is 30 seconds.
    sentinel monitor mymaster 10.211.55.20 6380 1
    
    # sentinel parallel-syncs <master-name> <numslaves>
    #
    # How many slaves we can reconfigure to point to the new slave simultaneously
    # during the failover. Use a low number if you use the slaves to serve query
    # to avoid that all the slaves will be unreachable at about the same
    # time while performing the synchronization with the master.
    sentinel config-epoch mymaster 3
    
    # sentinel failover-timeout <master-name> <milliseconds>
    #
    # Specifies the failover timeout in milliseconds. It is used in many ways:
    #
    # - The time needed to re-start a failover after a previous failover was
    #   already tried against the same master by a given Sentinel, is two
    #   times the failover timeout.
    #
    # - The time needed for a slave replicating to a wrong master according
    #   to a Sentinel current configuration, to be forced to replicate
    #   with the right master, is exactly the failover timeout (counting since
    #   the moment a Sentinel detected the misconfiguration).
    #
    # - The time needed to cancel a failover that is already in progress but
    #   did not produced any configuration change (SLAVEOF NO ONE yet not
    #   acknowledged by the promoted slave).
    #
    # - The maximum time a failover in progress waits for all the slaves to be
    #   reconfigured as slaves of the new master. However even after this time
    #   the slaves will be reconfigured by the Sentinels anyway, but not with
    #   the exact parallel-syncs progression as specified.
    #
    # Default is 3 minutes.
    sentinel leader-epoch mymaster 3
    
    #
    # Specify the log file name. Also the empty string can be used to force
    # Redis to log on the standard output. Note that if you use standard
    # output for logging but daemonize, logs will be sent to /dev/null
    logfile "/var/log/redis/sentinel.log"
    
    # SCRIPTS EXECUTION
    #
    # sentinel notification-script and sentinel reconfig-script are used in order
    # to configure scripts that are called to notify the system administrator
    # or to reconfigure clients after a failover. The scripts are executed
    # with the following rules for error handling:
    #
    # If script exits with "1" the execution is retried later (up to a maximum
    # number of times currently set to 10).
    #
    # If script exits with "2" (or an higher value) the script execution is
    # not retried.
    #
    # If script terminates because it receives a signal the behavior is the same
    # as exit code 1.
    #
    # A script has a maximum running time of 60 seconds. After this limit is
    # reached the script is terminated with a SIGKILL and the execution retried.
    
    # NOTIFICATION SCRIPT
    #
    # sentinel notification-script <master-name> <script-path>
    #
    # Call the specified notification script for any sentinel event that is
    # generated in the WARNING level (for instance -sdown, -odown, and so forth).
    # This script should notify the system administrator via email, SMS, or any
    # other messaging system, that there is something wrong with the monitored
    # Redis systems.
    #
    # The script is called with just two arguments: the first is the event type
    # and the second the event description.
    #
    # The script must exist and be executable in order for sentinel to start if
    # this option is provided.
    #
    # Example:
    #
    # sentinel notification-script mymaster /var/redis/notify.sh
    
    # CLIENTS RECONFIGURATION SCRIPT
    #
    # sentinel client-reconfig-script <master-name> <script-path>
    #
    # When the master changed because of a failover a script can be called in
    # order to perform application-specific tasks to notify the clients that the
    # configuration has changed and the master is at a different address.
    #
    # The following arguments are passed to the script:
    #
    # <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port>
    #
    # <state> is currently always "failover"
    # <role> is either "leader" or "observer"
    #
    # The arguments from-ip, from-port, to-ip, to-port are used to communicate
    # the old address of the master and the new address of the elected slave
    # (now a master).
    #
    # This script should be resistant to multiple invocations.
    #
    # Example:
    #
    # sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
    
    # Generated by CONFIG REWRITE
    /etc/redis-sentinel.con
    // Sentinel节点的端口
    port 26379  
    dir /var/redis/data/
    logfile "26379.log"
    
    // 当前Sentinel节点监控 192.168.119.10:6379 这个主节点
    // 2代表判断主节点失败至少需要2个Sentinel节点节点同意
    // mymaster是主节点的别名
    sentinel monitor mymaster 192.168.119.10 6379 2
    
    //每个Sentinel节点都要定期PING命令来判断Redis数据节点和其余Sentinel节点是否可达,如果超过30000毫秒且没有回复,则判定不可达
    sentinel down-after-milliseconds mymaster 30000
    
    //当Sentinel节点集合对主节点故障判定达成一致时,Sentinel领导者节点会做故障转移操作,选出新的主节点,原来的从节点会向新的主节点发起复制操作,限制每次向新的主节点发起复制操作的从节点个数为1
    sentinel parallel-syncs mymaster 1
    
    //故障转移超时时间为180000毫秒
    sentinel failover-timeout mymaster 180000

    3. 操作

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
     
    from redis.sentinel import Sentinel
     
    # 连接哨兵服务器(主机名也可以用域名)
    sentinel = Sentinel([('10.211.55.20', 26379),
                         ('10.211.55.20', 26380),
                         ],
                        socket_timeout=0.5)
     
    # # 获取主服务器地址
    # master = sentinel.discover_master('mymaster')
    # print(master)
    #
    # # # 获取从服务器地址
    # slave = sentinel.discover_slaves('mymaster')
    # print(slave)
    #
    #
    # # # 获取主服务器进行写入
    # master = sentinel.master_for('mymaster')
    # master.set('foo', 'bar')
     
     
    # # # # 获取从服务器进行读取(默认是round-roubin)
    # slave = sentinel.slave_for('mymaster', password='redis_auth_pass')
    # r_ret = slave.get('foo')
    # print(r_ret)

    第三阶段:分布式集群

    ### 分布式集群:redis-cluster 【16384】 ###
        
        a. 创建3主、3从
        b. 安装
            目标:
                0 ~ 5500  5501 ~ 11000 11001 ~ 16384
                7001        7002           7003
                7004        7005           7006
                
                所有redis节点,彼此互联。
            连接:
                客户端链接时,无需proxy也无需链接所有节点,只需要连接cluster就可访问所有数据。
                
            依赖:
                yum install ruby rubygems -y
                gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
                gem install redis
                
                由于redis创建集群的脚本用ruby编写的。
                
                报错:
                
                升级ruby:
                    ERROR:  Error installing redis:
                        redis requires Ruby version >= 2.2.2.
                升级ruby:
                    gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
                    curl -sSL https://get.rvm.io | bash -s stable
                    source /usr/local/rvm/scripts/rvm
                    rvm list known 
                    rvm install 指定版本
                    rvm use 指定版本
                    rvm remove 指定版本
                    ruby --version
                    
                重新安装:
                    gem install redis
        
        
            配置:
                1. 创建6个配置文件
                    ...
                2. 在配置文件中添加
                    daemonize yes
                    cluster-enabled yes
                    cluster-config-file nodes-各自配置.conf
                    cluster-node-timeout 5000
                    appendonly yes
                    
                3. 创建集群
                    ./redis-trib.rb create --replicas 1 192.168.10.25:7001 192.168.10.25:7002 192.168.10.25:7003 192.168.10.25:7004 192.168.10.25:7005 192.168.10.25:7006
                    
                4. 重新分配
                    ./redis-trib.rb reshard 192.168.10.25:7001
                    
                    拿走的大小
                        目标:给谁?
                        源头:从哪里那
                    done完成这事

    操作:

    1
    2
    3
    4
    5
    6
    7
    from rediscluster import StrictRedisCluster
    startup_nodes = [{"host": "127.0.0.1", "port": "7000"}]
    rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)
     
    rc.set("foo", "bar")
     
    print(rc.get("foo"))

      

  • 相关阅读:
    noip宝藏
    [HNOI2008]玩具装箱TOY
    hdu 4372 Count the Buildings 轮换斯特林数
    bzoj 3000 Big Number 估算n!在k进制下的位数 斯特林公式
    自然数幂求和方法1:扰动法(求两次)
    斯特林数 学习笔记
    hdu 3625 Examining the Rooms 轮换斯特林数
    cf 472G Design Tutorial: Increase the Constraints 分块+压位/FFT
    chef and churu 分块 好题
    分块 学习笔记
  • 原文地址:https://www.cnblogs.com/xiao-xue-di/p/10268957.html
Copyright © 2011-2022 走看看