主从复制过程

  • 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>

https://blog.csdn.net/zgf19930504/article/details/51850594

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的更多相关文章

  1. redis主从+ 哨兵模式(sentinel)+漂移VIP实现高可用系统

    原文:https://www.jianshu.com/p/c2ab606b00b7 客户端程序 客户端程序(如PHP程序)连接redis时需要ip和port,但redis-server进行故障转移时, ...

  2. Redis 主从+哨兵+监控 (centos7.2 + redis 3.2.9 )

    环境准备: 192.168.0.2  redis01 主 192.168.0.3  redis02 从 192.168.0.4  redis03 从 Redis 主从搭建 一:下载并安装redis软件 ...

  3. redis主从+哨兵模式

    主从模式配置分为手动和配置文件两种方式进行配置,我现在有192.168.238.128(CentOS1).192.168.238.131(CentOS3).192.168.238.132(CentOS ...

  4. redis主从+哨兵模式(借鉴)

    三台机器分布 192.168.189.129  //  master的角色 192.168.189.130  //  slave1的角色 192.168.189.131  //  salve2的角色 ...

  5. 【Redis学习专题】- Redis主从+哨兵集群部署

    集群版本: redis-4.0.14 集群节点: 节点角色 IP redis-master 10.100.8.21 redis-slave1 10.100.8.22 redis-slave2 10.1 ...

  6. Redis主从&哨兵集群搭建

    主从集群 在搭建主从集群前,我们先把Redis安装起来: #解压Redis压缩包 [root@master lf]# tar -zxvf redis-6.2.1.tar.gz -- #安装gcc [r ...

  7. 三千字介绍Redis主从+哨兵+集群

    一.Redis持久化策略 1.RDB 每隔几分钟或者一段时间会将redis内存中的数据全量的写入到一个文件中去. 优点: 因为他是每隔一段时间的全量备份,代表了每个时间段的数据.所以适合做冷备份. R ...

  8. redis 主从 哨兵

    数据库为什么要读写分离 写代码好多年了,大家先抛弃在代码框架里面各种花哨的设计之外,写的代码到最后无非就是为了增删查改数据库.一般项目数据库刚开始只是但一个库,随着数据量的增大,就开始优化数据库(抛开 ...

  9. Redis主从哨兵和集群搭建

    主从配置 哨兵配置 集群配置 1.主从: 国王和丞相,国王权力大(读写),丞相权利小(读) 2.哨兵: 国王和王子,国王死了(主服务挂掉),王子继位(从服务变主服务) 3.集群: 国王和国王,一个国王 ...

随机推荐

  1. XEP-0199 XMPP Ping

    原文来自:https://xmpp.org/extensions/xep-0199.html,只翻译了技术方面的内容. 摘要:这个规范定义了一个通过XML流发送应用级别pings的XMPP扩展协议.这 ...

  2. 【思科】OSI和TCP/IP分层

    OSI参考模型 20世纪70年代,ISO创建OSI参考模型,希望不同供应商的网络能够相互协同工作 OSI:开放系统互联 open system interconnection ISO:国际标准化组织  ...

  3. 【Linux常见命令】dos2unix命令,unix2dos命令

    我们都知道.打回车键就是换行的意思. 在不同系统下打回车键效果是不同的: MAC OS下:dakdhih \r LINUX下:dakdhih \n DOS\WINDOWS下:dakdhih \r\n ...

  4. mysql不同端口的连接

    连接mysql3306端口命令 mysql -h58.64.217.120 -ushop -p123456 连接非3306端口(指定其他端口) 的命令 mysql -h58.64.217.120 -P ...

  5. 在独立的 Root 和 Home 硬盘驱动器上安装 Ubuntu

    安装 Linux 系统时,可以有两种不同的方式.第一种方式是在一个超快的固态硬盘上进行安装,这样可以保证迅速开机和高速访问数据.第二种方式是在一个较慢但很强大的普通硬盘驱动器上安装,这样的硬盘转速快并 ...

  6. linux和windows互传文件、用户配置文件和密码配置文件、用户组管理、用户管理...

    linux和windows互传文件 第一种:在linux主机上下载lrzsz软件包 1.yum install lrzsz 2.通过rz命令上传window的文件到linux主机上 用过sz 文件名下 ...

  7. Codeforces Round #618 (Div. 2)-B. Assigning to Classes

    Reminder: the median of the array [a1,a2,-,a2k+1] of odd number of elements is defined as follows: l ...

  8. Codeforces Round #618 (Div. 2)-Non-zero

    Guy-Manuel and Thomas have an array a of n integers [a1,a2,-,an]. In one step they can add 1 to any ...

  9. Jmeter 数据库测试

    1.环境准备,下载驱动 mysql-connector-java-5.1.45-bin.jar 下载的 jar 包保存在 Jmeter 的文件的 lib 下的 ext 目录下,则不需要做其他的配置了, ...

  10. git新手使用教程包含各种系统

    Git Tutorial 1.下载客户端 从Git官网下载客户端:   https://git-scm.com/   Windows版下载地址:   https://git-scm.com/downl ...