Redis Redis命令
Redis是内存型kv数据库
Redis可以定时磁盘持久化,2种方式:快照,appendonlyfile
快照方式是fork一个子进程,把内存数据压缩,同步到磁盘
aof方式(。。。。),把增量数据以命令行方式,写到磁盘
大数据情况下,aof比快照方式对系统影响小,但都会因为fork而引起对外响应的超时(如果请求超时比较短,例如1秒)
一个比较可行的方式是:master-slave方式部署,并在slave上做磁盘持久化,这样可以最大化避免对master的影响。
yum install ruby , gem install redis –version 3.2.8 指定版本,否则系统的ruby可能版本不够!
常用命令 1 2 3 4 5 6 7 8 config set appendonly yes bgsave config set requirepass MNaVuRizat config set masterauth MNaVuRizat config rewrite slaveof 111.111.111.111 6379 slaveof no one scan 0
修改系统参数
echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
sysctl -p
echo "BGSAVE" | redis-cli -p 6379 -a abcdefg
安装 快速安装
1 2 3 4 5 6 7 8 9 10 wget http://download.redis.io/releases/redis-3.2.6.tar.gz tar xzf redis-3.2.6.tar.gz cd redis-3.2.6 make sudo make install or sudo cp src/redis-server /usr/local/bin/ sudo cp src/redis-cli /usr/local/bin/
yum方式安装
2.4 1 2 3 4 rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm yum --enablerepo=remi,remi-test install redis
配置 一个典型的配置:
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 daemonize yes port 6399 dir "/home/zhaigy/local/rts-cluster/redis/inst_test" pidfile "/home/zhaigy/local/rts-cluster/redis/inst_test/redis_test.pid" logfile "/home/zhaigy/local/rts-cluster/redis/inst_test/redis_test.log" dbfilename "dump_test.rdb" tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice databases 16 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 0 0 0 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes repl-timeout 300 repl-backlog-size 2gb maxmemory 78125000kb
info config set client-output-buffer-limit “slave 0 0 0” config rewrite
10.32.54.110
同步数据 SLAVEOF 10.32.54.154 6399 SLAVEOF NO ONE 关闭
监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常
提醒(Notification)
自动故障迁移(Automatic failover),失效主,提升从;客户端连接时,会向客户端返回新主服务器的地址
流言协议(gossip protocols) 接收关于主服务器是否下线
投票协议(agreement protocols)来决定自动故障迁移
Redis 2.8.0 或以上的版本
Master挂了。 第一个发现的Sentinel会负责进行自动故障迁移,它会立即在Slaves中选出一个担任Master,所有Slaves从属于新Master,所有客户端对Master的操作转而到这台新Master上
命令 1 2 redis-sentinel /path/to/sentinel.conf redis-server /path/to/sentinel.conf --sentinel
1 2 3 PING SENTINEL masters SENTINEL failover 强制开始一次自动故障迁移
Jedis操作 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 private static final JedisSentinelPool pool;static { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig (); jedisPoolConfig.setMaxTotal(10 ); jedisPoolConfig.setMaxIdle(5 ); jedisPoolConfig.setMinIdle(5 ); Set<String> sentinels = new HashSet <>(Arrays.asList( "111.111.111.111:26379" , "111.111.111.112:26379" , "111.111.111.113:26379" )); GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig (); poolConfig.setMaxTotal(10 ); poolConfig.setMaxIdle(5 ); poolConfig.setMinIdle(5 ); pool = new JedisSentinelPool ("yewu01" , sentinels, jedisPoolConfig); } public static void main (String[] args) throws Exception { String key1 = "key1" ; try (Jedis jedis = pool.getResource()) { jedis.set(key1, "222" ); System.out.println(jedis.get(key1)); } catch (Exception e) { e.printStackTrace(); } }
集群
性能:采用了P2P而非Proxy,异步复制,客户端重定向,牺牲了一致性
可用性:增加了自动failover
水平扩展
配置 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 bind 111.111.111.111 port 7000 ################ dir "/home/abc/local/redis-cluster/7000" dbfilename "dump.rdb" logfile "log.log" pidfile "pid.pid" cluster-config-file "nodes.conf" ################ protected-mode yes tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no loglevel notice databases 16 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 ### cluster-enabled yes cluster-node-timeout 5000 slowlog-log-slower-than 1000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes
创建启动 master节点最少3个,slave数可以为0
1 redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
–replicas 1 表示每个master 1个slaver
上面共6个,所以3个master,3个slaver;前3个是master,后3个是slaver
redis-trib 可以使用yum安装,yum search redis-trib1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 D="/home/abc/local/redis-cluster" cd $D hosts="7001 7002 7003 7004 7005" for h in $hosts; do echo $h cd ./$h if [ -f "./pid.pid" ]; then pid=`cat ./pid.pid` kill $pid sleep 0.5 rm -rf ./pid.pid fi redis-server ./redis.conf cd $D sleep 1 done
加密码 1 2 3 4 5 6 7 8 9 master: redis-cli -c -h 10.32.54.204 -p 7002 config set requirepass bS4JjzZpj5E75QHD redis-cli -c -h 10.32.54.204 -p 7002 -a bS4JjzZpj5E75QHD config rewrite slave: redis-cli -c -h 10.32.54.182 -p 7006 config set requirepass bS4JjzZpj5E75QHD redis-cli -c -h 10.32.54.182 -p 7006 -a bS4JjzZpj5E75QHD config set masterauth bS4JjzZpj5E75QHD redis-cli -c -h 10.32.54.182 -p 7006 -a bS4JjzZpj5E75QHD config
迁移节点 : 把一个节点迁往另一个机器 从 cnodeA 迁移到 cnodeB
常用命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 cluster info cluster meet 127.0.0.1 6380 cluster slots cluster nodes cluster failover 在从节点上执行“cluster failover”命令将触发手动failover,结果是原来的主节点变成了从节点,从节点变成了主节点。 cluster replicate 0b00721a509444db793d28448d8f02168b94bd38 # 要求本节点成为指定节点的slave,要求其先成为slave redis-trib.rb reshard 127.0.0.1:6739 # 重新分配slot,需要交互输入 redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000 # 添加新节点,要先启动服务
添加节点 1 2 3 4 //增加M节点 ~/redis-trib.rb add-node new_ip:new_port(M新节点) existing_host:existing_port(已存在节点) //增加S节点 ~/redis-trib.rb add-node --slave --master-id masterid(master节点id) new_ip:new_port(S新节点) existing_host:existing_port(已存在节点)
删除节点 1 2 3 4 //删除节点 ~/redis-trib.rb del-node existing_host:existing_port(已存在节点) node_id(删除节点id) 删除节点后,redis实例会shutdown
Redis集群常用操作方式
问题 复制中断和无限同步 https://zhuoroger.github.io/2016/07/31/redis-replication-broken-and-loopsync/
Redis复制中断后,Slave会立即发起psync,psync尝试部署同步不成功,就会全量同步RDB
中断原因主要有:
Master端的Slave客户端输出缓冲,达到限制大小,被Redis Server强制Kill,导致复制中断
Master和Slave之间复制超时repl-timeout断开,导致复制中断
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 jaytp@qq.com