[root@hadoop01 /]# cd /usr/local/redis-6.0.0/utils/ [root@hadoop01 utils]# ./install_server.sh Welcome to the redis service installer This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379] 6381 Please select the redis config file name [/etc/redis/6381.conf] Selected default - /etc/redis/6381.conf Please select the redis log file name [/var/log/redis_6381.log] Selected default - /var/log/redis_6381.log Please select the data directory for this instance [/var/lib/redis/6381] Selected default - /var/lib/redis/6381 Please select the redis executable path [/usr/local/bin/redis-server] Selected config: Port : 6381 Config file : /etc/redis/6381.conf Log file : /var/log/redis_6381.log Data dir : /var/lib/redis/6381 Executable : /usr/local/bin/redis-server Cli Executable : /usr/local/bin/redis-cli Is this ok? Then press ENTER to go on or Ctrl-C to abort. Copied /tmp/6381.conf => /etc/init.d/redis_6381 Installing service... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server... Installation successful! [root@hadoop01 utils]#
因为是做测试,所以不直接在系统里修改,我们把redis配置文件拷贝出来一份
1 2 3 4 5 6 7 8 9
[root@hadoop01 utils]# service redis_6381 stop Stopping ... Redis stopped [root@hadoop01 utils]# cd / [root@hadoop01 /]# mkdir test [root@hadoop01 /]# cd test [root@hadoop01 test]# cp /etc/redis/* ./ [root@hadoop01 test]# ls 6379.conf 6380.conf 6381.conf
[root@hadoop01 redis]# redis-server /test/6379.conf 2621:C 30 Mar 2021 17:10:49.733 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 2621:C 30 Mar 202117:10:49.733 # Redis version=6.0.0, bits=64, commit=00000000, modified=0, pid=2621, just started 2621:C 30 Mar 202117:10:49.734 # Configuration loaded 2621:M 30 Mar 202117:10:49.734 * Increased maximum number of open files to10032 (it was originally setto1024). _._ _.-``__ ''-._ _.-```. `_. ''-._ Redis 6.0.0 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 2621 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-'
2621:M 30 Mar 202117:10:49.736 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn issetto the lowervalue of 128. 2621:M 30 Mar 202117:10:49.736 # Server initialized 2621:M 30 Mar 202117:10:49.736 # WARNING overcommit_memory issetto0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1'to /etc/sysctl.conf andthen reboot or run the command 'sysctl vm.overcommit_memory=1'for this to take effect. 2621:M 30 Mar 202117:10:49.736 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled'as root, and add it to your /etc/rc.local inorderto retain the setting after a reboot. Redis must be restarted after THP is disabled. 2621:M 30 Mar 202117:10:49.737 * Ready to accept connections
再开两个tab,分别其他6380、6381
期望,6379是主,6380、6381是主从复制中的从机
4、主从配置
a、【Redis集群方式一】通过命令配置(人为方式)
主机不动,只需要6380、6381追随主机;
老版本是SLAVEOF host port这个命令,redis5.0之后是REPLICAOF host port
1 2 3 4 5 6 7 8
127.0.0.1:6380> help SLAVEOF
SLAVEOF host port summary: Make the server a replica of another instance, or promote it as master. Deprecated starting with Redis 5. Use REPLICAOF instead. since: 1.0.0 group: server
127.0.0.1:6380>
ok ,我们到6380的客户端实施:
1 2 3
127.0.0.1:6380> REPLICAOF 127.0.0.1 6379 OK 127.0.0.1:6380>
然后我们去主机6379控制台看看:
1 2 3 4 5 6 7 8 9
3971:M 31 Mar 2021 10:22:35.933 * Ready to accept connections 3971:M 31 Mar 2021 10:36:23.333 * Replica 127.0.0.1:6380 asks for synchronization 3971:M 31 Mar 2021 10:36:23.333 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for 'f7aea8d38b63623a3d66330aac8a128e45ef6e2c', my replication IDs are 'b0102be2fc0f8d3f08cd98c46b967bf8ee5f6544' and '0000000000000000000000000000000000000000') 3971:M 31 Mar 2021 10:36:23.333 * Starting BGSAVE for SYNC with target: disk 3971:M 31 Mar 2021 10:36:23.338 * Background saving started by pid 3992 3992:C 31 Mar 2021 10:36:23.344 * DB saved on disk 3992:C 31 Mar 2021 10:36:23.345 * RDB: 4 MB of memory used by copy-on-write 3971:M 31 Mar 2021 10:36:23.436 * Background saving terminated with success 3971:M 31 Mar 2021 10:36:23.436 * Synchronization with replica 127.0.0.1:6380 succeeded
3976:S 31 Mar 2021 10:36:22.278 * Before turning into a replica, using my own master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer. 3976:S 31 Mar 2021 10:36:22.278 * REPLICAOF 127.0.0.1:6379 enabled (user request from 'id=4 addr=127.0.0.1:53660 fd=7 name= age=453 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=44 qbuf-free=32724 obl=0 oll=0 omem=0 events=r cmd=replicaof user=default') 3976:S 31 Mar 2021 10:36:23.332 * Connecting to MASTER 127.0.0.1:6379 3976:S 31 Mar 2021 10:36:23.332 * MASTER <-> REPLICA sync started 3976:S 31 Mar 2021 10:36:23.332 * Non blocking connect for SYNC fired the event. 3976:S 31 Mar 2021 10:36:23.333 * Master replied to PING, replication can continue... 3976:S 31 Mar 2021 10:36:23.333 * Trying a partial resynchronization (request f7aea8d38b63623a3d66330aac8a128e45ef6e2c:1). 3976:S 31 Mar 2021 10:36:23.343 * Full resync from master: d4af9c563e480428c2a16e4c949e0b5558af4fd0:0 3976:S 31 Mar 2021 10:36:23.343 * Discarding previously cached master state. 3976:S 31 Mar 2021 10:36:23.437 * MASTER <-> REPLICA sync: receiving 175 bytes from master to disk 3976:S 31 Mar 2021 10:36:23.437 * MASTER <-> REPLICA sync: Flushing old data #这里是需要将自己的老的数据显删除 3976:S 31 Mar 2021 10:36:23.437 * MASTER <-> REPLICA sync: Loading DB in memory 3976:S 31 Mar 2021 10:36:23.437 * Loading RDB produced by version 6.0.0 3976:S 31 Mar 2021 10:36:23.437 * RDB age 0 seconds 3976:S 31 Mar 2021 10:36:23.437 * RDB memory usage when created 1.82 Mb 3976:S 31 Mar 2021 10:36:23.437 * MASTER <-> REPLICA sync: Finished with success
我们在主机6379建立一个客户端,创建一个数据,在主机获取数据肯定也是没有问题的:
1 2 3 4 5 6 7 8
[root@hadoop01 ~]# redis-cli -p 6379 127.0.0.1:6379> set k1 hello OK 127.0.0.1:6379> keys * 1) "k1" 127.0.0.1:6379> get k1 "hello" 127.0.0.1:6379>
我们再去6380去看看有没有数据:
1 2 3 4 5
127.0.0.1:6380> keys * 1) "k1" 127.0.0.1:6380> get k1 "hello" 127.0.0.1:6380>
没有问题,所以说数据是同步过来了
需要注意的是,如果我们去从机上执行set key,会发现不能写
1 2 3
127.0.0.1:6380> set k2 world (error) READONLY You can't write against a read only replica. 127.0.0.1:6380>
[root@hadoop01 ~]# redis-cli -p 6381 127.0.0.1:6381> set k2 aaa OK 127.0.0.1:6381> keys * 1) "k2" 127.0.0.1:6381> get k2 "aaa" 127.0.0.1:6381> REPLICAOF 127.0.0.1 6379 OK 127.0.0.1:6381> keys * 1) "k1"
这个时候,再去看看持久化文件夹,发现都有dump.rdb文件了
增量同步&从机挂了
如果这个时候6381挂掉了,我们看到6379显示
1
3971:M 31 Mar 2021 11:47:13.143 # Connection with replica 127.0.0.1:6381 lost.
然后主机还是写数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
127.0.0.1:6379> set k3 qweqweqw OK 127.0.0.1:6379> set k4 32f23f23 OK 127.0.0.1:6379> set k5 3d3dqdddq OK 127.0.0.1:6379> set k6 3d33d12d OK 127.0.0.1:6379> keys * 1) "k6" 2) "k3" 3) "k5" 4) "k4" 5) "k1" 127.0.0.1:6379>
[root@hadoop01 ~]# redis-server /test/6381.conf --replicaof 127.0.0.1 6379 --appendonly yes 1296:C 31 Mar 2021 14:59:57.942 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1296:C 31 Mar 2021 14:59:57.942 # Redis version=6.0.0, bits=64, commit=00000000, modified=0, pid=1296, just started 1296:C 31 Mar 2021 14:59:57.942 # Configuration loaded 1296:S 31 Mar 2021 14:59:57.943 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 6.0.0 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6381 | `-._ `._ / _.-' | PID: 1296 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-'
1296:S 31 Mar 2021 14:59:57.944 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 1296:S 31 Mar 2021 14:59:57.944 # Server initialized 1296:S 31 Mar 2021 14:59:57.944 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 1296:S 31 Mar 2021 14:59:57.944 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 1296:S 31 Mar 2021 14:59:57.946 * Ready to accept connections 1296:S 31 Mar 2021 14:59:57.947 * Connecting to MASTER 127.0.0.1:6379 1296:S 31 Mar 2021 14:59:57.948 * MASTER <-> REPLICA sync started 1296:S 31 Mar 2021 14:59:57.949 * Non blocking connect for SYNC fired the event. 1296:S 31 Mar 2021 14:59:57.968 * Master replied to PING, replication can continue... 1296:S 31 Mar 2021 14:59:57.970 * Partial resynchronization not possible (no cached master) 1296:S 31 Mar 2021 14:59:57.982 * Full resync from master: 99c61ed7f56cfb5c98c8e9fddf65a641731f7f7b:0 1296:S 31 Mar 2021 14:59:58.070 * MASTER <-> REPLICA sync: receiving 243 bytes from master to disk 1296:S 31 Mar 2021 14:59:58.070 * MASTER <-> REPLICA sync: Flushing old data 1296:S 31 Mar 2021 14:59:58.071 * MASTER <-> REPLICA sync: Loading DB in memory 1296:S 31 Mar 2021 14:59:58.071 * Loading RDB produced by version 6.0.0 1296:S 31 Mar 2021 14:59:58.071 * RDB age 1 seconds 1296:S 31 Mar 2021 14:59:58.071 * RDB memory usage when created 1.83 Mb 1296:S 31 Mar 2021 14:59:58.071 * MASTER <-> REPLICA sync: Finished with success 1296:S 31 Mar 2021 14:59:58.072 * Background append only file rewriting started by pid 1302 1296:S 31 Mar 2021 14:59:58.099 * AOF rewrite child asks to stop sending diffs. 1302:C 31 Mar 2021 14:59:58.100 * Parent agreed to stop sending diffs. Finalizing AOF... 1302:C 31 Mar 2021 14:59:58.100 * Concatenating 0.00 MB of AOF diff received from parent. 1302:C 31 Mar 2021 14:59:58.100 * SYNC append only file rewrite performed 1302:C 31 Mar 2021 14:59:58.100 * AOF rewrite: 4 MB of memory used by copy-on-write 1296:S 31 Mar 2021 14:59:58.151 * Background AOF rewrite terminated with success 1296:S 31 Mar 2021 14:59:58.151 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB) 1296:S 31 Mar 2021 14:59:58.151 * Background AOF rewrite finished successfully