redis多实例主从配置

hcwei 2020年06月10日 441次浏览

一、安装redis

以redis-4.0.11为例:

[root@localhost]# yum install -y gcc             
[root@localhost]# tar xf redis-4.0.11.tar.gz
[root@localhost]# cd redis-4.0.11
[root@localhost]# make MALLOC=libc
[root@localhost]# cd src && make install
[root@localhost]# cp /tools/package/redis-4.0.11/redis.conf /etc/redis/6379.conf
[root@localhost]# sed -i "s#daemonize no#daemonize yes#g" /etc/redis/6379.conf
[root@localhost]# cp /tools/package/redis-4.0.11/utils/redis_init_script /etc/init.d/redis
[root@localhost]# sed -i "2i# chkconfig:   2345 90 10" /etc/init.d/redis
[root@localhost]# chkconfig redis on  
启动命令:

[root@localhost]# systemctl start redis      #启动redis
[root@localhost]# systemctl stop redis       #关闭redis
[root@localhost]# redis-cli                  #进入redis
[root@localhost]# redis-server xxx.conf    #指定配置文件启动redis
设置密码:

[root@localhost]# vim /etc/redis/6379.conf
打开requirepass这项的注释,在其后添加密码然后重启redis

[root@localhost]# systemctl restart redis

二、配置多实例:

[root@localhost]# mkdir -p  /redis/{6380,6381,6382}/{conf,db,log}
[root@localhost]# cp /etc/redis/6379.conf /redis/6380/conf/6380.conf
[root@localhost]# cp /etc/redis/6379.conf /redis/6381/conf/6381.conf
[root@localhost]# cp /etc/redis/6379.conf /redis/6382/conf/6382.conf
修改配置文件:

[root@localhost]# vim /redis/6380/conf/6380.conf

daemonize yes                 # daemon进程运行
pidfile /redis/6380/redis.pid       # 进程id存放文件
port 6380                           # 端口
logfile /redis/6380/log/redis.log     # 日志目录
dir /redis/6380/db/                 # db目录
启动实例:

[root@localhost]# redis-server /redis/6380/conf/6380.conf
[root@localhost]# redis-server /redis/6381/conf/6381.conf
[root@localhost]# redis-server /redis/6382/conf/6382.conf
[root@localhost]# netstat -ntlp | grep -E ":6380|:6381|:6382"
tcp0  0 127.0.0.1:6380  0.0.0.0:*   LISTEN  14301/redis-server  
tcp0  0 127.0.0.1:6381  0.0.0.0:*   LISTEN  14305/redis-server  
tcp0  0 127.0.0.1:6382  0.0.0.0:*   LISTEN  14309/redis-server  
验证启动:

[root@localhost]# redis-cli -p 6380 
[root@localhost]# redis-cli -p 6381
[root@localhost]# redis-cli -p 6382

三、配置主从同步

修改从库配置,6380实例为主库,从库为 6381,6382:

[root@localhost]# vim /redis/6381/conf/6381.conf
[root@localhost]# vim /redis/6382/conf/6382.conf

################################# REPLICATION #################################

# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# 1) Redis replication is asynchronous, but you can configure a master to
#stop accepting writes if it appears to be not connected with at least
#a given number of slaves.
# 2) Redis slaves are able to perform a partial resynchronization with the
#master if the replication link is lost for a relatively small amount of
#time. You may want to configure the replication backlog size (see the next
#sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
#network partition slaves automatically try to reconnect to masters
#and resynchronize with them.
#
# slaveof <masterip> <masterport>
slaveof 127.0.0.1 6380

# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
# masterauth <master-password>

# When a slave loses its connection with the master, or when the replication
# is still in progress, the slave can act in two different ways:
#
# 1) if slave-serve-stale-data is set to 'yes' (the default) the slave will
#still reply to client requests, possibly with out of date data, or the
#data set may just be empty if this is the first synchronization.
#
# 2) if slave-serve-stale-data is set to 'no' the slave will reply with
#an error "SYNC with master in progress" to all the kind of commands
查看主库info:

[root@localhost]# redis-cli -p 6380 "info"
.
.此处略去n行
.
# Replication
role:master                                            # 角色:master
connected_slaves:2                             # slave链接数 2
slave0:ip=127.0.0.1,port=6381,state=online,offset=141,lag=1    # slave 的信息
slave1:ip=127.0.0.1,port=6382,state=online,offset=141,lag=1
master_repl_offset:141
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:140
查看从库info: [root@localhost]# redis-cli -p 6381 "info" ... .此处略去n行 .... # Replication role:slave         # 角色 slave masterhost:127.0.0.1   # master主机 masterport:6380      # master端口 masterlinkstatus:up # 链接状态 up masterlastiosecondsago:5 mastersyncinprogress:0 slavereploffset:673 slavepriority:100 slavereadonly:1 connectedslaves:0 masterreploffset:0 replbacklogactive:0 replbacklogsize:1048576 replbacklog_firstbyteoffset:0 replbackloghistlen:0

[root@localhost]# redis-cli -p 6382 "info"
...
.此处略去n行
....
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:911
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
验证:在主库上写入数据:

[root@localhost]# redis-cli -p 6380
127.0.0.1:6380> set a b
OK
127.0.0.1:6380> keys *
1) "a"
127.0.0.1:6380> get a
"b"
127.0.0.1:6380> exit
从库上查看是否已同步:

[root@localhost]# redis-cli -p 6381
127.0.0.1:6381> keys *
1) "a"
127.0.0.1:6381> get a
"b"
127.0.0.1:6381> exit

[root@localhost]# redis-cli -p 6382
127.0.0.1:6382> keys *
1) "a"
127.0.0.1:6382> get a
"b"
127.0.0.1:6382> exit
主库删除数据:

[root@localhost]# redis-cli -p 6380
127.0.0.1:6380> keys *
1) "a"
127.0.0.1:6380> del a
(integer) 1
127.0.0.1:6380> keys *
(empty list or set)
127.0.0.1:6380> exit
从库查看:

[root@localhost]# redis-cli -p 6381
127.0.0.1:6381> keys *
(empty list or set)
127.0.0.1:6381> exit

[root@localhost]# redis-cli -p 6382
127.0.0.1:6382> keys *
(empty list or set)
127.0.0.1:6382> exit

至此redis多实例主从配置安装完成!!!