Replication
To configure basic Redis replication is trivial: just add the following line to the replica configuration file:
配置基本 Redis 复制非常简单: 只需在副本配置文件中添加以下代码行:
replicaof 192.168.1.1 6379
安装Redis-4
搭建主从
配置redis.conf
[root@localhost bin]# vim redis.conf
# 追加如下
#4.0版本配置还是`slaveof <masterip> <masterport>`
#masterip masterport
slaveof 192.168.0.2 6379
启动slave
#从节点追加配置slaveof后启动
[root@localhost bin]# ./redis-server redis.conf
#查看启动日志
[root@localhost bin]# tail -f redis.log
15220:S 28 Feb 17:26:10.505 * Connecting to MASTER 192.168.0.2:6379
15220:S 28 Feb 17:26:10.506 * MASTER <-> SLAVE sync started
15220:S 28 Feb 17:26:10.506 * Non blocking connect for SYNC fired the event.
15220:S 28 Feb 17:26:10.506 * Master replied to PING, replication can continue...
15220:S 28 Feb 17:26:10.507 * (Non critical) Master does not understand REPLCONF listening-port: -NOAUTH Authentication required.
15220:S 28 Feb 17:26:10.507 * (Non critical) Master does not understand REPLCONF capa: -NOAUTH Authentication required.
15220:S 28 Feb 17:26:10.507 * Partial resynchronization not possible (no cached master)
15220:S 28 Feb 17:26:10.508 # Unexpected reply to PSYNC from master: -NOAUTH Authentication required.
15220:S 28 Feb 17:26:10.508 * Retrying with SYNC...
15220:S 28 Feb 17:26:10.508 # MASTER aborted replication with an error: NOAUTH Authentication required.
15220:S 28 Feb 17:26:11.508 * Connecting to MASTER 192.168.0.2:6379
15220:S 28 Feb 17:26:11.509 * MASTER <-> SLAVE sync started
15220:S 28 Feb 17:26:11.509 * Non blocking connect for SYNC fired the event.
15220:S 28 Feb 17:26:11.510 * Master replied to PING, replication can continue...
解决
-NOAUTH Authentication required
To set it permanently, add this to your config file:
为了永久地设置它,把这个添加到你的配置文件中:
masterauth <password>
配置redis.conf
[root@localhost bin]# vim redis.conf
# 追加如下
#master的requirepass
masterauth 123456
启动slave
#追加masterauth之后启动
[root@localhost bin]# ./redis-server redis.conf
#查看日志OK
[root@localhost bin]# tail -f redis.log
8125:S 28 Feb 17:46:29.533 * MASTER <-> SLAVE sync: Finished with success
8125:S 28 Feb 17:46:29.534 * Background append only file rewriting started by pid 8129
8125:S 28 Feb 17:46:29.557 * AOF rewrite child asks to stop sending diffs.
8129:C 28 Feb 17:46:29.557 * Parent agreed to stop sending diffs. Finalizing AOF...
8129:C 28 Feb 17:46:29.557 * Concatenating 0.00 MB of AOF diff received from parent.
8129:C 28 Feb 17:46:29.557 * SYNC append only file rewrite performed
8129:C 28 Feb 17:46:29.558 * AOF rewrite: 0 MB of memory used by copy-on-write
8125:S 28 Feb 17:46:29.630 * Background AOF rewrite terminated with success
8125:S 28 Feb 17:46:29.630 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
8125:S 28 Feb 17:46:29.630 * Background AOF rewrite finished successfully
查看主从状态
#由此可见当前节点是slave,master是2
[root@localhost bin]# redis-cli -h 192.168.0.3
192.168.0.3:6379> auth 123456
OK
192.168.0.3:6379> info replication
# Replication
role:slave
master_host:192.168.0.2
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:1246
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:45b7afd69fd8093337f9301313fc0922ccfa6e55
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1246
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1246
测试
#master
[root@localhost bin]# redis-cli -h 192.168.0.2
192.168.0.2:6379> auth 123456
OK
192.168.0.2:6379> set 2 2
OK
192.168.0.2:6379> exit
#slave1
[root@localhost bin]# redis-cli -h 192.168.0.3
192.168.0.3:6379> auth 123456
OK
192.168.0.3:6379> get 2
"2"
192.168.0.3:6379> exit
#slave2
[root@localhost bin]# redis-cli -h 192.168.0.5
192.168.0.5:6379> auth 123456
OK
192.168.0.5:6379> get 2
"2"