
Redis主从部署
1.安装
# 指定版本 [root@tike ~]# wget http://download.redis.io/releases/redis-3.2.12.tar.gz [root@tike ~]# tar -xzvf redis-3.2.12.tar.gz [root@tike ~]# mv redis-3.2.12 /usr/local/ [root@tike ~]# cd /usr/local/redis-3.2.12 [root@tike ~]# make [root@tike ~]# make install prefix=/usr/local/redis-3.2.12 # 默认5.0.3 [root@tike ~]# yum -y install redis [root@tike ~]# systemctl status redis
2.配置文件
Master
bind 0.0.0.0 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile /var/log/redis/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 /var/lib/redis slave-serve-stale-data yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 rename-command FLUSHALL "TIKR_RMRB_FLUSHALL" rename-command FLUSHDB "TIKR_RMRB_FLUSHDB" rename-command CONFIG "TIKR_RMRB_CONFIG" maxmemory 16G 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 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 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 yesSlave
bind 0.0.0.0 protected-mode yes port 6379 slaveof 172.16.18.112 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile /var/log/redis/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 /var/lib/redis 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 rename-command FLUSHALL "TIKR_RMRB_FLUSHALL" rename-command FLUSHDB "TIKR_RMRB_FLUSHDB" rename-command CONFIG "TIKR_RMRB_CONFIG" maxmemory 8G 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 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 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.验证
[root@tike ~]# redis-cli 127.0.0.1:6379> info replication # Replication role:master connected_slaves:1 slave0:ip=172.16.18.131,port=6379,state=online,offset=70,lag=0 master_replid:bfc1c49774fe93a72eb212b49f56cc7c8ba4a410 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:70 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:70 [root@tike ~]# redis-cli 127.0.0.1:6379> info replication # Replication role:slave master_host:172.16.18.112 master_port:6379 master_link_status:up master_last_io_seconds_ago:5 master_sync_in_progress:0 slave_repl_offset:168 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:bfc1c49774fe93a72eb212b49f56cc7c8ba4a410 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:168 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:168
4.配置密码认证
进入配置文件vi redis.conf:
- /slaveof 回车搜索slaveof
在他下一行写上slaveof ip 端口号6379 如:slaveof 192.168.1.224 6379
- 如果主机设置了密码则:查找/masterauth 回车搜索masterauth 的下一行
masterauth 主机密码 如:masterauth beijing