集群配置
- 单机多服务集群方式,复制多份配置文件,修改对应信息
- 端口
- pid 名字
- log 文件名字
- dump.rdb 名字
redis-server conf-path启动服务ps -rf/aux | grep "redis"查看服务器是否启动进程redis 9919 1 0 Nov12 ? 00:00:00 /usr/lib/systemd/systemd --userredis 9923 9919 0 Nov12 ? 00:00:00 (sd-pam)redis 9931 1 0 Nov12 ? 00:02:02 /www/server/redis/src/redis-server 0.0.0.0:6379root 17342 1 0 17:41 ? 00:00:00 redis-server 0.0.0.0:6380root 17352 1 0 17:41 ? 00:00:00 redis-server 0.0.0.0:6381root 17361 1 0 17:41 ? 00:00:00 redis-server 0.0.0.0:6382root 17371 16890 0 17:41 pts/2 00:00:00 grep --color=auto redis
info replication查看集群信息(redis默认为master)- 配置一主机两从机
连接集群,在要配置为从机的命令行内输入SLAVEOF host port指定主机
# 从机视角信息info replication# Replicationrole:slavemaster_host:0.0.0.0master_port:6379master_link_status:upmaster_last_io_seconds_ago:7master_sync_in_progress:0slave_repl_offset:98slave_priority:100slave_read_only:1connected_slaves:0master_replid:d0c23b2314d314d1547af95b24e502a7ac7f87c8master_replid2:0000000000000000000000000000000000000000master_repl_offset:98second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:29repl_backlog_histlen:70# 主机视角信息info replication# Replicationrole:masterconnected_slaves:3slave0:ip=127.0.0.1,port=6380,state=online,offset=266,lag=1slave1:ip=127.0.0.1,port=6381,state=online,offset=266,lag=1slave2:ip=127.0.0.1,port=6382,state=online,offset=266,lag=1master_replid:d0c23b2314d314d1547af95b24e502a7ac7f87c8master_replid2:0000000000000000000000000000000000000000master_repl_offset:280second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:280
conf文件配置
由于上面属于临时配置,永久配置需要设置conf里面的replication,这样配置后一起的默认就是从机了
# replicaof <masterip> <masterport># If the master is password protected (using the "requirepass" configuration# directive below) it is possible to tell the replica to authenticate before# starting the replication synchronization process, otherwise the master will# refuse the replica request.## masterauth <master-password>
复制原理
Slave 启动成功连接到master 后会发送一个sync同步命令Master 接到命令,启动后台的存盘进程,同时收集所有接收到的用于修改数据集命令,在后台进程执行完毕之后,master将传送整个数据文件到slave,并完成一次完全同步。<br /> 全量复制:而slave服务在接收到数据库文件数据后,将其存盘并加载到内存中。<br /> 增量复制:Master继续将新的所有收集到的修改命令依次传给slave,完成同步但是只要是重新连接master,一次完全同步(全量复制)将被自动执行
宕机后手动配置
如果主机宕机后,要让从机变成主机使用SLAVEF no one命令即可
但是宕机后的主机在重启,之前连接到他的从机就不属于他了
