redis 主从哨兵01
主从复制过程
- 1.从服务器开始连接主服务器时,会向主服务器发送一个
SYNC
同步命令 - 2.主服务器接收到命令后,执行
BGSAVE
,异步的将写命令保存到一个缓冲区里 - 3.主服务器执行完
BGSAVE
之后,就.rdb文件(BOF的持久化文件)发送给从服务器,从服务器从该文件恢复数据到内存中 - 4.主服务器还会以Redis命令协议的格式,将写命令缓冲区中积累的所有内容发送给从服务器,从服务器通过执行命令将数据恢复到内存中
1.Redis安装
- 1)下载解压,不需要编译
wget http://download.redis.io/releases/redis-4.0.8.tar.gz
tar -zxvf redis-4.0.8.tar.gz
cd redis-4.0.8
make && make install
- 2)结束之后在目录
/usr/local/bin
会生成如下文件
-rwxr-xr-x. 1 root root 5.5M Mar 29 10:55 redis-server
-rwxr-xr-x. 1 root root 2.4M Mar 29 10:55 redis-benchmark
-rwxr-xr-x. 1 root root 2.5M Mar 29 10:55 redis-cli
-rwxr-xr-x. 1 root root 5.5M Mar 29 10:55 redis-check-rdb
-rwxr-xr-x. 1 root root 5.5M Mar 29 10:55 redis-check-aof
lrwxrwxrwx. 1 root root 12 Mar 29 10:55 redis-sentinel -> redis-server
也可以安装到指定目录
make install PREFIX=/usr/local/redis
- 3)从源码目录拷贝单独的配置文件
mkdir -p /usr/local/redis/etc
cp /root/redis-4.0.8/*.conf /usr/local/redis/etc
- 4)配置连接地址和密码
vi /usr/local/redis/etc/redis.conf
#bind 127.0.0.1
requirepass foobared
daemonize yes
#protected-mode yes
dir /usr/local/redis/work
#slaveof 172.16.3.140 6379
# master不需要设置slaveof,只有slave才需要
- 5)启动脚本
cp /root/redis-4.0.8/utils/redis_init_script /etc/init.d/redis
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
PASSWORD=foobared
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/etc/redis_${REDISPORT}.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT -a $PASSWORD shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
- 6)启动验证
/etc/init.d/redis start
[root@haproxy02 init.d]# redis-cli -h 172.16.3.141 -p 6379 -a foobared
172.16.3.141:6379>
2.Redis主从复制
master: 172.16.3.140
slave01: 172.16.3.141
slave02: 172.16.3.142
1)永久主从配置
- 在从库进行配置
vi /usr/local/redis/etc/redis.conf
#slaveof <masterip> <masterport> # 将当前slave指向master的IP和主库端口
#masterauth <master password> # 指定连接mster时的密码
#bind 127.0.0.1
slaveof 172.16.3.140 6379
requirepass foobared
masterauth foobared
daemonize yes
#protected-mode yes
dir /usr/local/redis/work
slave-serve-statle-data yes # 当redis master出故障时,是否可以继续请求slave
slave-read-only yes # 设置slave只读
repl-ping-slave-period 10 # slave每隔10秒,ping一次master
slave-priority 100 # slave成为master的优先级,不同从库设置不同
2)临时主从配置,重启失效
需要授权
- 在从库使用redis-cli客户端配置
slaveof <masterip> <masterport>
redis-cli -h 172.16.3.141 -p 6379 -a foobared
slaveof 172.16.3.140 6379
[root@haproxy02 bin]# redis-cli -h 172.16.3.141 -p 6379 -a foobared
172.16.3.141:6379> slaveof 172.16.3.140 6379
OK
redis-cli -h 172.16.3.142 -p 6379 -a foobared
slaveof 172.16.3.140 6379
[root@mycat01 bin]# redis-cli -h 172.16.3.142 -p 6379 -a foobared
172.16.3.142:6379> slaveof 172.16.3.140 6379
OK
3.查看主从配置情况
- 1)主库
info replication
172.16.3.141:6379> info replication
# Replication
role:slave
master_host:172.16.3.140
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:98
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:4e7f0f8fd5262d3a05167a6392644b2ea6c8473d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:98
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:43
repl_backlog_histlen:56
- 2)主库
info replication
172.16.3.141:6379> info replication
# Replication
role:slave
master_host:172.16.3.140
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1522295362
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:79fb7497f701f546e3951f79f73467806fa435bd
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
- 3)主库
info relication
172.16.3.142:6379> info replication
# Replication
role:slave
master_host:172.16.3.140
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:126
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:4e7f0f8fd5262d3a05167a6392644b2ea6c8473d
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
4.确认同步
- 1)主库set key值
172.16.3.140:6379> select 15
OK
172.16.3.140:6379[15]> set k1 v1
OK
- 2)slave01 get key值
172.16.3.141:6379> get k1
(nil)
172.16.3.141:6379> select 15
OK
172.16.3.141:6379[15]> get k1
"v1"
- 3)slave02 get key值
172.16.3.142:6379> select 15
OK
172.16.3.142:6379[15]> get k1
"v1"
- 4)down掉slave02重新get key值
[root@mycat01 ~]# ps -ef|grep redis
root 12428 1 0 12:03 ? 00:00:00 /usr/local/redis/bin/redis-server *:6379
root 12434 8465 0 12:04 pts/1 00:00:00 redis-cli -h 172.16.3.142 -p 6379 -a foobared
root 12457 12436 0 12:09 pts/2 00:00:00 grep --color=auto redis
[root@mycat01 ~]# kill -9 12428
[root@mycat01 ~]# ps -ef|grep redis
root 12434 8465 0 12:04 pts/1 00:00:00 redis-cli -h 172.16.3.142 -p 6379 -a foobared
root 12460 12436 0 12:09 pts/2 00:00:00 grep --color=auto redis
[root@mycat01 ~]# /etc/init.d/redis start
Starting redis (via systemctl):
172.16.3.142:6379> select 15
OK
172.16.3.142:6379[15]> get k1
"v1"
5.人肉手工切slave为master
- 1)当master挂了
[root@haproxy01 etc]# ps -ef|grep redis
root 7860 1 0 13:31 ? 00:00:01 /usr/local/redis/bin/redis-server *:6379
root 10123 15648 0 13:44 pts/1 00:00:00 grep --color=auto redis
[root@haproxy01 etc]# kill -9 7860
[root@haproxy01 etc]# ps -ef|grep redis
root 10156 15648 0 13:44 pts/1 00:00:00 grep --color=auto redis
- 2)从库上的状态
master_link_status:down
172.16.3.141:6379> info replication
# Replication
role:slave
master_host:172.16.3.140
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1146
master_link_down_since_seconds:8
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:8e08f00217d957d8365018f714737abfe271bcd0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1146
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:1132
- 3)在从库slave01上取消slave角色
slaveof no one
172.16.3.141:6379> slaveof no one
OK
172.16.3.141:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:4d3e73fa16b05139e7cb1b440c788ccd13200ca5
master_replid2:8e08f00217d957d8365018f714737abfe271bcd0
master_repl_offset:1146
second_repl_offset:1147
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:1132
- 4)从库slave02指向先的master
172.16.3.142:6379> info replication
# Replication
role:slave
master_host:172.16.3.140
master_port:6379
master_link_status:down # 原来的master挂了
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1146
master_link_down_since_seconds:207
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:8e08f00217d957d8365018f714737abfe271bcd0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1146
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:43
repl_backlog_histlen:1104
172.16.3.142:6379> slaveof 172.16.3.141 6379
OK
172.16.3.142:6379> auth foobared
OK
172.16.3.142:6379> info replication
# Replication
role:slave
master_host:172.16.3.141
master_port:6379
master_link_status:up #已经指向到新的master
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:1174
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:4d3e73fa16b05139e7cb1b440c788ccd13200ca5
master_replid2:8e08f00217d957d8365018f714737abfe271bcd0
master_repl_offset:1174
second_repl_offset:1147
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:43
repl_backlog_histlen:1132
6.哨兵模式sentinel.conf
- 1)当前主从模式
172.16.3.141:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.16.3.142,port=6379,state=online,offset=1720,lag=1
slave1:ip=172.16.3.140,port=6379,state=online,offset=1720,lag=1
- 2)配置所有节点的sentinel
vi /usr/local/redis/etc/sentinel.conf
dir "/usr/local/redis/work"
logfile "/usr/local/redis/sentinel.log"
daemonize yes
protected-mode no
sentinel monitor mymaster 172.16.3.140 6379 2
# 上面的mymaster随意起,但是一定要放在下面这行引用的名字之前,不然会报名字找不到
sentinel auth-pass mymaster foobared
- 3)启动sentinel监控
redis-sentinel /usr/local/redis/etc/sentinel.conf
14332:X 29 Mar 14:02:37.607 # +monitor master mymaster 172.16.3.141 6379 quorum 2
14332:X 29 Mar 14:03:07.660 # +sdown master mymaster 172.16.3.141 6379 # ???
- 4)关闭master 172.16.3.141
[root@haproxy02 init.d]# ps -ef|grep redis
root 12880 1 0 13:32 ? 00:00:04 /usr/local/redis/bin/redis-server *:6379
root 12919 5968 0 13:32 pts/2 00:00:00 redis-cli -h 172.16.3.141 -p 6379 -a foobared
root 14332 14166 0 14:02 pts/3 00:00:01 redis-sentinel *:26379 [sentinel]
root 14667 32366 0 14:09 pts/1 00:00:00 grep --color=auto redis
[root@haproxy02 init.d]# kill -9 12880
[root@haproxy02 init.d]# ps -ef|grep redis
root 12919 5968 0 13:32 pts/2 00:00:00 redis-cli -h 172.16.3.141 -p 6379 -a foobared
root 14332 14166 0 14:02 pts/3 00:00:01 redis-sentinel *:26379 [sentinel]
root 14676 32366 0 14:09 pts/1 00:00:00 grep --color=auto redis
- 5)master已经飘到其中一个slave上了
172.16.3.141:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.16.3.142,port=6379,state=online,offset=6827,lag=1
- 6)sentinel切换日志
23205:X 29 Mar 17:06:28.590 # +sdown master mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:28.590 # +odown master mymaster 172.16.3.140 6379 #quorum 1/1
23205:X 29 Mar 17:06:28.590 # +new-epoch 98
23205:X 29 Mar 17:06:28.590 # +try-failover master mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:28.592 # +vote-for-leader 863c1c8c627415dbc3004deb529d27df2299c2df 98
23205:X 29 Mar 17:06:28.597 # da7c4243e90de25a81ed87acd6a4e40a53c860e3 voted for 863c1c8c627415dbc3004deb529d27df2299c2df 98
23205:X 29 Mar 17:06:28.670 # +elected-leader master mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:28.670 # +failover-state-select-slave master mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:28.725 # +selected-slave slave 172.16.3.141:6379 172.16.3.141 6379 @ mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:28.725 * +failover-state-send-slaveof-noone slave 172.16.3.141:6379 172.16.3.141 6379 @ mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:28.792 * +failover-state-wait-promotion slave 172.16.3.141:6379 172.16.3.141 6379 @ mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:29.795 # +promoted-slave slave 172.16.3.141:6379 172.16.3.141 6379 @ mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:29.795 # +failover-state-reconf-slaves master mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:29.850 * +slave-reconf-sent slave 172.16.3.142:6379 172.16.3.142 6379 @ mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:30.844 * +slave-reconf-inprog slave 172.16.3.142:6379 172.16.3.142 6379 @ mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:30.844 * +slave-reconf-done slave 172.16.3.142:6379 172.16.3.142 6379 @ mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:30.927 # +failover-end master mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:30.927 # +switch-master mymaster 172.16.3.140 6379 172.16.3.141 6379
23205:X 29 Mar 17:06:30.927 * +slave slave 172.16.3.142:6379 172.16.3.142 6379 @ mymaster 172.16.3.141 6379
23205:X 29 Mar 17:06:30.927 * +slave slave 172.16.3.140:6379 172.16.3.140 6379 @ mymaster 172.16.3.141 6379
23205:X 29 Mar 17:07:00.953 # +sdown slave 172.16.3.140:6379 172.16.3.140 6379 @ mymaster 172.16.3.141 6379
- 7)注意
21239:X 29 Mar 16:43:12.722 # +try-failover master mymaster 172.16.3.140 6379
21239:X 29 Mar 16:43:12.724 # +vote-for-leader 863c1c8c627415dbc3004deb529d27df2299c2df 95
21239:X 29 Mar 16:43:23.438 # -failover-abort-not-elected master mymaster 172.16.3.140 6379
21239:X 29 Mar 16:43:23.497 # Next failover delay: I will not start a failover before Thu Mar 29 16:49:13 2018
当出现上面停掉master后,无法failover,我用的是第一种方法
1)如果redis实例没有配置
protected-mode yes
bind 192.168.98.136
则在sentinel 配置文件加上
protected-mode no
即可
2)如果redis实例有配置
protected-mode yes
bind 192.168.98.136
则在sentinel 配置文件加上
protected-mode yes
bind 192.168.98.136
即可
redis常用命令
- 1)指定参数运行
/usr/local/redis-server /usr/local/redis/etc/redis.conf
- 2)停止redis-server服务
redis -cli shutdown
- 3)远程关闭服务
redis-cli -h xxx.xxx.xxx.xxx -p xxx -u xxx -a xxx shutdown
- 4)命令行获取密码
172.16.3.141:6379> config get requirepass
1) "requirepass"
2) "foobared"
redis命令
redis-benchmark
压力测试工具redis-check-aof
检查redis持久化命令文件的完整性redis-check-dump
检查redis持久化数据文件的完整性redis-cli
客户端命令redis-sentinel
sentinel集群管理工具,主要负责主从切换,ln到redis-server
redis-server
服务daemon启动程序
https://blog.csdn.net/zhu1289303556/article/details/50839112
https://blog.csdn.net/yingxiake/article/details/51669030
https://blog.csdn.net/xuxile/article/details/52213854
http://blog.51cto.com/ckl893/1769094
https://blog.csdn.net/yypzye/article/details/52281282
https://blog.csdn.net/huangjin0507/article/details/50737671
https://blog.csdn.net/vtopqx/article/details/49247285
redis 主从哨兵01的更多相关文章
- redis主从+ 哨兵模式(sentinel)+漂移VIP实现高可用系统
原文:https://www.jianshu.com/p/c2ab606b00b7 客户端程序 客户端程序(如PHP程序)连接redis时需要ip和port,但redis-server进行故障转移时, ...
- Redis 主从+哨兵+监控 (centos7.2 + redis 3.2.9 )
环境准备: 192.168.0.2 redis01 主 192.168.0.3 redis02 从 192.168.0.4 redis03 从 Redis 主从搭建 一:下载并安装redis软件 ...
- redis主从+哨兵模式
主从模式配置分为手动和配置文件两种方式进行配置,我现在有192.168.238.128(CentOS1).192.168.238.131(CentOS3).192.168.238.132(CentOS ...
- redis主从+哨兵模式(借鉴)
三台机器分布 192.168.189.129 // master的角色 192.168.189.130 // slave1的角色 192.168.189.131 // salve2的角色 ...
- 【Redis学习专题】- Redis主从+哨兵集群部署
集群版本: redis-4.0.14 集群节点: 节点角色 IP redis-master 10.100.8.21 redis-slave1 10.100.8.22 redis-slave2 10.1 ...
- Redis主从&哨兵集群搭建
主从集群 在搭建主从集群前,我们先把Redis安装起来: #解压Redis压缩包 [root@master lf]# tar -zxvf redis-6.2.1.tar.gz -- #安装gcc [r ...
- 三千字介绍Redis主从+哨兵+集群
一.Redis持久化策略 1.RDB 每隔几分钟或者一段时间会将redis内存中的数据全量的写入到一个文件中去. 优点: 因为他是每隔一段时间的全量备份,代表了每个时间段的数据.所以适合做冷备份. R ...
- redis 主从 哨兵
数据库为什么要读写分离 写代码好多年了,大家先抛弃在代码框架里面各种花哨的设计之外,写的代码到最后无非就是为了增删查改数据库.一般项目数据库刚开始只是但一个库,随着数据量的增大,就开始优化数据库(抛开 ...
- Redis主从哨兵和集群搭建
主从配置 哨兵配置 集群配置 1.主从: 国王和丞相,国王权力大(读写),丞相权利小(读) 2.哨兵: 国王和王子,国王死了(主服务挂掉),王子继位(从服务变主服务) 3.集群: 国王和国王,一个国王 ...
随机推荐
- Linux open() 一个函数,两个函数原型
open在手册中有两个函数原型, 如下所示: int open(const char *pathname, int flags); int open(const char *pathname, int ...
- Git 提交项目命令
git add . //添加⽂件到待提交区 git commit -m "注释" //创建⼀个提交 git push origin //将修改内容提交
- java 设计模式-责任链
责任链设计模式,其实就是处理同一个请求的对象连接成一条链,请求的路径经过这条链,符合要求的就处理这个请求,不符合就接着往下面抛出,直道有人处理这条请求. 业务:比如啊,公司个人请假,三天以下就是主管审 ...
- Redis(三):多机数据库的实现
复制 在Redis中,用户可以通过SLAVEOF命令或是slaveof选项设置服务器的主从关系,从(SLAVE)服务器会复制主(Master)服务器. 旧版复制功能实现(2.8以前) 旧版复制功能主要 ...
- codeforce 1311 C. Perform the Combo 前缀和
You want to perform the combo on your opponent in one popular fighting game. The combo is the string ...
- python的unittest框架中的assert断言
unittest框架自带断言,如果想用assert断言,一定要引入unittest.TestCase框架才行,不然不会自动识别assert断言
- spark系列-3、缓存、共享变量
一.persist 和 unpersist 1.1.persist() : 用来设置RDD的存储级别 存储级别 意义 MEMORY_ONLY 将RDD作为反序列化的的对象存储JVM中.如果RDD不能 ...
- 题目分享Y
题意:给出一个n个点n条边的图且不一定连通(原题面为每个节点出度为1),相邻节点不能同时被选,每个节点有其对应价值,求最多能获得多少价值?n<=1e6,val[i]<=1e6 分析:很容易 ...
- C. Helga Hufflepuff's Cup 树形dp 难
C. Helga Hufflepuff's Cup 这个题目我感觉挺难的,想了好久也写了很久,还是没有写出来. dp[i][j][k] 代表以 i 为根的子树中共选择了 j 个特殊颜色,且当前节点 i ...
- 设计模式(Java语言)- 建造者模式
前言 在日常的生活中,我们可以经常看到建造者模式的影子.比如,建造房子,那么房子就是一个产品,房子由门,窗,墙,地板等部门组成.然后包工头在建造房子的时候就根据设计好的图纸来建造,但是包工头并不是亲自 ...