Redis主从复制

1.准备3台服务器,一台master ,两台 slave

  Master    192.168.146.128

  Slave      192.168.146.129

  slave         192.168.146.130

 

2.每台服务器安装redis

redis安装的详细文档地址:

https://www.cnblogs.com/Amywangqing/p/12732742.html

 

3.进入第一台Maste 192.168.146.128服务器

cd /usr/local/redis进入redis目录

对应的修改

注意(需要修改redis.conf文件的bind 192.168.146.128 127.0.0.1)

注意(需要修改redis.conf文件的bind 192.168.146.129 127.0.0.1)

注意(需要修改redis.conf文件的bind 192.168.146.130 127.0.0.1)

 

4.启动redis   三台都一样

./bin/redis-server ./redis.conf 
[root@localhost redis]# ./bin/redis-server ./redis.conf 
2859:C 21 Apr 2020 20:37:21.282 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2859:C 21 Apr 2020 20:37:21.282 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=2859, just started
2859:C 21 Apr 2020 20:37:21.282 # Configuration loaded
[root@localhost redis]#

  

5.进入bin目录

 

cd bin

 

./redis-cli -h 192.168.146.128 -p 6379 -a 123456

  

进入redid客户端Slave   192.168.146.129
./redis-cli -h 192.168.146.129 -p 6379 -a 123456

进入redid客户端Slave   192.168.146.130
./redis-cli -h 192.168.146.130 -p 6379 -a 123456

  

6.通过info replication查看redis主从信息

主机92.168.146.128

192.168.146.128:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:22df065d1113b3458a187d70dee08d0f39baaeee
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
192.168.146.128:6379>

  

主机92.168.146.129

192.168.146.129:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:818ffa04e22d35c5c15b5ea1f227c0cf6297984e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
192.168.146.129:6379>

  

主机192.168.146.130

192.168.146.130:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:1da39532e2d39e6dcfd2b7b17883bc3ec659b527
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
192.168.146.130:6379>

  

可以看出来现在它们都是master

我们把主机192.168.146.128作为master,就不需要修改了

 

这主机192.168.146.128Redis里的数据

192.168.146.128:6379> keys *
 1) "subCat:2"
 2) "shopcart:1908017YR51G1XWH"
 3) "subCat:3"
 4) "subCat:6"
 5) "subCat:1"
 6) "subCat:8"
 7) "subCat:9"
 8) "subCat:4"
 9) "shopcart:2004209Z3KCNCD1P"
10) "subCat:7"
11) "subCat:10"
12) "carousel"
13) "subCat:5"
14) "cats"
192.168.146.128:6379>

  

7.修改主机192.168.146.129

进入cd /usr/local/redis

修改redisd.conf文件

 

添加

replicaof 192.168.146.128 6379

masterauth 123456

添加完成,保存

 

8.关闭redis

 

[root@localhost bin]#  ps aux | grep redis
root      3586  0.1  1.2 161628 12452 ?        Ssl  21:19   0:05 ./bin/redis-server 192.168.146.129:6379
root      4208  0.0  0.0 103248   832 pts/0    S+   22:11   0:00 grep redis
[root@localhost bin]# kill -9 3586
[root@localhost bin]#

 

  

9.删除我们redis的持久化文件

 

[root@localhost log]# ls
appendonly.aof  dump.rdb
[root@localhost log]# rm appendonly.aof dump.rdb 
rm: remove regular file `appendonly.aof'? y
rm: remove regular file `dump.rdb'? y
[root@localhost log]#

 

  

10.开启redis

./bin/redis-server ./redis.conf

进入redis客户

./redis-cli -h 192.168.146.129 -p 6379 -a 123456

[root@localhost bin]# ./redis-cli -h 192.168.146.129 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.146.129:6379> keys *
 1) "subCat:4"
 2) "subCat:9"
 3) "subCat:8"
 4) "subCat:1"
 5) "shopcart:2004209Z3KCNCD1P"
 6) "subCat:3"
 7) "shopcart:1908017YR51G1XWH"
 8) "subCat:10"
 9) "carousel"
10) "cats"
11) "subCat:5"
12) "subCat:7"
13) "subCat:6"
14) "subCat:2"
192.168.146.129:6379>

  这里的数据又有了,这跟192.168.146.128上的redis同步了

 

 

 

 

11.通过info replication查看

 

这里它已经变为slave从机了

 

它的主机master_host:192.168.146.128

 

master_link_status:up 主机master启动的

192.168.146.129:6379> info replication
# Replication
role:slave
master_host:192.168.146.128
master_port:6379
master_link_status:up 
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:1831
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:13e5ca34c383c3a0043d46a3dec7093a5199e4e6
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1831
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1831
192.168.146.129:6379>

  

12.从为了slave从机就只能读操作了,不能进行写操作

 

192.168.146.129:6379> set name aaa
(error) READONLY You can't write against a read only replica.
192.168.146.129:6379>

 

  

这里主从复制就成功了

 

 

修改主机192.168.146.130

 

192.168.146.130同上修改就可以了

 

 

添加

replicaof 192.168.146.128 6379

masterauth 123456

添加完成,保存

后面的步骤同上面的192.168.146.129一样

 

在192.168.146.130通过info replication查看

192.168.146.130:6379> info replication
# Replication
role:slave
master_host:192.168.146.128
master_port:6379
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:2741
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:13e5ca34c383c3a0043d46a3dec7093a5199e4e6
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2741
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1359
repl_backlog_histlen:1383
192.168.146.130:6379>

  

在192.168.146.128nfo replication查看

192.168.146.128:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.146.129,port=6379,state=online,offset=2895,lag=0
slave1:ip=192.168.146.130,port=6379,state=online,offset=2895,lag=0
master_replid:13e5ca34c383c3a0043d46a3dec7093a5199e4e6
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2895
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2895
192.168.146.128:6379>

  

  这样主从复制就完成了

 

posted @ 2020-05-15 21:18  Amy清风  阅读(224)  评论(0编辑  收藏  举报