第一篇:Redis 基础命令
第二篇:Redis 常见应用场景
第三篇:Redis Cluster集群搭建
第四篇:Redis 主从及哨兵搭建
第五篇:Redis 主从及集群
第六篇:Redis 持久化
第七篇:Redis 分布式锁
第八篇:Redis 底层数据存储结构
第九篇:Redis 面试常问问题
redis主从及哨兵配置
提示:以下是本篇文章正文内容,下面案例可供参考
以下是我的搭建步骤
- 复制安装目录下的
bin/
目录,到/usr/local/redis-master-slave/
并重命名为master8000
,修改配置项:
# bind 127.0.0.1 -::1
# 注释bind或者指定ipprotected-mode no
# 修改yes
为no
daemonize yes
# 后台运行port 8000
# 修改6379
为8000
, 以8000
为master端口号masterauth 1234
# master密码,master也需要配置requirepass 1234
# 修改密码- 复制
master8000
,命名为slave8001
、slave8002
,修改端口号和replicaof
(4.0版本以前是slaveof)选项:
port 8001
replicaof 182.254.134.174 8000
- 启动主从服务
[root@VM-0-12-centos local]# cp -r ./redis/bin/ ./redis-master-slave/master8000
[root@VM-0-12-centos local]# cd ./redis-master-slave
[root@VM-0-12-centos redis-master-slave]# vim redis.conf
# bind 127.0.0.1 -::1
protected-mode no
port 8000
masterauth 1234
requirepass 1234
[root@VM-0-12-centos redis-master-slave]# cp -r master8000/ slave8001
[root@VM-0-12-centos redis-master-slave]# cp -r master8000/ slave8002
[root@VM-0-12-centos redis-master-slave]# vim ./slave8001/redis.conf
port 8001
replicaof 182.254.134.174 8000
[root@VM-0-12-centos redis-master-slave]# vim ./slave8002/redis.conf
port 8002
replicaof 182.254.134.174 8000
[root@VM-0-12-centos redis-master-slave]# vim start-all.sh # 写一个启动脚本
cd master8000
./redis-server ./redis.conf
cd ..
cd slave8001
./redis-server ./redis.conf
cd ..
cd slave8002
./redis-server ./redis.conf
cd ..
[root@VM-0-12-centos redis-master-slave]# ./start-all.sh
[root@VM-0-12-centos redis-master-slave]# ps -ef | grep redis
root 32197 1 0 11:00 ? 00:00:00 ./redis-server *:8000
root 32203 1 0 11:00 ? 00:00:00 ./redis-server *:8001
root 32205 1 0 11:00 ? 00:00:00 ./redis-server *:8002
root 32244 18521 0 11:01 pts/2 00:00:00 grep --color=auto redis
[root@VM-0-12-centos redis-master-slave]# ./master8000/redis-cli -p 8000 -a 1234
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:8000> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=182.254.134.174,port=8001,state=online,offset=126,lag=1
slave1:ip=182.254.134.174,port=8002,state=online,offset=126,lag=1
master_failover_state:no-failover
master_replid:3a59c2e1097319c0c09b37fa3b0a11911d54aa0e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:126
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:126
127.0.0.1:8000> set aaa 123
OK
127.0.0.1:8000>
连接8001控制台,能够获取master写入的数据
[root@VM-0-12-centos redis-master-slave]# ./slave8001/redis-cli -p 8001 -a 1234
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:8001> info replication
# Replication
role:slave
master_host:182.254.134.174
master_port:8000
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_read_repl_offset:952
slave_repl_offset:952
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:3a59c2e1097319c0c09b37fa3b0a11911d54aa0e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:952
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:952
127.0.0.1:8001> get aaa
"123"
127.0.0.1:8001>
master下线
127.0.0.1:8000> shutdown
not connected>
slave8001 执行slaveof no one
命令升为master
127.0.0.1:8001> slaveof no one
OK
127.0.0.1:8001> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:4165062e260e2fc005030d76d7f653a61c809907
master_replid2:3a59c2e1097319c0c09b37fa3b0a11911d54aa0e
master_repl_offset:11702
second_repl_offset:11703
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:11702
127.0.0.1:8001>
master重新启动后还是master的身份,这时把slave8001恢复从机身份
127.0.0.1:8001> slaveof 182.254.134.174 8000
OK
127.0.0.1:8001> info replication
# Replication
role:slave
master_host:182.254.134.174
master_port:8000
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:11702
slave_repl_offset:11702
master_link_down_since_seconds:-1
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:4165062e260e2fc005030d76d7f653a61c809907
master_replid2:3a59c2e1097319c0c09b37fa3b0a11911d54aa0e
master_repl_offset:11702
second_repl_offset:11703
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:11702
127.0.0.1:8001>
上面主从之间还没办法自动切换,所以接下来看安装哨兵
当服务器宕机后,需要手动的一台从机切换为主机,这需要人工干预,不仅费时费力而且会造成一段时间内服务不可用。为了解主从复制的缺点,就有了哨兵机制。
- 找到安装目录下的
sentinel.conf
文件复制到/usr/local/redis-master-slave/master8000/
目录下- 修改sentinel.conf
protected-mode no
port 28000
# 改26379为28000daemonize yes
# 后台运行sentinel monitor mymaster 127.0.0.1 8000 2
sentinel auth-pass mymaster 1234
# redis主从密码logfile "/usr/local/redis-master-slave/master8000/sentinel.log"
# 日志位置- 创建脚本启动哨兵
sentinel-start.sh
,
新手的话(嗯,我就是)最好在测试之前保存一份配置文件备份,因为sentinel.conf
会重写所以如果测试出现问题重启后加载的是修改后的配置,会导致一直监控混乱。redis.conf
也备份一下 replicaof
参数也会被修改,下面我会写一个脚本在启动服务之前进行配置文件还原。
[root@VM-0-12-centos master8000]# find / -name sentinel.conf
/usr/local/redis/redis-7.0.4/tests/sentinel/tests/includes/sentinel.conf
/usr/local/redis/redis-7.0.4/sentinel.conf # redis安装目录下的sentinel.conf文件
[root@VM-0-12-centos master8000]# cp /usr/local/redis/redis-7.0.4/sentinel.conf ./
[root@VM-0-12-centos master8000]# vim sentinel.conf
protected-mode no
port 28000
daemonize yes
sentinel monitor mymaster 127.0.0.1 8000 2
logfile "/usr/local/redis-master-slave/master8000/sentinel.log"
[root@VM-0-12-centos master8000]# cp ./sentinel.conf ../slave8001
[root@VM-0-12-centos master8000]# vim ../slave8001/sentinel.conf
port 28001
logfile "/usr/local/redis-master-slave/slave8001/sentinel.log"
[root@VM-0-12-centos master8000]# cp ./sentinel.conf ../slave8002
[root@VM-0-12-centos master8000]# vim ../slave8002/sentinel.conf
port 28002
logfile "/usr/local/redis-master-slave/slave8002/sentinel.log"
[root@VM-0-12-centos master8000]# cd ..
[root@VM-0-12-centos master8000]# vim sentinel-start.sh
cd master8000
./redis-sentinel ./sentinel.conf
cd ..
cd slave8001
./redis-sentinel ./sentinel.conf
cd ..
cd slave8002
./redis-sentinel ./sentinel.conf
cd ..
[root@VM-0-12-centos master8000]# vim sentinel-stop.sh
cd master8000
./redis-cli -p 28000 shutdown
cd ..
cd slave8001
./redis-cli -p 28001 shutdown
cd ..
cd slave8002
./redis-cli -p 28002 shutdown
cd ..
[root@VM-0-12-centos redis-master-slave]# ./sentinel-start.sh
[root@VM-0-12-centos redis-master-slave]# ps -ef | grep redis
root 26057 1 0 16:15 ? 00:00:00 ./redis-sentinel *:28000 [sentinel]
root 26063 1 0 16:15 ? 00:00:00 ./redis-sentinel *:28001 [sentinel]
root 26069 1 0 16:15 ? 00:00:00 ./redis-sentinel *:28002 [sentinel]
root 18897 18521 0 15:38 pts/2 00:00:00 grep --color=auto redis
root 26326 1 0 13:24 ? 00:00:08 ./master8000/redis-server *:8000
root 32203 1 0 11:00 ? 00:00:15 ./redis-server *:8001
root 32205 1 0 11:00 ? 00:00:15 ./redis-server *:8002
[root@VM-0-12-centos redis-master-slave]# ./master8000/redis-cli -p 28000
127.0.0.1:28000> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=127.0.0.1:8002,slaves=4,sentinels=3
主从自动切换,master下线,slave8001自动升为master
[root@VM-0-12-centos redis-master-slave]# ./master8000/redis-cli -p 8000 -a 1234
127.0.0.1:8000> shutdown
not connected>
[root@VM-0-12-centos redis-master-slave]# ./slave8001/redis-cli -p 8001 -a 1234
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:8001> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=8002,state=online,offset=731134,lag=1
master_failover_state:no-failover
master_replid:e16dbdf67b2a849f944438f2d7f982fb10696c41
master_replid2:50204c1cf1e157582bc33a560e9faec8b80e57ef
master_repl_offset:731283
second_repl_offset:725414
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:624157
repl_backlog_histlen:107127
127.0.0.1:8001>
[root@VM-0-12-centos redis-master-slave]#
[root@VM-0-12-centos redis-master-slave]# ./master8000/redis-server ./redis.conf # 重新启动
[root@VM-0-12-centos redis-master-slave]# ./master8000/redis-cli -p 8000 -a 1234
127.0.0.1:8000> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:8001
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:764200
slave_repl_offset:764200
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:e16dbdf67b2a849f944438f2d7f982fb10696c41
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:764200
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:763908
repl_backlog_histlen:293
127.0.0.1:8000>
配置初始化脚本
cd master8000
yes | cp -f ./sentinel-conf.bak ./sentinel.conf
yes | cp -f ./redis-conf.bak ./redis.conf
cd ..
cd slave8001
yes | cp -f ./sentinel-conf.bak ./sentinel.conf
yes | cp -f ./redis-conf.bak ./redis.conf
cd ..
cd slave8002
yes | cp -f ./sentinel-conf.bak ./sentinel.conf
yes | cp -f ./redis-conf.bak ./redis.conf
cd ..
主从同步配置参数优化
repl-diskless-sync no #是否使用无盘同步RDB文件,默认为no,no为不使用无盘,将rdb保存到磁盘再发给slave
repl-diskless-sync-delay 5 #Master准备好RDB文件后等等待传输时间
repl-ping-slave-period 10 #slave端向server端发送ping的时间区间设置,默认为10秒
repl-timeout 60 #设置超时时间
repl-disable-tcp-nodelay no #是否启用TCP_NODELAY,如设置成yes,则redis会合并小的TCP包从而节省带宽,但会增加同步延迟(40ms),造成master与slave数据不一致,假如设置成no,则redis master会立即发送同步数据,没有延迟,前者关注性能,后者关注redis服务中的数据一致性。
repl-backlog-size 3072mb #master的写入数据缓冲区,用于记录自上一次同步后到下一次同步过程中间的写入命令。
repl-backlog-ttl 3600 #如果一段时间后没有slave连接到master,则backlog size的内存将会被释放。如果值为0则表示永远不释放这部份内存。
slave-priority 100 #slave端的优先级设置,值是一个整数,数字越小表示优先级越高。当master故障时将会按照优先级来选择slave端进行恢复,如果值设置为0,则表示该slave永远不会被选择。
min-slaves-to-write 1 #设置一个master端的可用slave少于多少个
min-slaves-max-lag 20 #设置所有slave延迟时间都大于多少秒时,master不接收写操作(拒绝写入)。
参考:搭建redis哨兵集群时,哨兵无法感知到其他哨兵
参考:【redis哨兵模式】解决redis哨兵模式无法切换的问题
就是上述所说的启动哨兵会修改配置文件,导致主从参数配置都产生了变化,而在重新启动时导致独取的时之前的配置,不是效果总是与预期不符就是哨兵无法正常工作,如果时自己个人测试的话建议备份一下所有的redis.conf
和sentinel.conf
,出现问题可以直接重启。如果时线上的话那就时运维的事了,咱管不着