当前位置:   article > 正文

Redis 7.2.x 主从复制

Redis 7.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、创建工作目录
  1. mkdir -p /opt/redis/
  2. mkdir -p /opt/redis/logs/
4、编译安装
  1. # 进入解压目录
  2. cd /opt/redis-7.2.5
  3. # 编译
  4. make
  5. # 安装
  6. make install PREFIX=/opt/redis/
5、修改配置文件(Master)
  1. cp /opt/redis-7.2.5/redis.conf /opt/redis/
  2. vi /opt/redis/redis.conf
  3. # 修改如下内容
  4. bind 192.168.140.153 -::1
  5. # 允许后台运行
  6. daemonize yes
  7. # pid文件位置
  8. pidfile /opr/redis/redis_6379.pid
  9. # 日志位置
  10. logfile "/opt/redis/logs/redis.log"
  11. # 工作目录
  12. dir /opt/redis/
  13. # 主从同步密码
  14. masterauth final123
  15. # redis密码
  16. requirepass final123
6、修改配置文件(Slave)
  1. cp /opt/redis-7.2.5/redis.conf /opt/redis/
  2. vi /opt/redis/redis.conf
  3. # 修改如下内容
  4. bind 192.168.140.153 -::1
  5. # 允许后台运行
  6. daemonize yes
  7. # pid文件位置
  8. pidfile /opr/redis/redis_6379.pid
  9. # 日志位置
  10. logfile "/opt/redis/logs/redis.log"
  11. # 工作目录
  12. dir /opt/redis/
  13. # 主从同步密码
  14. masterauth final123
  15. # redis密码
  16. requirepass final123
  17. replilcaof 192.168.140.153 6379
7、创建启动脚本
  1. [root@localhost redis]# vi /etc/systemd/system/redis.service
  2. [Unit]
  3. Description=Redis
  4. After=network.target
  5. [Service]
  6. Type=simple
  7. User=root
  8. Group=root
  9. ExecStart=/opt/redis/bin/redis-server /opt/redis/redis.conf --daemonize no
  10. Restart=always
  11. LimitNPROC=65535
  12. LimitNOFILE=65535
  13. [Install]
  14. WantedBy=multi-user.target
8、启动服务
  1. systemctl daemon-reload
  2. systemctl start redis.service
  3. systemctl enable redis.service

三、测试

1、配置环境变量
  1. [root@localhost redis]# vi /etc/profile
  2. # 在最后添加如下内容
  3. PATH=${PATH}:/opt/redis/bin/
  4. [root@localhost redis]# source /etc/profile
2、查看redis-master
  1. [root@localhost redis]# redis-cli -h 192.168.140.153
  2. 192.168.140.153:6379> auth final123
  3. OK
  4. 192.168.140.153:6379> info replication
  5. # Replication
  6. role:master
  7. connected_slaves:1
  8. slave0:ip=192.168.140.159,port=6379,state=online,offset=350,lag=1
  9. master_failover_state:no-failover
  10. master_replid:28a3b660f11504c07b2cc4bc07a093970af1544b
  11. master_replid2:0000000000000000000000000000000000000000
  12. master_repl_offset:350
  13. second_repl_offset:-1
  14. repl_backlog_active:1
  15. repl_backlog_size:1048576
  16. repl_backlog_first_byte_offset:1
  17. repl_backlog_histlen:350
3、查看redis-slave
  1. [root@localhost redis]# redis-cli -h 192.168.140.159
  2. 192.168.140.159:6379> auth final123
  3. OK
  4. 192.168.140.159:6379> info replication
  5. # Replication
  6. role:slave
  7. master_host:192.168.140.153
  8. master_port:6379
  9. master_link_status:up
  10. master_last_io_seconds_ago:6
  11. master_sync_in_progress:0
  12. slave_read_repl_offset:406
  13. slave_repl_offset:406
  14. slave_priority:100
  15. slave_read_only:1
  16. replica_announced:1
  17. connected_slaves:0
  18. master_failover_state:no-failover
  19. master_replid:28a3b660f11504c07b2cc4bc07a093970af1544b
  20. master_replid2:0000000000000000000000000000000000000000
  21. master_repl_offset:406
  22. second_repl_offset:-1
  23. repl_backlog_active:1
  24. repl_backlog_size:1048576
  25. repl_backlog_first_byte_offset:1
  26. repl_backlog_histlen:406
4、master节点插入数据
  1. 192.168.140.153:6379> set test "master"
  2. OK
  3. 192.168.140.153:6379> get test
  4. "master"
5、slave节点查看数据是否同步
  1. 192.168.140.159:6379> get test
  2. "master"
  3. # slave节点只能读取,不能写入。
  4. 192.168.140.159:6379> set test1 "ceshi"
  5. (error) READONLY You can't write against a read only replica.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/682809
推荐阅读
相关标签
  

闽ICP备14008679号