Redis主从复制环境搭建(基于 CentOS 7.9)
Redis主从复制(基于 CentOS 7.9)
1. 单机 redis 安装
- 下载源码包
https://download.redis.io/releases/redis-6.2.6.tar.gz
-
解压缩
tar -zxvf redis-6.2.6.tar.gz
-
安装依赖
yum install gcc-c++ -y
-
编译安装
cd redis-6.2.6/ make && make install
-
创建 redis 专属目录
mkdir -p /usr/local/redis/working
-
拷贝 redis.conf 至 redis 专属目录
cp redis.conf /usr/local/redis/6379.conf
-
修改相关配置
vim 6379.conf daemonzie yes dir /usr/local/redis/working bind 0.0.0.0 # 如需设置密码 requirepass xxxx
-
拷贝启动脚本并修改相关代码
cp redis_init_script /etc/init.d/ vim /etc/init.d/redis_init_script # 修改配置文件路径 CONF="/usr/local/redis/${REDISPORT}.conf" # 自启配置 #chkconfig: 22345 10 90 #description: Start and Stop redis
-
设置自启
chkconfig redis_init_script on
-
启动 redis 并检查
./redis_init_script start ps -ef |grep redis
2. 主从复制
-
编辑从配置文件
replicaof master-node 6379 masterauth root
-
重启从 redis
./redis_init_script stop ./redis_init_script start
-
主 redis 查看
127.0.0.1:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=192.168.31.202,port=6379,state=online,offset=420,lag=1 slave1:ip=192.168.31.203,port=6379,state=online,offset=420,lag=1 master_failover_state:no-failover master_replid:c226480cd48b14c59701fa3a6d801009f8a464f8 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:420 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:420 127.0.0.1:6379>