redis实现主从复制-单机测试

一、redis实现主从复制-单机测试
1、安装redis
tar -zxvf redis-2.8.4.tar.gz
cd redis-2.8.4
make && make install
2、配置主从关系
需要在slave服务器的redis.conf中配置
slaveof 192.168.1.1 6379 #指定master的ip和端口
具体配置见下:
cp redis.conf redis-master-6379.conf
vi2 redis-master-6379.conf
logfile "/appcom/Redis/redis-2.8.4/redis-master-6379.log"

cp redis.conf redis-master-6389.conf
vi2 redis-master-6379.conf
port 6389
slaveof localhost 6379
logfile "/appcom/Redis/redis-2.8.4/redis-slave-6389.log"

3、启动master服务器和slave服务器
./src/redis-server redis-master-6379.conf &
[19810] 28 Jan 14:18:55.825 * The server is now ready to accept connections on port 6379
[19810] 28 Jan 14:23:19.918 * Slave asks for synchronization
[19810] 28 Jan 14:23:19.919 * Full resync requested by slave.
[19810] 28 Jan 14:23:19.919 * Starting BGSAVE for SYNC
[19810] 28 Jan 14:23:19.928 * Background saving started by pid 22336
[22336] 28 Jan 14:23:19.947 * DB saved on disk
[22336] 28 Jan 14:23:19.948 * RDB: 6 MB of memory used by copy-on-write
[19810] 28 Jan 14:23:19.985 * Background saving terminated with success
[19810] 28 Jan 14:23:19.986 * Synchronization with slave succeeded
[19810] 28 Jan 14:23:21.038 # Connection with slave ::1:6389 lost.
[19810] 28 Jan 14:23:25.159 * Slave asks for synchronization
[19810] 28 Jan 14:23:25.159 * Full resync requested by slave.
[19810] 28 Jan 14:23:25.159 * Starting BGSAVE for SYNC
[19810] 28 Jan 14:23:25.163 * Background saving started by pid 22399
[22399] 28 Jan 14:23:25.177 * DB saved on disk
[22399] 28 Jan 14:23:25.178 * RDB: 6 MB of memory used by copy-on-write
[19810] 28 Jan 14:23:25.210 * Background saving terminated with success
[19810] 28 Jan 14:23:25.210 * Synchronization with slave succeeded

./src/redis-server redis-slave-6389.conf &
[22327] 28 Jan 14:23:18.915 * The server is now ready to accept connections on port 6389
[22327] 28 Jan 14:23:19.913 * Connecting to MASTER localhost:6379
[22327] 28 Jan 14:23:19.915 * MASTER <-> SLAVE sync started
[22327] 28 Jan 14:23:19.915 * Non blocking connect for SYNC fired the event.
[22327] 28 Jan 14:23:19.916 * Master replied to PING, replication can continue...
[22327] 28 Jan 14:23:19.917 * Partial resynchronization not possible (no cached master)

在master shutdown之后slave中可以使用数据,但在后台日志中出现下面信息,并不会将slave转化为master
[7084] 28 Jan 14:04:59.940 * Connecting to MASTER localhost:6379
[7084] 28 Jan 14:04:59.941 * MASTER <-> SLAVE sync started
[7084] 28 Jan 14:04:59.941 # Error condition on socket for SYNC: Connection refused

二、利用Redis-sentinel实现redis集群的故障恢复-单机测试
1、redis安装
master localhost 6379
slave1 localhost 6389
slave2 localhost 6399
master-sentinel: localhost 26379
slave1-sentinel: localhost 26389
slave2-sentinel: localhost 26399
2、redis配置
master配置
cp redis.conf redis-master-6379.conf
vi2 redis-master-6379.conf
port 6379
requirepass rd123
masterauth rd123
#rename-command
appendonly yes //开启aof
save “”
slave-read-only yes
logfile "/appcom/Redis/redis-2.8.4/redis-master-6379.log"

vi2 sentinel-6379.conf
port 26379
sentinel monitor mymaster 127.0.0.1 6379 2 //sentinel需要监控的master信息:<mastername> <masterIP> <masterPort> <quorum>. <quorum>应该小于集群中slave的个数,只有当至少<quorum>个sentinel实例提交"master失效" 才会认为master为ODWON("客观"失效) .
sentinel auth-pass mymaster rd123
sentinel down-after-milliseconds mymaster 30000 //master被当前sentinel实例认定为“失效”(SDOWN)的间隔时间
sentinel parallel-syncs mymaster 1 //当新master产生时,同时进行“slaveof”到新master并进行同步复制的slave个数。
sentinel failover-timeout mymaster 180000 //failover过期时间,当failover开始后,在此时间内仍然没有触发任何failover操作,当前sentinel将会认为此次failoer失败

slave1配置
cp redis-master-6379.conf redis-slave-6389.conf
vi2 redis-slave-6389.conf
prot 6389
slaveof localhost 6379
logfile "/appcom/Redis/redis-2.8.4/redis-master-6389.log"

cp sentinel-6379.conf sentinel-6389.conf
vi2 sentinel-6389.conf
prot 26389

slave2配置
cp redis-master-6379.conf redis-slave-6399.conf
vi2 redis-slave-6399.conf
prot 6399
slaveof localhost 6379
logfile "/appcom/Redis/redis-2.8.4/redis-master-6399.log"

cp sentinel-6379.conf sentinel-6399.conf
vi2 sentinel-6399.conf
prot 26399

3、启动
首先启动master server和master sentinel
./src/redis-server --include redis-master-6379.conf &
./src/redis-sentinel sentinel-6379.conf > sentinel-6379.log &
启动slave1 server和sentinel
./src/redis-server --include redis-slave-6389.conf &
./src/redis-sentinel sentinel-6389.conf > sentinel-6389.log &
启动slave1 server和sentinel
./src/redis-server --include redis-slave-6399.conf &
./src/redis-sentinel sentinel-6399.conf > sentinel-6399.log &

[45564] 28 Jan 15:03:37.444 * +slave slave 127.0.0.1:6389 127.0.0.1 6389 @ mymaster 127.0.0.1 6379
[45564] 28 Jan 15:03:37.444 * +slave slave 127.0.0.1:6399 127.0.0.1 6399 @ mymaster 127.0.0.1 6379
[45564] 28 Jan 15:04:02.364 * +sentinel sentinel 127.0.0.1:26389 127.0.0.1 26389 @ mymaster 127.0.0.1 6379
[45564] 28 Jan 15:04:19.711 * +sentinel sentinel 127.0.0.1:26399 127.0.0.1 26399 @ mymaster 127.0.0.1 6379

查看master的状态:

# ./src/redis-cli -h 127.0.0.1 -p 6379 -a rd123
localhost:6379> info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6389,state=online,offset=54505,lag=0
slave1:ip=127.0.0.1,port=6399,state=online,offset=54505,lag=1
master_repl_offset:54505
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:54504

查看slave1的状态:
# ./src/redis-cli -h localhost -p 6389 -a rd123
localhost:6389> info Replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:59720
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

查看slave2的状态:
# ./src/redis-cli -h localhost -p 6399 -a rd123
localhost:6399> info Replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:68701
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

4、测试
(1)场景一,slave1宕机
localhost:6389> shutdown
在sentinel中
[45794] 28 Jan 15:12:10.335 # +sdown slave 127.0.0.1:6389 127.0.0.1 6389 @ mymaster 127.0.0.1 6379

# ./src/redis-cli -h localhost -p 6379 -a rd123
localhost:6379> info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6399,state=online,offset=120536,lag=1
master_repl_offset:120669
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:120668

(2)场景二,slave恢复
重启slave1
./src/redis-server --include redis-slave-6389.conf &
[3] 52287

[45794] 28 Jan 15:15:19.726 * +reboot slave 127.0.0.1:6389 127.0.0.1 6389 @ mymaster 127.0.0.1 6379
[45794] 28 Jan 15:15:19.874 # -sdown slave 127.0.0.1:6389 127.0.0.1 6389 @ mymaster 127.0.0.1 6379

localhost:6379> info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6399,state=online,offset=197860,lag=1
slave1:ip=127.0.0.1,port=6389,state=online,offset=197727,lag=1
master_repl_offset:198126
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:198125

(3)场景三,master宕机
localhost:6379> shutdown
localhost:6379> info Replication

[45564] 28 Jan 15:36:37.710 # +sdown master mymaster 127.0.0.1 6379
[45564] 28 Jan 15:36:37.967 # +new-epoch 1
[45564] 28 Jan 15:36:37.968 # +vote-for-leader 1f6f588c7c28a2176c2886e540a638ce92033e65 1
[45564] 28 Jan 15:36:38.892 # +odown master mymaster 127.0.0.1 6379 #quorum 3/2
[45564] 28 Jan 15:36:39.178 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6399
[45564] 28 Jan 15:36:39.178 * +slave slave 127.0.0.1:6389 127.0.0.1 6389 @ mymaster 127.0.0.1 6399
[45564] 28 Jan 15:36:39.180 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6399
[45564] 28 Jan 15:37:09.193 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6399

master转换为slave2
localhost:6399> info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6389,state=online,offset=21724,lag=1
master_repl_offset:21990
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:21989

(4)场景四,master恢复
./src/redis-server --include redis-master-6379.conf &
[1] 67400

[45564] 28 Jan 15:41:47.608 # -sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6399
[45564] 28 Jan 15:41:57.513 * +reboot slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6399

原来的master自动切换成slave,不会自动恢复成master

localhost:6379> info Replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6399
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:70642
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

localhost:6399> info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6389,state=online,offset=93539,lag=0
slave1:ip=127.0.0.1,port=6379,state=online,offset=93539,lag=0
master_repl_offset:93553
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:93552

三、redis集群搭建
1、从github中下载最新开发版的redis,https://codeload.github.com/antirez/redis/zip/unstable
2、安装redis
node1 10.25.22.185 6379
node2 10.25.22.186 6379
node3 10.25.22.187 6379
3、修改配置
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
logfile "/appcom/Redis/redis-unstable/redis.log"

./src/redis-server redis.conf &
[1] 6856
./src/redis-server redis.conf &
[1] 43951
./src/redis-server redis.conf &
[1] 80642

在node1中查看集群状态
# ./src/redis-cli
127.0.0.1:6379> cluster nodes
af6224cbc9ce9b66e21b90af442678ba096989d9 :0 myself,master - 0 0 0 connected
127.0.0.1:6379> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0

通过cluster meet命令关联集群各个服务器
127.0.0.1:6379> cluster meet 10.25.22.186 6379
OK
127.0.0.1:6379> cluster meet 10.25.22.187 6379
OK
127.0.0.1:6379> cluster nodes
ed85b32aa566511bf917e8ecdc6150df7449dcf2 10.25.22.187:6379 master - 0 1390897200350 0 connected
af6224cbc9ce9b66e21b90af442678ba096989d9 :0 myself,master - 0 0 0 connected
918fc015490599a93e680893c7e387336dac35bc 10.25.22.186:6379 master - 0 1390897199347 0 connected
127.0.0.1:6379> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:0
cluster_current_epoch:0
cluster_stats_messages_sent:23
cluster_stats_messages_received:23

为集群中各个服务器分配hash slots
Redis Cluster通过hash slot将数据根据主键来分区,所以一条key-value数据会根据算法自动映射到一个hash slot,
但是一个hash slot存储在哪个Redis节点上并不是自动映射的,是需要集群管理者自行分配的。
根据源码得知共有16384个hash slots

修改node-conf文件,保留myself那行记录,其余记录删除
node1的改为:af6224cbc9ce9b66e21b90af442678ba096989d9 :0 myself,master - 0 0 0 connected 0-5000

node2的改为:918fc015490599a93e680893c7e387336dac35bc :0 myself,master - 0 0 0 connected 5001-10000

node3的改为:ed85b32aa566511bf917e8ecdc6150df7449dcf2 :0 myself,master - 0 0 0 connected 10001-16383

之后重启服务器

重新使用cluster meet命令关联各个服务器节点

127.0.0.1:6379> cluster meet 10.25.22.186 6379
OK
127.0.0.1:6379> cluster meet 10.25.22.187 6379
OK
127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:3
cluster_current_epoch:0
cluster_stats_messages_sent:29
cluster_stats_messages_received:29
127.0.0.1:6379>

[[email protected] redis-unstable]# ./src/redis-cli
127.0.0.1:6379> set name "Make"
(error) MOVED 5798 10.25.22.186:6379
[[email protected] redis-unstable]# ./src/redis-cli
127.0.0.1:6379> set name "Make"
OK
127.0.0.1:6379> get name
"Make"

时间: 2024-07-29 05:09:29

redis实现主从复制-单机测试的相关文章

物联网架构成长之路(11)-Redis缓存主从复制

1. 说明 在我的物联网平台框架框架中,会用到Redis这个中间件.作为EMQ权限认证的缓存.https://www.cnblogs.com/think-in-java/p/5123884.html 2. 编译&运行 1 wget http://download.redis.io/releases/redis-4.0.6.tar.gz 2 make && make test && make PREFIX=/home/user/workspace/redis inst

Redis安装(单机及各类集群,阿里云)

Redis安装(单机及各类集群,阿里云) 前言 上周,我朋友突然悄悄咪咪地指着手机上的一篇博客说,这是你的博客吧.我看了一眼,是之前发布的<Rabbit安装(单机及集群,阿里云>.我朋友很哈皮地告诉我,我的博客被某个Java平台进行了微信推送.看到许多人阅读,并认同了我的博客,心理还是很开心的. 好了,话题收回来.这次就Redis在实际服务器中的各种安装,进行详细描述. 另外由于内容较多,并不一定能涵盖各个方面,万望见谅.如果存在什么问题,或者有什么需要添加的,请私信或@我. 最后,由于打马赛

redis的主从复制配置

redis的主从复制配置 一.     原理 Redis的主从复制功能非常强大,一个master可以拥有多个slave,而一个slave又可以拥有多个slave,如此下去,形成了强大的多级服务器集群架构.下面是关于redis主从复制的一些特点:1.master可以有多个slave2.除了多个slave连到相同的master外,slave也可以连接其他slave形成图状结构3.主从复制不会阻塞master.也就是说当一个或多个slave与master进行初次同步数据时,master可以继续处理cl

实现Redis的主从复制配置

实现Redis的主从复制配置比较简单,而且容易明白. 下图是要配置的主从复制结构图: 1.说明 Redis主从复制中一个主服务可以有多个从服务,一个从服务可以有多个从服务. 配置比较简单,只需要更改redis.conf文件中的slaveof参数配置即可. slaveof参数的格式如:slaveof <masterip> <masterport> 如果master服务器设置有密码则需要配置masterauth参数. masterauth参数格式如:masterauth <mas

redis之(十四)redis的主从复制的原理

一:redis主从复制的原理,步骤. 第一步:复制初始化 --->从redis启动后,会根据配置,向主redis发送SYNC命令.2.8版本以后,发送PSYNC命令. --->主redis收到SYNC命令后,开始在后台保存快照文件(即RDB持久化的过程),并将保存快照期间接收到的命令缓存起来. --->当主redis完成快照后,主redis会将快照文件和缓存命令发送给从redis.复制初始化结束. --->当主redis的复制初始化结束后,主redis每当收到写命令就会异步将写命令

Redis介绍及Jedis测试

1.Redis简介 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询. Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱

(转载).NET中使用Redis(三)Redis 的主从复制

Redis跟MySQL一样,拥有非常强大的主从复制功能,而且还支持一个master可以拥有多个slave,而一个slave又可以拥有多个slave,从而形成强大的多级服务器集群架构. redis的主从复制是异步进行的,它不会影响master的运行,所以不会降低redis的处理性能.主从架构中,可以考虑关闭Master的数据持久化功能,只让Slave进行持久化,这样可以提高主服务器的处理性能.同时Slave为只读模式,这样可以避免Slave缓存的数据被误修改. 1.配置 实际生产中,主从架构是在几

【深入学习Redis】主从复制(上)

前言 在前面的两篇文章中,分别介绍了Redis的内存模型和Redis的持久化. 在Redis的持久化中曾提到,Redis高可用的方案包括持久化.主从复制(及读写分离).哨兵和集群.其中持久化侧重解决的是Redis数据的单机备份问题(从内存到硬盘的备份):而主从复制则侧重解决数据的多机热备.此外,主从复制还可以实现负载均衡和故障恢复. 这篇文章中,将详细介绍Redis主从复制的方方面面,包括:如何使用主从复制.主从复制的原理(重点是全量复制和部分复制.以及心跳机制).实际应用中需要注意的问题(如数

企业——Redis的主从复制的搭建

一.Redis的基本简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset(有序集合).这些数据类型都支持push/pop.add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的.在此基础上,redis支持各种不同方式的排序.与memcached一样,为了保证效率,数据都是缓存在内存中.区别的是:redis会周期性的把更新的数据写入磁盘或者把修改