Redis的主从配置相对于MySQL来说确实是非常简单,不过我们这里还是简单总结一下,仅供参考和总结,如有不足之处,敬请指正。
Redis的编译安装
# wget http://download.redis.io/releases/redis-6.0.8.tar.gz
# tar -xzvf redis-6.0.8.tar.gz
# cd redis-6.0.8
# make -j 4
# make PREFIX=/usr/local/redis install
如下所示,编译安装后,你可以看到下面目录和文件
创建下面目录,用来存放日志文件、配置文件(redis.conf)、pidfile文件,数据库dump.rdb文件
# mkdir -p /usr/local/redis/data
# mkdir -p /usr/local/redis/log
# mkdir -p /usr/local/redis/run
# mkdir -p /usr/local/redis/conf
# cp /tmp/redis-6.0.8/redis.conf /usr/local/redis/conf
修改部分参数,根据实际情况修改。大部分参数选择默认参数即可。
bind 127.0.0.1 192.168.27.115
daemonize yes
port 6666
logfile "/usr/local/redis/log/redis.log"
dir /usr/local/redis/data
pidfile /usr/local/redis/redis_6666.pid 这里指定pidfile跟监听端口一致,例如,监听端口为6666,那么pidfile设置为redis_6666.pid
protected-mode no
requirepass 75612a867ceeb452924c98778caa959c37120bfff6582aaad48298acbf320b28
查看redis.conf的配置参数
# grep ‘^[a-Z]‘ /usr/local/redis/conf/redis.conf
bind 127.0.0.1 192.168.27.115
protected-mode no
port 6666
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /usr/local/redis/redis_6666.pid
loglevel notice
logfile "/usr/local/redis/log/redis.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir /usr/local/redis/data
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass 75612a867ceeb452924c98778caa959c37120bfff6582aaad48298acbf320b28
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
oom-score-adj no
oom-score-adj-values 0 200 800
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble 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-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
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
创建普通用户
# groupadd redis
# useradd redis -g redis -s /sbin/nologin
修改目录的Owner
# chown -R redis:redis /usr/local/redis/
# ls -lrt /usr/local/redis/
限制redis文件目录访问权限
设置redis的主目录权限为700;
# chmod -R 700 /usr/local/redis/
Redis主从的配置
关于Redis的主服务器的配置如上所示,而Redis的从服务器配置也差不多,可以将/usr/local/redis/目录拷贝到从服务器去,也可以直接在从服务器按照上面步骤安装即可,当然也要创建redis用户、修改目录的owner、限制redis文件夹权限等等。
Redis从服务器的redis.conf还需要额外增加两个参数,如下所示,如果Redis主服务器没有配置requirepass,那么从服务器无需配置masterauth参数
slaveof 192.168.27.115 6666
masterauth 75612a867ceeb452924c98778caa959c37120bfff6582aaad48298acbf320b28
Redis的主从同步很简单,只需要启动Slave实例,会自动将主节点已有的数据同步到从节点。另外,在主节点写入数据也会自动同步到从节点,因此从节点不需要做数据还原的步骤。另外,slave是不允许写入的,在加入了slaveof 之后,实例就成为了只读的。
防火墙配置
# firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="10.20.57.24" port protocol="tcp" port="6666" accept"
# systemctl restart firewalld.service
启动Redis的主节点
sudo -u redis /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
启动Redis的从节点
sudo -u redis /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
Redis主从测试同步
[root@localhost ~]# /usr/local/redis/bin/redis-cli -h 192.168.27.115 -p 6666
192.168.27.115:6666> auth 75612a867ceeb452924c98778caa959c37120bfff6582aaad48298acbf320b28
OK
192.168.27.115:6666> set test1 "it‘s ok"
OK
192.168.27.115:6666> get test1
"it‘s ok
[root@KerryDB ~]# /usr/local/redis/bin/redis-cli -h 10.20.57.24 -p 6666
10.20.57.24:6666> get test1
"it‘s ok"
192.168.27.115:6666> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.20.57.24,port=6666,state=online,offset=2526,lag=1
master_replid:b30f19ff9f5e9c852636b07248f1e3b3fb8ea71b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2526
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2526
127.0.0.1:6666> info replication
# Replication
role:slave
master_host:192.168.27.115
master_port:6666
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:557
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b30f19ff9f5e9c852636b07248f1e3b3fb8ea71b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:557
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:557
正常情况下,主从同步正常,但是也会由于配置原因,导致主从同步异常的情况,需要详细分析日志找出主从同步的原因,当然也有主从不一致等问题,后续篇幅再做介绍。
参考资料:
http://mdba.cn/2013/10/22/redis%e5%ae%89%e8%a3%85%e4%b8%8e%e4%b8%bb%e4%bb%8e%e9%85%8d%e7%bd%ae/