Redis配置主从复制
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
从节点延迟时长超出此处指定的时长时,主节点会拒绝写入操作。
Redis配置主从复制的更多相关文章
- redis的主从复制和哨兵模式
Redis主从复制是什么? 行话:也就是我们所说的主从复制,主机数据更新后根据配置和策略, 自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主 Redis主从复制 ...
- redis的主从复制配置
redis的主从复制配置 一. 原理 Redis的主从复制功能非常强大,一个master可以拥有多个slave,而一个slave又可以拥有多个slave,如此下去,形成了强大的多级服务器集群架 ...
- 实现Redis的主从复制配置
实现Redis的主从复制配置比较简单,而且容易明白. 下图是要配置的主从复制结构图: 1.说明 Redis主从复制中一个主服务可以有多个从服务,一个从服务可以有多个从服务. 配置比较简单,只需要更改r ...
- contos7下安装redis&redis的主从复制的配置&redis 哨兵(sentinel)
一.centos7下安装redis 1.解压 redis-5.0.5.tar.gz 压缩文件 解压命令为: .tar.gz -C redis 解压后进入 redis 工作目录,进入 redis-5.0 ...
- redis配置详解
##redis配置详解 # Redis configuration file example. # # Note that in order to read the configuration fil ...
- Redis总结(三)Redis 的主从复制
接着上一篇,前面两篇我总结了<Redis总结(一)Redis安装>和<Redis总结(二)C#中如何使用redis> 所以这一篇,会讲讲Redis 的主从复制以及C#中如何调用 ...
- redis的主从复制部署和使用
reids一种key-value的缓存数据库目前非常流行的被使用在很多场景,比如在数据库读写遇到瓶颈时缓存且读写分离会大大提升这块的性能,下面我就说说redis的主从复制 首先需要启动多个redis实 ...
- redis实现主从复制-单机测试
一.redis实现主从复制-单机测试1.安装redis tar -zxvf redis-2.8.4.tar.gzcd redis-2.8.4make && make install2. ...
- 8. redis的主从复制和sentinal
一. redis主从复制(读写分离) redis的主从复制分为两类节点:1个master和多个slave,master进行读写操作,slav进行只读操作 启动步骤: 主节点照常启动,slave节点启动 ...
随机推荐
- XPath Helper的安装与使用
摘要 : XPath Helper可以支持在网页点击元素生成xpath,整个抓取使用了xpath.正则表达式.消息中间件.多线程调度框架的chrome插件. xpath:是一门XML和HTML文档中查 ...
- zabbix监控Oracle
可监控项 使用zabbix监控oracle数据库需要借助第三方的插件,目前使用较多的是orabbix.目前维护到了1.2.3版本.关于oracle自带的监控项目有以下几个: DB Version (i ...
- 记录基于VMware虚拟机, Linux7.2下外部主机访问配置
systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall开机启动
- 爬虫系列二(数据清洗--->xpath解析数据)
一 xpath介绍 XPath 是一门在 XML 文档中查找信息的语言.XPath 用于在 XML 文档中通过元素和属性进行导航. XPath 使用路径表达式在 XML 文档中进行导航 XPath 包 ...
- Html 改变原有标签属性
内容简要: 当标签内内容 达到某以条件的时候改变当前标签属性 例如原标签为<tr> 当tr内的值符合某一条件时把<tr>变成<a>标签 例:当订单状体编程已支付的时 ...
- iOS开发基础-KVC简单介绍
一.KVC介绍 键值编码(Key Value Coding,KVC):使开发者不必进行任何操作就可以进行属性的动态读写. KVC操作方法由 NSKeyValueCoding 协议提供,而 NSObje ...
- xshell中进入PLSQL命令不能使用方向键和退格键的做法(输入后显示乱码)
解决输入退格键为乱码的情况 输入时可以ctrl+backspace进行强制退格,或者使用下面一种方法: 在xshell的连接属性中配置,如下图红圈部分: 彻底解决方向键和退格键的一种办法(未亲测) ...
- lr12 websocket
loadrunner12以上版本支持websocket,在http/html协议录制时可以直接录制websocket相关内容信息. 网上找的一个测试websocket网址:http://www.blu ...
- Tampermonkey-让百度云下载飞起来
1. 简介Tampermonkey是一款免费的浏览器扩展程序. 我们这里用于谷歌浏览器,目的是为了让百度云里面的文件以满速下载,节约金钱. 2. 安装安装Lantern蓝灯或者其他的FQ工具. 打开F ...
- python 装饰器练习题
1.写出完整的装饰器(不用开了带参装饰器,就是普通装饰器)语法 2.有一个计算两个数和的方法,为其添加一个确保两个参数都是int或float类型的装饰器,保证运算不会抛异常 3.有一个一次性录入人名并 ...