CentOS7 redis主从同步配置

主:10.116.186.9 6379 配置:4C16GB redis配置使用12G
从:10.116.186.10 6379 配置:4C16GB redis配置使用12G
OS:centos 7.6 min x86_64

1. 主从服务器安装redis

yum info redis
	Loaded plugins: fastestmirror
	Loading mirror speeds from cached hostfile
	Installed Packages
	Name        : redis
	Arch        : x86_64
	Version     : 3.2.6
	Release     : 1.eju.el7  # 我们自己从新打了一个rpm包,主要是为了企业内部统一安装目录,日志目录等
	Size        : 6.8 M
	Repo        : installed
	Summary     : eju redis rpm package
	URL         : redis.io
	License     : unknown
	Description : eju redis rpm package

yum -y install redis

rpm -qa |grep redis
	redis-3.2.6-1.eju.el7.x86_64

2. 主服务器配置

grep -v -e ^# -e ^$ redis.conf

bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 300
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis.pid
loglevel notice
logfile "/opt/app/redis/log/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /opt/app/redis/data
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass hN@b4MQhLjwD6@b#
maxmemory 12G
maxmemory-policy volatile-lru
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 512mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

3. 从服务器配置

grep -v -e ^# -e ^$ redis.conf

bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 300
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis.pid
loglevel notice
logfile "/opt/app/redis/log/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /opt/app/redis/data
slaveof 10.116.186.9 6379                            # 从服务器需要的配置
masterauth hN@b4MQhLjwD6@b#              # 从服务器需要的配置
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass hN@b4MQhLjwD6@b#
maxmemory 12G
maxmemory-policy volatile-lru
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 512mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

4. 启动服务

主/从服务器

systemctl start redis
systemctl enable redis

5. 主从验证

主服务器:10.116.186.9

redis-cli -a hN@b4MQhLjwD6@b#  info
...

# Replication
role:master             # 角色,主
connected_slaves:1      # 有一个从服务器
slave0:ip=10.116.186.10,port=6379,state=online,offset=2927,lag=1   # 从服务器信息
master_repl_offset:2927
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:2926


redis-cli -a hN@b4MQhLjwD6@b#
127.0.0.1:6379> set test hello
OK
127.0.0.1:6379> get test
"hello"

从服务器:10.116.186.10

redis-cli -a hN@b4MQhLjwD6@b#  info
...

# Replication
role:slave           # 角色,从
master_host:10.116.186.9 
master_port:6379
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:3011
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

redis-cli -a hN@b4MQhLjwD6@b#
127.0.0.1:6379> get test
"hello"
127.0.0.1:6379> del test          #从服务器配置为只读模式,删除key需要在主服务器
(error) READONLY You can't write against a read only slave.

你可能感兴趣的:(Linux,Databases)