Redis

关注公众号 jb51net

关闭
首页 > 数据库 > Redis > Redis7.2.x主从复制

Redis7.2.x主从复制的实现示例

作者:梦·D·

本文主要介绍了Redis7.2.x主从复制的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
IP操作系统服务版本
192.168.140.153CentOS 7remaster7.2.5
192.168.140.159CentOS 7redis-slave7.2.5

一、安装依赖

yum -y install gcc gcc-c++

二、安装Redis

1、下载安装包

wget http://download.redis.io/releases/redis-7.2.5.tar.gz

2、解压

tar -zxvf redis-7.2.5.tar.gz -C /opt/

3、创建工作目录

mkdir -p /opt/redis/
mkdir -p /opt/redis/logs/

4、编译安装

# 进入解压目录
cd /opt/redis-7.2.5

# 编译
make

# 安装
make install PREFIX=/opt/redis/

5、修改配置文件(Master)

cp /opt/redis-7.2.5/redis.conf /opt/redis/

vi /opt/redis/redis.conf
# 修改如下内容
bind 192.168.140.153 -::1
# 允许后台运行
daemonize yes
# pid文件位置
pidfile /opr/redis/redis_6379.pid
# 日志位置
logfile "/opt/redis/logs/redis.log"
# 工作目录
dir /opt/redis/
# 主从同步密码
masterauth final123
# redis密码
requirepass final123

6、修改配置文件(Slave)

cp /opt/redis-7.2.5/redis.conf /opt/redis/

vi /opt/redis/redis.conf
# 修改如下内容
bind 192.168.140.153 -::1
# 允许后台运行
daemonize yes
# pid文件位置
pidfile /opr/redis/redis_6379.pid
# 日志位置
logfile "/opt/redis/logs/redis.log"
# 工作目录
dir /opt/redis/
# 主从同步密码
masterauth final123
# redis密码
requirepass final123

replilcaof 192.168.140.153 6379

7、创建启动脚本

[root@localhost redis]# vi /etc/systemd/system/redis.service
[Unit]
Description=Redis
After=network.target
 
[Service]
Type=simple
User=root
Group=root
ExecStart=/opt/redis/bin/redis-server /opt/redis/redis.conf --daemonize no
Restart=always
LimitNPROC=65535
LimitNOFILE=65535
 
[Install]
WantedBy=multi-user.target

8、启动服务

systemctl daemon-reload
systemctl start redis.service
systemctl enable redis.service

三、测试

1、配置环境变量

[root@localhost redis]# vi /etc/profile
# 在最后添加如下内容
PATH=${PATH}:/opt/redis/bin/

[root@localhost redis]# source /etc/profile

2、查看redis-master

[root@localhost redis]# redis-cli -h 192.168.140.153
192.168.140.153:6379> auth final123
OK
192.168.140.153:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.140.159,port=6379,state=online,offset=350,lag=1
master_failover_state:no-failover
master_replid:28a3b660f11504c07b2cc4bc07a093970af1544b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:350
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:350

3、查看redis-slave

[root@localhost redis]# redis-cli -h 192.168.140.159
192.168.140.159:6379> auth final123
OK
192.168.140.159:6379> info replication
# Replication
role:slave
master_host:192.168.140.153
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_read_repl_offset:406
slave_repl_offset:406
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:28a3b660f11504c07b2cc4bc07a093970af1544b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:406
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:406

4、master节点插入数据

192.168.140.153:6379> set test "master"
OK
192.168.140.153:6379> get test
"master"

5、slave节点查看数据是否同步

192.168.140.159:6379> get test
"master"
# slave节点只能读取,不能写入。
192.168.140.159:6379> set test1 "ceshi"
(error) READONLY You can't write against a read only replica.

到此这篇关于Redis7.2.x主从复制的实现示例的文章就介绍到这了,更多相关Redis7.2.x主从复制内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

您可能感兴趣的文章:
阅读全文