Redis主从复制-配置
我这里在一台电脑上通过不同端口的方式实现一主多从。
1.修改主redis服务器配置
查看本机ip
[root@localhost etc]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:3c:dd:1c brd ff:ff:ff:ff:ff:ff inet 192.168.211.131/24 brd 192.168.211.255 scope global noprefixroute dynamic ens33 valid_lft 1423sec preferred_lft 1423sec inet6 fe80::2290:713d:e49:28fe/64 scope link noprefixroute valid_lft forever preferred_lft forever
IP地址:192.168.211.131
主服务器配置如下
vim redis.conf
以守护进程的方式启动redis服务器
# By default Redis does not run as a daemon. Use 'yes' if you need it. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. daemonize yes
主服务器的与本机ip绑定
bind 192.168.211.131 127.0.0.1
设置端口,主服务器默认6379端口
# If port 0 is specified Redis will not listen on a TCP socket. port 6379
2.从服务器配置。配置简单的主从,从服务器使用的配置与主服务器区别在于端口设置不一样。
直接复制主配置
[root@localhost etc]# cp redis.conf slave6380.conf
设置从服务器端口为6380
[root@localhost etc]# cp redis.conf slave6380.conf
# If port 0 is specified Redis will not listen on a TCP socket. port 6380
3.启动主从服务器
[root@localhost etc]# redis-server redis.conf 78247:C 06 Dec 22:32:17.871 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 78247:C 06 Dec 22:32:17.871 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=78247, just started 78247:C 06 Dec 22:32:17.872 # Configuration loaded [root@localhost etc]# redis-server slave6380.conf 78383:C 06 Dec 22:32:25.570 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 78383:C 06 Dec 22:32:25.570 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=78383, just started 78383:C 06 Dec 22:32:25.570 # Configuration loaded
4.连接主服务器,查看是否配置成功
[root@localhost etc]# redis-cli -h 192.168.211.131 -p 6379 info Replication # Replication role:master connected_slaves:1 slave0:ip=192.168.211.131,port=6380,state=online,offset=2463,lag=0 master_replid:7f4521023b468779fe7560801deadd9279382586 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:2463 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:2463 [root@localhost etc]#
slave0:ip=192.168.211.131,port=6380,state=online,offset=2463,lag=0 这里可以看出配置成功
5.主服务器创建数据后,登录从服务器查看新增的数据
[root@localhost etc]# redis-cli -h 192.168.211.131 -p 6379 192.168.211.131:6379> set message hello world (error) ERR syntax error 192.168.211.131:6379> set message hello world [EX seconds] [PX millis [root@localhost etc]# redis-cli -h 192.168.211.131 -p 6379 192.168.211.131:6379> set name zhangsan OK 192.168.211.131:6379> [root@localhost etc]# redis-cli -h 192.168.211.131 -p 6380 192.168.211.131:6380> get name "zhangsan" 192.168.211.131:6380>
简单的主从就配好了。
[root@localhost etc]# ls dump.rdb redis.conf slave6380.conf