IP | 操作系统 | 服务 | 版本 |
192.168.140.153 | CentOS 7 | remaster | 7.2.5 |
192.168.140.159 | CentOS 7 | redis-slave | 7.2.5 |
一、安装依赖
1 | yum -y install gcc gcc -c++ |
二、安装Redis
1、下载安装包
1 | wget http: //download .redis.io /releases/redis-7 .2.5. tar .gz |
2、解压
1 | tar -zxvf redis-7.2.5. tar .gz -C /opt/ |
3、创建工作目录
1 2 | mkdir -p /opt/redis/ mkdir -p /opt/redis/logs/ |
4、编译安装
1 2 3 4 5 6 7 8 | # 进入解压目录 cd /opt/redis-7 .2.5 # 编译 make # 安装 make install PREFIX= /opt/redis/ |
5、修改配置文件(Master)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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、创建启动脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [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、启动服务
1 2 3 | systemctl daemon-reload systemctl start redis.service systemctl enable redis.service |
三、测试
1、配置环境变量
1 2 3 4 5 | [root@localhost redis] # vi /etc/profile # 在最后添加如下内容 PATH=${PATH}: /opt/redis/bin/ [root@localhost redis] # source /etc/profile |
2、查看redis-master
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | [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节点插入数据
1 2 3 4 | 192.168.140.153:6379> set test "master" OK 192.168.140.153:6379> get test "master" |
5、slave节点查看数据是否同步
1 2 3 4 5 | 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主从复制内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!