欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

redis集群持久化数据重启恢复 博客分类: redis redis 

程序员文章站 2024-03-17 13:00:16
...
网上已有很多关于redis的集群部署方案的文章,这里就直接忽略了。
目前考虑redis集群宕机后,如果有数据是持久化了的,那么重启的时候如何恢复呢?
网上给的答案都是使用dump.rdb或者aof方案来实现
    
  • 修改redis_*.conf配置文件,屏蔽aof方案的持久化方式
  •        
               # AOF and RDB persistence can be enabled at the same time without problems.
               # If the AOF is enabled on startup Redis will load the AOF, that is the file
               # with the better durability guarantees.
               appendonly no
            

        
  • 修改redis_*.conf配置文件,设置dump方式备份文件名称,每个节点文件不能相同,否则会覆盖
  •         
                # The filename where to dump the DB
                dbfilename "dump_20000.rdb"
             

       使用以上文件重启redis cluster时会明确提示启动失败:
          
    [ERR] Node 127.0.0.1:20000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0. 

       根据以上内容再次排查发现,给的方案普遍是删除相关文件,但是这样就无法实现题目中的目的。
       [最终] 通过对redis-cli --cluster 的不同命令进行尝试,发现需要对每个节点进行check就可以恢复集群模式,不需要用create来重新构建集群。因为create和add-node都需要保证节点数据为空【以下方式正常情况是无需操作的,直接启动对应端口的服务即可,因为node_2000.conf已生成】
        
           redis-cli --cluster check 127.0.0.1:20000 
           redis-cli --cluster check 127.0.0.1:20001
           redis-cli --cluster check 127.0.0.1:20002
           redis-cli --cluster check 127.0.0.1:20003
           redis-cli --cluster check 127.0.0.1:20004
           redis-cli --cluster check 127.0.0.1:20005
         


       以下是centos下的redis无数据启动和持久化重启脚本

        
  • redis_clear_restart.sh
  •     
    for p in `ps -ef|grep /usr/local/bin/redis-server | awk '{print $2"_"$3}'`
    do
      a=`echo $p |awk -F _ '{print $1}'`
      b=`echo $p |awk -F _ '{print $2}'`
      if [ $b == "1" ]; then
         echo "kill -9 $a `kill -9 $a` "
      fi
    done
    echo "rm -rf appendonly_*.aof dump_*.rdb nodes-2000*.conf"
    rm -rf appendonly_*.aof dump_*.rdb  nodes-2000*.conf
    
    /usr/local/bin/redis-server /home/sysusr/redis/redis-cluster/redis-20000.conf
    /usr/local/bin/redis-server /home/sysusr/redis/redis-cluster/redis-20001.conf
    /usr/local/bin/redis-server /home/sysusr/redis/redis-cluster/redis-20002.conf
    /usr/local/bin/redis-server /home/sysusr/redis/redis-cluster/redis-20003.conf
    /usr/local/bin/redis-server /home/sysusr/redis/redis-cluster/redis-20004.conf
    /usr/local/bin/redis-server /home/sysusr/redis/redis-cluster/redis-20005.conf
    /usr/local/bin/redis-cli -a 123456 --cluster create --cluster-replicas 1 127.0.0.1:20000 127.0.0.1:20001 127.0.0.1:20002 127.0.0.1:20003 127.0.0.1:20004 127.0.0.1:20005 <<EO
    F
    yes
    EOF
         
        

        
  • redis_dump_restart.sh
  •     
    for p in `ps -ef|grep /usr/local/bin/redis-server | awk '{print $2"_"$3}'`
    do
      a=`echo $p |awk -F _ '{print $1}'`
      b=`echo $p |awk -F _ '{print $2}'`
      if [ $b == "1" ]; then
         echo "kill -9 $a `kill -9 $a` "
      fi
    done
    echo "rm -rf appendonly_*.aof dump_*.rdb nodes-2000*.conf"
    rm -rf appendonly_*.aof dump_*.rdb  nodes-2000*.conf
    
    /usr/local/bin/redis-server /home/sysusr/redis/redis-cluster/redis-20000.conf
    /usr/local/bin/redis-server /home/sysusr/redis/redis-cluster/redis-20001.conf
    /usr/local/bin/redis-server /home/sysusr/redis/redis-cluster/redis-20002.conf
    /usr/local/bin/redis-server /home/sysusr/redis/redis-cluster/redis-20003.conf
    /usr/local/bin/redis-server /home/sysusr/redis/redis-cluster/redis-20004.conf
    /usr/local/bin/redis-server /home/sysusr/redis/redis-cluster/redis-20005.conf
    
    /usr/local/bin/redis-cli -a 123456 --cluster check 127.0.0.1:20000
    /usr/local/bin/redis-cli -a 123456 --cluster check 127.0.0.1:20001
    /usr/local/bin/redis-cli -a 123456 --cluster check 127.0.0.1:20002
    /usr/local/bin/redis-cli -a 123456 --cluster check 127.0.0.1:20003
    /usr/local/bin/redis-cli -a 123456 --cluster check 127.0.0.1:20004
    /usr/local/bin/redis-cli -a 123456 --cluster check 127.0.0.1:20005
         
        
     
         不确定该方法在生产环境上是否存在缺陷或者有效?【本内容为原创,转载请注明出处】
    相关标签: redis