Redis配置主从复制

时间:2021-08-23 00:19:16

                  Redis配置主从复制

                                      作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.操作环境介绍

1>.操作系统环境

[root@node101.yinzhengjie.org.cn ~]# cat /etc/redhat-release
CentOS Linux release 7.6. (Core)
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# uname -r
3.10.-.el7.x86_64
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# uname -m
x86_64
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# free -h
total used free shared buff/cache available
Mem: .7G 102M .3G 11M 292M .3G
Swap: .0G 0B .0G
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

2>.安装Redis(3台配置一样的虚拟机启动成功后,都需要安装Redis服务)

[root@node101.yinzhengjie.org.cn ~]# yum -y install epel-release
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch :- will be installed
--> Finished Dependency Resolution Dependencies Resolved ==============================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================
Installing:
epel-release noarch - extras k Transaction Summary
==============================================================================================================================================
Install Package Total download size: k
Installed size: k
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
epel-release--.noarch.rpm | kB ::
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : epel-release--.noarch /
Verifying : epel-release--.noarch / Installed:
epel-release.noarch :- Complete!
[root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# yum -y install epel-release

[root@node101.yinzhengjie.org.cn ~]# yum -y install epel-release

[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# yum info redis
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 5.8 kB ::
* base: mirrors.aliyun.com
* epel: mirrors.yun-idc.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
epel | 4.7 kB ::
(/): epel/x86_64/group_gz | kB ::
epel/x86_64/updateinfo FAILED
https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/repodata/27797f54681404f3261395d766df370206f7d92cd3e1551a698663a6317d5c5a-updateinfo.xml.bz2: [Errno 14] HTTPS Error 404 - Not Found
Trying other mirror.
To address this issue please refer to the below wiki article https://wiki.centos.org/yum-errors If above article doesn't help to resolve this issue please use https://bugs.centos.org/. (/): epel/x86_64/updateinfo | 1.0 MB ::
(/): epel/x86_64/primary_db | 6.6 MB ::
Available Packages
Name : redis
Arch : x86_64
Version : 3.2.
Release : .el7
Size : k
Repo : epel/x86_64
Summary : A persistent key-value database
URL : http://redis.io
License : BSD
Description : Redis is an advanced key-value store. It is often referred to as a data
: structure server since keys can contain strings, hashes, lists, sets and
: sorted sets.
:
: You can run atomic operations on these types, like appending to a string;
: incrementing the value in a hash; pushing to a list; computing set
: intersection, union and difference; or getting the member with highest
: ranking in a sorted set.
:
: In order to achieve its outstanding performance, Redis works with an
: in-memory dataset. Depending on your use case, you can persist it either
: by dumping the dataset to disk every once in a while, or by appending
: each command to a log.
:
: Redis also supports trivial-to-setup master-slave replication, with very
: fast non-blocking first synchronization, auto-reconnection on net split
: and so forth.
:
: Other features include Transactions, Pub/Sub, Lua scripting, Keys with a
: limited time-to-live, and configuration settings to make Redis behave like
: a cache.
:
: You can use Redis from most programming languages also. [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# yum info redis            #查看Redis安装包的相应信息

[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# yum -y install redis
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.yun-idc.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package redis.x86_64 :3.2.-.el7 will be installed
--> Processing Dependency: libjemalloc.so.()(64bit) for package: redis-3.2.-.el7.x86_64
--> Running transaction check
---> Package jemalloc.x86_64 :3.6.-.el7 will be installed
--> Finished Dependency Resolution Dependencies Resolved ==============================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================
Installing:
redis x86_64 3.2.-.el7 epel k
Installing for dependencies:
jemalloc x86_64 3.6.-.el7 epel k Transaction Summary
==============================================================================================================================================
Install Package (+ Dependent package) Total download size: k
Installed size: 1.7 M
Downloading packages:
warning: /var/cache/yum/x86_64//epel/packages/jemalloc-3.6.-.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Public key for jemalloc-3.6.-.el7.x86_64.rpm is not installed
(/): jemalloc-3.6.-.el7.x86_64.rpm | kB ::
(/): redis-3.2.-.el7.x86_64.rpm | kB ::
----------------------------------------------------------------------------------------------------------------------------------------------
Total 3.4 MB/s | kB ::
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
Userid : "Fedora EPEL (7) <epel@fedoraproject.org>"
Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
Package : epel-release--.noarch (@extras)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : jemalloc-3.6.-.el7.x86_64 /
Installing : redis-3.2.-.el7.x86_64 /
Verifying : redis-3.2.-.el7.x86_64 /
Verifying : jemalloc-3.6.-.el7.x86_64 / Installed:
redis.x86_64 :3.2.-.el7 Dependency Installed:
jemalloc.x86_64 :3.6.-.el7 Complete!
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# yum -y install redis        #安装Redis服务

3>.修改时区

[root@node102.yinzhengjie.org.cn ~]# date -R
Thu, Apr :: -
[root@node102.yinzhengjie.org.cn ~]# rm -f /etc/localtime
[root@node102.yinzhengjie.org.cn ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@node102.yinzhengjie.org.cn ~]# date -R
Fri, Apr :: +
[root@node102.yinzhengjie.org.cn ~]#

二.基于修改配置文件的方式实现Redis的主从复制

1>.配置主机说明

  master 节点:node101.yinzhengjie.org.cn

  slave    节点:node102.yinzhengjie.org.cn

2>.查看master节点的配置文件

[root@node101.yinzhengjie.org.cn ~]# egrep -v "^$|^#" /etc/redis.conf
bind node101.yinzhengjie.org.cn                    #指定当前的主机
protected-mode yes
port                                  #默认的Redis端口号
tcp-backlog
unixsocket /tmp/redis.sock
timeout
tcp-keepalive
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
databases
save
save
save
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay
repl-disable-tcp-nodelay no
slave-priority
requirepass yinzhengjie
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit
slowlog-log-slower-than
slowlog-max-len
latency-monitor-threshold
notify-keyspace-events ""
hash-max-ziplist-entries
hash-max-ziplist-value
list-max-ziplist-size -
list-compress-depth
set-max-intset-entries
zset-max-ziplist-entries
zset-max-ziplist-value
hll-sparse-max-bytes
activerehashing yes
client-output-buffer-limit normal
client-output-buffer-limit slave 256mb 64mb
client-output-buffer-limit pubsub 32mb 8mb
hz
aof-rewrite-incremental-fsync yes
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# egrep -v "^$|^#" /etc/redis.conf

3>.查看slave节点的配置文件

[root@node102.yinzhengjie.org.cn ~]# egrep -v "^$|^#" /etc/redis.conf
bind node102.yinzhengjie.org.cn                #绑定地址
protected-mode yes
port
tcp-backlog
timeout
tcp-keepalive
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
databases
save
save
save
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
slaveof node101.yinzhengjie.org.cn           #指定master地址
masterauth yinzhengjie                    #指定连接master是需要认证的字符串!
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay
repl-disable-tcp-nodelay no
slave-priority
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit
slowlog-log-slower-than
slowlog-max-len
latency-monitor-threshold
notify-keyspace-events ""
hash-max-ziplist-entries
hash-max-ziplist-value
list-max-ziplist-size -
list-compress-depth
set-max-intset-entries
zset-max-ziplist-entries
zset-max-ziplist-value
hll-sparse-max-bytes
activerehashing yes
client-output-buffer-limit normal
client-output-buffer-limit slave 256mb 64mb
client-output-buffer-limit pubsub 32mb 8mb
hz
aof-rewrite-incremental-fsync yes
[root@node102.yinzhengjie.org.cn ~]#

[root@node102.yinzhengjie.org.cn ~]# egrep -v "^$|^#" /etc/redis.conf

4>.启动master节点和slave节点

[root@node101.yinzhengjie.org.cn ~]# systemctl start redis
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# systemctl status redis
● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/redis.service.d
└─limit.conf
Active: active (running) since Sat -- :: CST; 1h 13min ago
Main PID: (redis-server)
CGroup: /system.slice/redis.service
└─ /usr/bin/redis-server node101.yinzhengjie.org.cn: Apr :: node101.yinzhengjie.org.cn systemd[]: Starting Redis persistent key-value database...
Apr :: node101.yinzhengjie.org.cn systemd[]: Started Redis persistent key-value database.
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# systemctl start redis

[root@node102.yinzhengjie.org.cn ~]# systemctl start redis
[root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]# systemctl status redis
● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/redis.service.d
└─limit.conf
Active: active (running) since Sat -- :: CST; 4min 43s ago
Main PID: (redis-server)
CGroup: /system.slice/redis.service
└─ /usr/bin/redis-server 127.0.0.1: Apr :: node102.yinzhengjie.org.cn systemd[]: Starting Redis persistent key-value database...
Apr :: node102.yinzhengjie.org.cn systemd[]: Started Redis persistent key-value database.
[root@node102.yinzhengjie.org.cn ~]#

[root@node102.yinzhengjie.org.cn ~]# systemctl start redis

5>.验证是否复制实现Redis主从同步

[root@node101.yinzhengjie.org.cn ~]# redis-cli -h node101.yinzhengjie.org.cn -a yinzhengjie
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:> INFO
# Server                                      #服务器端的相应信息
redis_version:3.2.
redis_git_sha1:
redis_git_dirty:
redis_build_id:7897e7d0e13773f
redis_mode:standalone
os:Linux 3.10.-.el7.x86_64 x86_64
arch_bits:
multiplexing_api:epoll
gcc_version:4.8.
process_id:
run_id:514f95dd0a54449ec221b662da02caa65f74353e
tcp_port:
uptime_in_seconds:
uptime_in_days:
hz:
lru_clock:
executable:/usr/bin/redis-server
config_file:/etc/redis.conf # Clients                                #客户单的相应详细
connected_clients:                          #表示当前连接进来的客户端个数
client_longest_output_list:
client_biggest_input_buf:
blocked_clients: # Memory                                 #当前服务器的内存分配和使用状况
used_memory:
used_memory_human:1.80M
used_memory_rss:
used_memory_rss_human:2.78M
used_memory_peak:
used_memory_peak_human:1.80M
total_system_memory:
total_system_memory_human:.68G
used_memory_lua:
used_memory_lua_human:.00K
maxmemory:
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:1.55
mem_allocator:jemalloc-3.6. # Persistence              ·                      #当前启动的持久化是哪一种,以及是如何进行持久化的
loading:
rdb_changes_since_last_save:
rdb_bgsave_in_progress:
rdb_last_save_time:
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:
rdb_current_bgsave_time_sec:-
aof_enabled:
aof_rewrite_in_progress:
aof_rewrite_scheduled:
aof_last_rewrite_time_sec:-
aof_current_rewrite_time_sec:-
aof_last_bgrewrite_status:ok
aof_last_write_status:ok # Stats                                          #统计数据
total_connections_received:
total_commands_processed:
instantaneous_ops_per_sec:
total_net_input_bytes:
total_net_output_bytes:
instantaneous_input_kbps:0.04
instantaneous_output_kbps:0.00
rejected_connections:
sync_full:
sync_partial_ok:
sync_partial_err:
expired_keys:
evicted_keys:
keyspace_hits:
keyspace_misses:
pubsub_channels:
pubsub_patterns:
latest_fork_usec:
migrate_cached_sockets: # Replication                                    #记录主从复制中当前的信息
role:master                                      #当前节点的角色为master
connected_slaves:                                 #从节点连接进来的个数
slave0:ip=172.30.1.102,port=,state=online,offset=,lag=      #记录第一个从节点的相应信息
master_repl_offset:
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen: # CPU                       #查看CPU相关信息
used_cpu_sys:3.71
used_cpu_user:1.10
used_cpu_sys_children:0.00
used_cpu_user_children:0.00 # Cluster                    #查看Cluster信息,如果没有穷Cluster功能的话,那么其值默认为0.
cluster_enabled: # Keyspace                    #当前服务器键使用的空间
db0:keys=,expires=,avg_ttl=        #表示在第0个数据库这种,使用了9个KEYS。
node101.yinzhengjie.org.cn:>

[root@node101.yinzhengjie.org.cn ~]# redis-cli -h node101.yinzhengjie.org.cn -a yinzhengjie

[root@node101.yinzhengjie.org.cn ~]# redis-cli -h node101.yinzhengjie.org.cn -a yinzhengjie
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:> KEYS *
) "age"
) "count"
) "colors2"
) "weekdays"
) "color"
) "name"
) "liangshan"
) "stu1"
) "colors1"
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:> exit
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# redis-cli -h node101.yinzhengjie.org.cn -a yinzhengjie      #查看master相应信息

[root@node102.yinzhengjie.org.cn ~]# redis-cli -h node102.yinzhengjie.org.cn
node102.yinzhengjie.org.cn:>
node102.yinzhengjie.org.cn:> KEYS *
) "weekdays"
) "count"
) "colors2"
) "colors1"
) "age"
) "liangshan"
) "color"
) "stu1"
) "name"
node102.yinzhengjie.org.cn:>
node102.yinzhengjie.org.cn:> exit
[root@node102.yinzhengjie.org.cn ~]#

[root@node102.yinzhengjie.org.cn ~]# redis-cli -h node102.yinzhengjie.org.cn                #查看node102.yinzhengjie.org.cn信息是否和master节点配置一致

node101.yinzhengjie.org.cn:> INFO Replication
# Replication
role:master
connected_slaves:
slave0:ip=172.30.1.102,port=,state=online,offset=,lag=
slave1:ip=172.30.1.103,port=,state=online,offset=,lag=
master_repl_offset:
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>

node101.yinzhengjie.org.cn:6379> INFO Replication

三.基于CONFIG命令的方式实现Redis的主从复制

1>.配置主机说明

  master 节点:node101.yinzhengjie.org.cn

  slave    节点:node103.yinzhengjie.org.cn

2>.Server相关的命令

node101.yinzhengjie.org.cn:> HELP CLIENT GETNAME

  CLIENT GETNAME -
summary: Get the current connection name
since: 2.6.
group: server node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>

node101.yinzhengjie.org.cn:6379> HELP CLIENT GETNAME

node101.yinzhengjie.org.cn:> HELP CLIENT KILL

  CLIENT KILL [ip:port] [ID client-id] [TYPE normal|master|slave|pubsub] [ADDR ip:port] [SKIPME yes/no]
summary: Kill the connection of a client
since: 2.4.
group: server node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>

node101.yinzhengjie.org.cn:6379> HELP CLIENT KILL

node101.yinzhengjie.org.cn:> HELP CLIENT LIST

  CLIENT LIST -
summary: Get the list of client connections
since: 2.4.
group: server node101.yinzhengjie.org.cn:>

node101.yinzhengjie.org.cn:6379> HELP CLIENT LIST

node101.yinzhengjie.org.cn:> HELP CLIENT REPLY

  CLIENT REPLY ON|OFF|SKIP
summary: Instruct the server whether to reply to commands
since: 3.2
group: server node101.yinzhengjie.org.cn:>

node101.yinzhengjie.org.cn:6379> HELP CLIENT REPLY

node101.yinzhengjie.org.cn:> HELP CLIENT SETNAME

  CLIENT SETNAME connection-name
summary: Set the current connection name
since: 2.6.
group: server node101.yinzhengjie.org.cn:>

node101.yinzhengjie.org.cn:6379> HELP CLIENT SETNAME

node101.yinzhengjie.org.cn:> HELP  SHUTDOWN

  SHUTDOWN [NOSAVE|SAVE]
summary: Synchronously save the dataset to disk and then shut down the server
since: 1.0.
group: server node101.yinzhengjie.org.cn:>

node101.yinzhengjie.org.cn:6379> HELP SHUTDOWN

node101.yinzhengjie.org.cn:> CLIENT LIST
id= addr=172.30.1.102: fd= name= age= idle= flags=S db= sub= psub= multi=- qbuf= qbuf-free= obl= oll= omem= events=r cmd=replconf
id= addr=172.30.1.101: fd= name= age= idle= flags=N db= sub= psub= multi=- qbuf= qbuf-free= obl= oll= omem= events=r cmd=client
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>

node101.yinzhengjie.org.cn:6379> CLIENT LIST

node101.yinzhengjie.org.cn:> INFO stats
# Stats
total_connections_received:
total_commands_processed:
instantaneous_ops_per_sec:
total_net_input_bytes:
total_net_output_bytes:
instantaneous_input_kbps:0.07
instantaneous_output_kbps:0.00
rejected_connections:
sync_full:
sync_partial_ok:
sync_partial_err:
expired_keys:
evicted_keys:
keyspace_hits:
keyspace_misses:
pubsub_channels:
pubsub_patterns:
latest_fork_usec:
migrate_cached_sockets:
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:> CONFIG RESETSTAT
OK
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:> INFO stats
# Stats
total_connections_received:
total_commands_processed:
instantaneous_ops_per_sec:
total_net_input_bytes:
total_net_output_bytes:
instantaneous_input_kbps:0.07
instantaneous_output_kbps:0.00
rejected_connections:
sync_full:
sync_partial_ok:
sync_partial_err:
expired_keys:
evicted_keys:
keyspace_hits:
keyspace_misses:
pubsub_channels:
pubsub_patterns:
latest_fork_usec:
migrate_cached_sockets:
node101.yinzhengjie.org.cn:>

node101.yinzhengjie.org.cn:6379> CONFIG RESETSTAT

[root@node103.yinzhengjie.org.cn ~]# redis-cli -h node103.yinzhengjie.org.cn
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:> CONFIG SET requirepass yinzhengjie        #配置后,马上就生效啦,因此下面执行的所有相关命令均无效!需要验证时方能使用
OK
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:> CONFIG REWRITE                    #由于上面设置了认证的字符串,当前终端目前还没有进行认证,因此不可执行其他操作
(error) NOAUTH Authentication required.
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:> AUTH yinzhengjie                    #在命令行中进行认证,认证成功后,为我们就可以执行相应的操作啦~
OK
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:> CONFIG REWRITE
OK
node103.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:> HELP CONFIG GET CONFIG GET parameter
summary: Get the value of a configuration parameter
since: 2.0.
group: server node101.yinzhengjie.org.cn:>

node101.yinzhengjie.org.cn:6379> HELP CONFIG GET

node101.yinzhengjie.org.cn:> HELP CONFIG RESETSTAT

  CONFIG RESETSTAT -
summary: Reset the stats returned by INFO
since: 2.0.
group: server node101.yinzhengjie.org.cn:>

node101.yinzhengjie.org.cn:6379> HELP CONFIG RESETSTAT

node101.yinzhengjie.org.cn:> HELP CONFIG REWRITE

  CONFIG REWRITE -
summary: Rewrite the configuration file with the in memory configuration
since: 2.8.
group: server node101.yinzhengjie.org.cn:>

node101.yinzhengjie.org.cn:6379> HELP CONFIG REWRITE

node101.yinzhengjie.org.cn:> HELP CONFIG SET

  CONFIG SET parameter value
summary: Set a configuration parameter to the given value
since: 2.0.
group: server node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>

node101.yinzhengjie.org.cn:6379> HELP CONFIG SET

3>.使用CONFIG命令配置Redis主从同步(node103.yinzhengjie.org.cn)

[root@node103.yinzhengjie.org.cn ~]# egrep -v "^$|^#" /etc/redis.conf
bind node103.yinzhengjie.org.cn
protected-mode yes
port
tcp-backlog
timeout
tcp-keepalive
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
databases
save
save
save
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay
repl-disable-tcp-nodelay no
slave-priority
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit
slowlog-log-slower-than
slowlog-max-len
latency-monitor-threshold
notify-keyspace-events ""
hash-max-ziplist-entries
hash-max-ziplist-value
list-max-ziplist-size -
list-compress-depth
set-max-intset-entries
zset-max-ziplist-entries
zset-max-ziplist-value
hll-sparse-max-bytes
activerehashing yes
client-output-buffer-limit normal
client-output-buffer-limit slave 256mb 64mb
client-output-buffer-limit pubsub 32mb 8mb
hz
aof-rewrite-incremental-fsync yes
[root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]#

[root@node103.yinzhengjie.org.cn ~]# egrep -v "^$|^#" /etc/redis.conf          #修改bind地址即可,其他可以暂时不做修改

[root@node103.yinzhengjie.org.cn ~]# systemctl start redis
[root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# systemctl status redis
● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/redis.service.d
└─limit.conf
Active: active (running) since Sat -- :: PDT; 2s ago
Main PID: (redis-server)
CGroup: /system.slice/redis.service
└─ /usr/bin/redis-server node103.yinzhengjie.org.cn: Apr :: node103.yinzhengjie.org.cn systemd[]: Starting Redis persistent key-value database...
Apr :: node103.yinzhengjie.org.cn systemd[]: Started Redis persistent key-value database.
[root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]#

[root@node103.yinzhengjie.org.cn ~]# systemctl start redis                  #启动集群

[root@node103.yinzhengjie.org.cn ~]# redis-cli -h node103.yinzhengjie.org.cn
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:> KEYS *                            #启动集群后,我们还没有网当前节点创建测试数据,因此我们查看KEYS时发现是空的。
(empty list or set)
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:> SLAVEOF node101.yinzhengjie.org.cn 6379        #指定master的地址
OK
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:> CONFIG SET masterauth yinzhengjie            #指定master的验证信息
OK
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:> KEYS *                            #再次查看KEYS,发现是有数据的,原因是我们同步数据成功啦!
) "age"
) "colors1"
) "stu1"
) "name"
) "color"
) "colors2"
) "count"
) "liangshan"
) "weekdays"
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:> CONFIG REWRITE                      #上面的配置虽然说实现了主从复制,但是确实是临时生效的,因为重启后,配置就丢失了,想要永久保存配置,那么我们可以使用该命令将配置写入到Redis的配置文件中。
OK
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:> exit
[root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# tail - /etc/redis.conf               #我们查看Redis的最后三行,发现我们成功的将数据写入啦!
# Generated by CONFIG REWRITE
slaveof node101.yinzhengjie.org.cn
masterauth "yinzhengjie"
[root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]#

4>.在master节点上查看相应的信息

node101.yinzhengjie.org.cn:> INFO Replication
# Replication
role:master
connected_slaves:
slave0:ip=172.30.1.102,port=,state=online,offset=,lag=
slave1:ip=172.30.1.103,port=,state=online,offset=,lag=
master_repl_offset:
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>

node101.yinzhengjie.org.cn:6379> INFO Replication

node101.yinzhengjie.org.cn:> INFO stats
# Stats
total_connections_received:
total_commands_processed:
instantaneous_ops_per_sec:
total_net_input_bytes:
total_net_output_bytes:
instantaneous_input_kbps:0.07
instantaneous_output_kbps:0.00
rejected_connections:
sync_full:
sync_partial_ok:
sync_partial_err:
expired_keys:
evicted_keys:
keyspace_hits:
keyspace_misses:
pubsub_channels:
pubsub_patterns:
latest_fork_usec:
migrate_cached_sockets:
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>

node101.yinzhengjie.org.cn:6379> INFO stats

四.主从复制相关参数说明

1>.slaveof
 
 通过执行 SLAVEOF host port 命令,可以将当前服务器转变为指定服务器的从属服务器(slave server)。


2>.masterauth
 
 用于指定slaveof主机连接时的认证密码。


3>.slave-serve-stale-data yes

当一个slave失去和master的连接,或者同步正在进行中,slave的行为可以有两种表现:
  
  >.如果 slave-serve-stale-data 设置为 "yes" (默认值),slave会继续响应客户端请求,可能是正常数据,或者是过时了的数据,也可能是还没获得值的空数据。
  
  >. 如果 slave-serve-stale-data 设置为 "no",slave会回复"正在从master同步(SYNC with master in progress)"来处理各种请求,除了 INFO 和 SLAVEOF 命令。

4>.slave-read-only yes
  
你可以配置salve实例是否接受写操作。可写的slave实例可能对存储临时数据比较有用(因为写入salve的数据在同master同步之后将很容易被删除)。

5>.repl-diskless-sync no

  其有三个值,分别为:no,Disk-backed,Diskless
  
  新的从节点或某较长时间未能与主节点进行同步的从节点重新与主节点通信,需要做“full synchronization”,此时其同步方式有两种style:
    Disk-backend:
        主节点创建快照文件与磁盘中,而后将其发送给从节点。
    Diskless:
        主节点占新创建快照后直接通过网络套接字文件发送给从节点,为了实现并行复制,通常需要在复制启动前延迟一个时间段。

6>.repl-diskless-sync-delay 5

  无磁盘diskless方式在进行数据传递之前会有一个时间的延迟,以便slave端能够进行到待传送的目标队列中,这个时间默认是5秒。


7>.repl-ping-slave-period 10

  主从关系建立后,redis主会以repl-ping-slave-period为周期,向redis从发送PING指令。  


8>.repl-timeout 60

  slave会每隔repl-ping-slave-period(默认10秒)ping一次master,如果超过repl-timeout(默认 60秒)都没有收到响应,就会认为Master挂了。


9>.repl-diskable-tcp-nodelay no

  如果选择“ yes” ,Redis将使用一个较小的数字 TCP数据包和更少的带宽将数据发送到 slave,但是这可能导致数据发送到 slave端会有延迟 ,如果是 Linux kernel的默认配置,会达到 40毫秒 ,如果选择 "no",则发送数据到 slave端的延迟会降低,但将使用更多的带宽用于复制 。


10>.repl-backlog-size lmb

  设置backlog的大小,backlog是一个缓冲区,在slave端失连时存放要同步到slave的数据,因此当一个slave要重连时,经常是不需要完全同步的,执行局部同步就足够了。backlog设置的越大,slave可以失连的时间就越长。


11>.slave-priority 100
 
 复制集群中,主节点故障时,sentinel应用场景中的主节点选举时使用优先级;数字越小优先级越高,但0表示不参与选举。

12>.min-slaves-to-write 3
  主节点仅允许其能够通信的从节点数量大于等于此处的值时接收写操作。

13>.min-slaves-max-lag 10
  从节点延迟时长超出此处指定的时长时,主节点会拒绝写入操作。