redis 主从复制

redis 主从复制 Sentine哨兵机制 cluster集群搭建

redis 主从复制
实验环境
系统版本:CentOS Linux release 7.6.1810 (Core)
redis版本:redis-6.2.6.tar
三台设备:
主机:192.168.217.11
从机1:1921.68.217.12
从机2:192.168.217.13
为了实验顺利进行;首先关闭系统防火墙
systemctl stop frewalld
三台设备安装redis
源码安装

[root@localhost ~]# tar -zxf redis-6.2.6.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/redis-6.2.6/
[root@localhost redis-6.2.6]# vim redis.conf 
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT OUT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

bind 0.0.0.0  -::1    #修改此处  0.0.0.0  任何设备均可访问

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:

......

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379      #修改redis端口号 (此实验未修改)

# TCP listen() backlog.
#
# In high requests-per-second environments you need a high backlog in order

......

编译安装

[root@localhost redis-6.2.6]# make && make install 

启动redis

[root@localhost redis-6.2.6]# redis-server  redis.conf &
1] 18304
[root@localhost redis-6.2.6]# 18304:C 25 May 2022 05:35:35.331 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18304:C 25 May 2022 05:35:35.331 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=18304, just started
18304:C 25 May 2022 05:35:35.331 # Configuration loaded
18304:M 25 May 2022 05:35:35.333 * Increased maximum number of open files to 10032 (it was originally set to 1024).
18304:M 25 May 2022 05:35:35.333 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 18304
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

18304:M 25 May 2022 05:35:35.335 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
18304:M 25 May 2022 05:35:35.335 # Server initialized
18304:M 25 May 2022 05:35:35.335 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
18304:M 25 May 2022 05:35:35.368 * Loading RDB produced by version 6.2.6
18304:M 25 May 2022 05:35:35.368 * RDB age 362528 seconds
18304:M 25 May 2022 05:35:35.368 * RDB memory usage when created 0.84 Mb
18304:M 25 May 2022 05:35:35.368 # Done loading RDB, keys loaded: 0, keys expired: 0.
18304:M 25 May 2022 05:35:35.368 * DB loaded from disk: 0.033 seconds
18304:M 25 May 2022 05:35:35.368 * Ready to accept connections

[root@localhost redis-6.2.6]# redis-cli 

如果此处无法启动 请先安装redis插件

[root@localhost ]# yum -y install gcc tcl

查看主从状态 (此处也可以用info查看 找到 info replication 一项即可)

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0     #从机0
master_failover_state:no-failover
master_replid:bdc34060b44970af02e972e56e73f34da9244085
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

主机;查看主机ip

[root@localhost redis-6.2.6]# ip addr
......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:c2:15:cf brd ff:ff:ff:ff:ff:ff
    inet 192.168.217.11/24 brd 192.168.217.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::1e6f:d3ee:5554:1f34/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::ac8:77ad:9154:7983/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

从机1、2:配置

127.0.0.1:6379> slaveof 192.168.217.11 6397  #salveof 主机ip+端口号
23:04:18.568 * RDB memory usage when created 1.93 Mb
                                                                      21477:S 24 May 2022 23:04:18.568 # Done loading RDB, keys loaded: 0, keys expired: 0.
                                                                    21477:S 24 May 2022 23:04:18.568 * MASTER <-> REPLICA sync: Finished with success   #弹出success表示成功

此时主机会弹出以下提示,表示两个从机连接成功

18885:M 25 May 2022 06:28:14.820 * Synchronization with replica 192.168.217.13:6379 succeeded

18885:M 25 May 2022 07:04:18.440 * Synchronization with replica 192.168.217.12:6379 succeeded

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2    #已有2台设备接入
slave0:ip=192.168.217.13,port=6379,state=online,offset=3052,lag=1  #接入设备信息
slave1:ip=192.168.217.12,port=6379,state=online,offset=3052,lag=1
#接入设备信息
master_failover_state:no-failover
master_replid:5c1189728aa7e1e612349557c672442ebc51f84c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3052
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:3052
退出数据库  配置配置文件

[root@localhost redis-6.2.6]# vim redis.conf 	
# The requirepass is not compatable with aclfile option and the ACL LOAD
# command, these will cause requirepass to be ignored.
#
 requirepass 123456   #添加密码  123456
保存退出配置文件;



重启数据库
[root@localhost redis-6.2.6]# redis-cli shutdown
[root@localhost redis-6.2.6]# redis-server redis.conf &
[root@localhost redis-6.2.6]# redis-cli
认证
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> info replication  #再次查看从库,发现无从库接入



此处两种输入密码方法
方法1
127.0.0.1:6379> auth 123456
OK
方法2
[root@localhost redis-6.2.6]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

此时从库已经因 没有密码持续链接主库,进入死循环阶段,断开重新接入xshell
从1、2操作如下:
[root@localhost ~]# cd /usr/src/redis-6.2.6/
[root@localhost redis-6.2.6]# redis-server redis.conf &
[root@localhost redis-6.2.6]# redis-cli

127.0.0.1:6379> slaveof no one
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:4914ffdb86ab25c123b0d53debb41566eb212351
master_replid2:c48972dc8fde7294b537f89cd76dfb06bcdc132b
master_repl_offset:968
second_repl_offset:969
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:267
repl_backlog_histlen:70

[root@localhost redis-6.2.6]# vim redis.conf


 Master-Replica replication. Use replicaof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
#   +------------------+      +---------------+
#   |      Master      | ---> |    Replica    |
#   | (receive writes) |      |  (exact copy) |
#   +------------------+      +---------------+
#
# 1) Redis replication is asynchronous, but you can configure a master to
#    stop accepting writes if it appears to be not connected with at least
#    a given number of replicas.
# 2) Redis replicas are able to perform a partial resynchronization with the
#    master if the replication link is lost for a relatively small amount of
#    time. You may want to configure the replication backlog size (see the next
#    sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
#    network partition replicas automatically try to reconnect to masters
#    and resynchronize with them.
#
# replicaof 192.168.217.11 6379   #添加 主库的ip 及端口号

# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
#
# masterauth 123456   #添加主库密码

主库

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.217.13,port=6379,state=online,offset=322,lag=0
slave1:ip=192.168.217.12,port=6379,state=online,offset=322,lag=0
master_failover_state:no-failover
master_replid:c48972dc8fde7294b537f89cd76dfb06bcdc132b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:322
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:322

验证主从;
主库创建键

127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> slaveof no one
OK
127.0.0.1:6379> 

从库查看:
从库在创建文件需要输入密码

127.0.0.1:6379> auth 123456
ok

redis 主从复制_第1张图片

没有sentine的情况下,
主库宕机, 两从库需要输入
127.0.0.1:6379> slaveof no one
OK #停止主从复制
配置文件中,有主从相关配置的需修改 并且重新授权
slaveof 主机ip+端口号
slaveof 192.168.217.12 6379
剩余操作跟以上配置一样

关闭以上实验的 配置文件中的 ip+端口号和密码 包括主库
Sentine哨兵机制
从机哨兵用于监控主机,主机哨兵也用于监控主机。

主库 192.168.217.12 中
修改配置文件;

 [root@localhost redis-6.2.6]# ls
00-RELEASENOTES  COPYING   Makefile    runtest            sentinel.conf  utils
BUGS             deps      MANIFESTO   runtest-cluster    src
CONDUCT          dump.rdb  README.md   runtest-moduleapi  tests
CONTRIBUTING     INSTALL   redis.conf  runtest-sentinel   TLS.md
[root@localhost redis-6.2.6]# vim sentinel.conf 
# port <sentinel-port>
# The port that this sentinel instance will run on
port 26379   #sentinel端口号
# Note: master name should not include special characters or spaces.
# The valid charset is A-z 0-9 and the three characters ".-_".

sentinel monitor mymaster 192.168.217.12 6379 2  #mynaster  只是名字可修改   后跟主库的IP地址  +端口号  (2 相当于  有两台哨兵警报主机挂机  就开始 切换,  2这个数量 一般根据从机数量设置  一般大于一半)

......

# Default is 30 seconds.
sentinel down-after-milliseconds mymaster 10000  #10000毫秒   表示哨兵与主节点通信时间  1=1000毫秒
保存退出

开启哨兵
方法1
[root@localhost redis-6.2.6]# redis-server sentinel.conf --sentinal
方法2
[root@localhost redis-6.2.6]# redis-sentinel sentinel.conf &
[4] 74454

[root@localhost redis-6.2.6]# redis-cli -p 26379 
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.217.12:6379,slaves=2,sentinels=3   #显示主机ip及端口号   从机2台   哨兵3

从机配置哨兵 跟主机修改配置一样 ip+端口 为主机的ip 端口
当主机192.168.127.12 宕机
会立刻将主机自动切换到从机上 保障业务正常运行

cluster集群搭建
实验环境
系统版本:CentOS Linux release 7.6.1810 (Core)
redis版本:redis-6.2.6.tar
6台设备:
设备1:192.168.217.11
设备2:192.168.217.12
设备3:192.168.217.13
设备4:192.168.217.14
设备5:192.168.217.15
设备6:192.168.217.18
首先均关闭防火墙
systemctl stop frewalld
安装redis 6台设备均需安装
修改配置文件 6台设备均需配置
[root@localhost redis-6.2.6]# vim redis.conf

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT OUT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 0.0.0.0 -::1    #ip地址修改

......

################################ REDIS CLUSTER  ###############################

# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
#
 cluster-enabled yes    #去掉注释 

# Every cluster node has a cluster configuration file. This file is not

[root@localhost redis-6.2.6]# redis-cli shutdown
[root@localhost redis-6.2.6]# redis-server redis.conf &
[root@localhost redis-6.2.6]# redis-cli
127.0.0.1:6379> exit

设备1redis-cli --cluster create 192.168.217.11:6379 192.168.217.12:6379 192.168.217.13:6379 192.168.217.14:6379 192.168.217.15:6379 192.168.217.18:6379 --cluster-replicas 1

[root@localhost redis-6.2.6]# redis-cli --cluster create 192.168.217.11:6379 192.168.217.12:6379 192.168.217.13:6379 192.168.217.14:6379 192.168.217.15:6379 192.168.217.18:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.217.15:6379 to 192.168.217.11:6379
Adding replica 192.168.217.18:6379 to 192.168.217.12:6379
Adding replica 192.168.217.14:6379 to 192.168.217.13:6379  
#自动分配的三主三从   M为主  s为从
M: 0ad41309e24af8a769c7fc409b33da1a4d182b27 192.168.217.11:6379
   slots:[0-5460] (5461 slots) master
M: c50ac6825236563c127ce76b5a8be225c2d2ac9f 192.168.217.12:6379
   slots:[5461-10922] (5462 slots) master
M: 5162f16bcb01973d3660cca0af8e1feecd1139ae 192.168.217.13:6379
   slots:[10923-16383] (5461 slots) master
S: fed95b5da0c69bf6b0e3458e552552a18b94f84d 192.168.217.14:6379
   replicates 5162f16bcb01973d3660cca0af8e1feecd1139ae
S: 14a334b0979c66a0fc9b8fa78261354046aabdb3 192.168.217.15:6379
   replicates 0ad41309e24af8a769c7fc409b33da1a4d182b27
S: 2450bc85725dccd7b35073b9a60a8f4a354641f3 192.168.217.18:6379
   replicates c50ac6825236563c127ce76b5a8be225c2d2ac9f
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
82497:M 26 May 2022 05:56:27.460 # configEpoch set to 1 via CLUSTER SET-CONFIG-EPOCH
>>> Sending CLUSTER MEET messages to join the cluster
82497:M 26 May 2022 05:56:27.497 # IP address for this node updated to 192.168.217.11
Waiting for the cluster to join

82497:M 26 May 2022 05:56:28.504 * Replica 192.168.217.15:6379 asks for synchronization
82497:M 26 May 2022 05:56:28.504 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for '58386ac2ac3825af401e2fb454892b2c0857543b', my replication IDs are '64c75a73b7727e580e1da242a74d465a5d532889' and '0000000000000000000000000000000000000000')
82497:M 26 May 2022 05:56:28.504 * Replication backlog created, my new replication IDs are 'a5eab85eaafd513f6360bef7b49a0640234e41d0' and '0000000000000000000000000000000000000000'
82497:M 26 May 2022 05:56:28.504 * Starting BGSAVE for SYNC with target: disk
82497:M 26 May 2022 05:56:28.553 * Background saving started by pid 82625
82625:C 26 May 2022 05:56:28.554 * DB saved on disk
82625:C 26 May 2022 05:56:28.555 * RDB: 2 MB of memory used by copy-on-write
>>> Performing Cluster Check (using node 192.168.217.11:6379)
M: 0ad41309e24af8a769c7fc409b33da1a4d182b27 192.168.217.11:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: c50ac6825236563c127ce76b5a8be225c2d2ac9f 192.168.217.12:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 14a334b0979c66a0fc9b8fa78261354046aabdb3 192.168.217.15:6379
   slots: (0 slots) slave
   replicates 0ad41309e24af8a769c7fc409b33da1a4d182b27
S: fed95b5da0c69bf6b0e3458e552552a18b94f84d 192.168.217.14:6379
   slots: (0 slots) slave
   replicates 5162f16bcb01973d3660cca0af8e1feecd1139ae
M: 5162f16bcb01973d3660cca0af8e1feecd1139ae 192.168.217.13:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 2450bc85725dccd7b35073b9a60a8f4a354641f3 192.168.217.18:6379
   slots: (0 slots) slave
   replicates c50ac6825236563c127ce76b5a8be225c2d2ac9f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost redis-6.2.6]# 82497:M 26 May 2022 05:56:28.654 * Background saving terminated with success
82497:M 26 May 2022 05:56:28.654 * Synchronization with replica 192.168.217.15:6379 succeeded
82497:M 26 May 2022 05:56:32.494 # Cluster state changed: ok



查看端口
[root@localhost redis-6.2.6]# netstat -anput | grep 6379
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      82497/redis-server  
tcp        0      0 0.0.0.0:16379           0.0.0.0:*               LISTEN      82497/redis-server  
tcp        0      0 192.168.217.11:46687    192.168.217.13:16379    ESTABLISHED 82497/redis-server  
tcp        0      0 192.168.217.11:40273    192.168.217.12:16379    ESTABLISHED 82497/redis-server  
tcp        0      0 192.168.217.11:16379    192.168.217.18:37658    ESTABLISHED 82497/redis-server  
tcp        0      0 192.168.217.11:16379    192.168.217.12:33814    ESTABLISHED 82497/redis-server  
tcp        0      0 192.168.217.11:16379    192.168.217.13:36646    ESTABLISHED 82497/redis-server  
tcp        0      0 192.168.217.11:16379    192.168.217.14:34935    ESTABLISHED 82497/redis-server  
tcp        0      0 192.168.217.11:16379    192.168.217.15:40829    ESTABLISHED 82497/redis-server  
tcp        0      0 192.168.217.11:35484    192.168.217.18:16379    ESTABLISHED 82497/redis-server  
tcp        0      0 192.168.217.11:40828    192.168.217.15:16379    ESTABLISHED 82497/redis-server  
tcp        0      0 192.168.217.11:6379     192.168.217.15:35341    ESTABLISHED 82497/redis-server  
tcp        0      0 192.168.217.11:41179    192.168.217.14:16379    ESTABLISHED 82497/redis-server  
tcp6       0      0 ::1:6379                :::*                    LISTEN      82497/redis-server  
tcp6       0      0 ::1:16379               :::*                    LISTEN      82497/redis-server  

你可能感兴趣的:(redis,redis,数据库,缓存)