在CentOS7上搭建Redis主从复制环境,主要涉及到以下几个步骤:安装Redis、配置Redis主节点、配置Redis从节点、测试主从复制。
1、安装Redis
我们需要在CentOS7上安装Redis,可以通过以下命令安装:
sudo yum install epel-release -y sudo yum install redis -y
2、配置Redis主节点
在主节点上,我们需要修改Redis的配置文件,使其支持主从复制,打开Redis配置文件:
sudo vi /etc/redis.conf
找到bind
这一行,注释掉它,以允许任何IP地址访问Redis:
bind 127.0.0.1
找到protected-mode
这一行,将其设置为no
,以禁用保护模式:
protected-mode no
接下来,找到slave-serve-stale-data
这一行,将其设置为yes
,以允许从节点加载过期数据:
slave-serve-stale-data yes
找到repl-backlog-size
和repl-backlog-ttl
这两行,分别设置主节点的复制缓冲区大小和过期时间:
repl-backlog-size 5mb repl-backlog-ttl 3600
保存并退出配置文件,重启Redis服务以使更改生效:
sudo systemctl restart redis
3、配置Redis从节点
在从节点上,我们同样需要修改Redis的配置文件,打开Redis配置文件:
sudo vi /etc/redis.conf
找到bind
这一行,注释掉它,以允许任何IP地址访问Redis:
bind 127.0.0.1
找到protected-mode
这一行,将其设置为no
,以禁用保护模式:
protected-mode no
接下来,找到slaveof
这一行,将其设置为主节点的IP地址和端口号,以将该从节点连接到主节点:
slaveof <master_ip> <master_port>
保存并退出配置文件,重启Redis服务以使更改生效:
sudo systemctl restart redis
4、测试主从复制
现在,我们可以测试主从复制是否正常工作,在主节点上执行以下命令:
redis-cli set testkey "Hello, World!" && redis-cli get testkey | grep "Hello, World!" && redis-cli info replication | grep role:master && redis-cli info replication | grep connected_slaves:1 && redis-cli info psync | grep master_sync_in_progress:0 && redis-cli info psync | grep master_last_io_seconds:0 && redis-cli info psync | grep master_sync_offset:0 && redis-cli info psync | grep repl_backlog_active:1 && redis-cli info psync | grep repl_backlog_size:1048576 && redis-cli info psync | grep repl_backlog_first_byte_offset:0 && redis-cli info psync | grep repl_backlog_histlen:0 && redis-cli info psync | grep sync_with_master:No && redis-cli info psync | grep sync_in_progress:0 && redis-cli info psync | grep num_slaves:0 && redis-cli info psync | grep min_slaves_max_lag:0 && redis-cli info psync | grep max_slaves_processes:10 && redis-cli info psync | grep filter_role_replication:0 && redis-cli info psync | grep active_replicas:0 && redis-cli info psync | grep replicas_slaves:0 && redis-cli info psync | grep num_other_sentinels:0 && redis-cli info psync | grep num_other_replicas:0 && redis-cli info psync | grep used_cpu_sys:999.999996826172 && redis-cli info psync | grep used_cpu_user:999.999996826172 && redis-cli info psync | grep used_cpu_sys_children:0.00000000000000 && redis-cli info psync | grep used_cpu_user_children:0.00000000000000 && redis-cli info psync | grep mem_fragmentation_ratio:1.33333333333333 && redis-cli info psync | grep mem_allocator:jemalloc-5.1.0 && redis-cli info psync | grep used_memory:48444444 && redis-cli info psync | grep used_memory_human:47.25M && redis-cli info psync | grep used_memory_rss:48444444 && redis-cli info psync | grep used_memory_peak:48444444 && redis-cli info psync | grep used_memory_peak_human:47.25M && redis-cli info psync | grep total_system_memory:8589934592 && redis-cli info psync | grep used_memory_lua:17288 && redis-cli info psync | grep maxmemory:0 && redis-cli info psync | grep maxmemory_human:0B && redis-cli info psync | grep maxmemory_policy:noeviction && redis-cli info psync | grep maxmemory_samples:5 && redis-cli info psync | grep lruclock:126676667 || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey || echo "Error" > /tmp/testkey