redis主从复制搭建
2、分别部署redis
我已经提前准备好了,具体步骤看我另一个博客
3、主从配置文件更改
3.1、在188上更改配置文件(新增的,其他的都没改)
#cli连接密码,从机连接密码
requirepass tzh123
3.2、在189上操作(新增的,其他的都没改)
#配置主服务器的ip 端口 说明一下,replicaof是新版本的命令,旧版本是slaveof命令
slaveof 192.168.47.188 6379
#配置主服务器的密码
masterauth tzh123
4、启动
#启动主服务器
[root@t1 bin]# pwd
/app/redis/bin
[root@t1 bin]# ./redis-server ../conf/redis.conf
#启动从服务器
[root@t2 bin]# pwd
/app/redis/bin
[root@t2 bin]# ./redis-server ../conf/redis.conf
5、验证、测试
5.1、验证
#在主服务器上,输入连接密码
[root@t1 bin]# ./redis-cli
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> info replication
# Replication
role:master #这是主服务器master
connected_slaves:1 #成功连接slave服务器的数量
slave0:ip=192.168.47.189,port=6379,state=online,offset=1691,lag=1 #slave服务器的信息
master_replid:f60d4b3d8b8f295491af42a3f57a7e59a8f60f7e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1691
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:169
#在从服务器连接
127.0.0.1:6379> INFO replication
# Replication
role:slave #这是一台slave服务器
master_host:192.168.47.188 #主服务器端ip地址
master_port:6379 #主服务器端口号
master_link_status:up #连接状态
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:1943
slave_priority:100
slave_read_only:1 #成为slave服务器后,只读。不能set 只能get
connected_slaves:0
master_replid:f60d4b3d8b8f295491af42a3f57a7e59a8f60f7e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1943
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1943
5.2、测试
#在主服务器上
127.0.0.1:6379> set test 123
OK
#在从服务器上
127.0.0.1:6379> get test
"123"
#说明可以主从搭建成功。有空在更新个哨兵模式,也很简单。在再一台服务器,增加个哨兵就可以了。
5.3、从机日志查看
[root@slavenode2 bin]# tail -f /app/redis/logs/redis-6379.log
2282:S 19 Jun 2021 09:55:23.666 * MASTER <-> REPLICA sync started
2282:S 19 Jun 2021 09:55:23.667 * Non blocking connect for SYNC fired the event.
2282:S 19 Jun 2021 09:55:23.668 * Master replied to PING, replication can continue...
2282:S 19 Jun 2021 09:55:23.670 * Trying a partial resynchronization (request 522177d610ad0b0637a2599d7b83326cb261f05b:939).
2282:S 19 Jun 2021 09:55:23.672 * Full resync from master: ebb803791e33f3f2abfeef1e099b2d5ee2d7e3ab:0
2282:S 19 Jun 2021 09:55:23.672 * Discarding previously cached master state.
2282:S 19 Jun 2021 09:55:23.712 * MASTER <-> REPLICA sync: receiving 175 bytes from master
2282:S 19 Jun 2021 09:55:23.712 * MASTER <-> REPLICA sync: Flushing old data
2282:S 19 Jun 2021 09:55:23.712 * MASTER <-> REPLICA sync: Loading DB in memory
2282:S 19 Jun 2021 09:55:23.712 * MASTER <-> REPLICA sync: Finished with success
7、配置文件详解