主从复制:master以写为主,slave以读为主,当master数据变化的时候,自动将新的数据异步同步到其他的slave数据库
1. Redis复制介绍:
https://redis.io/docs/latest/operate/oss_and_stack/management/replication/
- 读写分离
- 容灾恢复
- 数据备份
- 水平扩容支撑高并发
2. 下载
下载到自己指定的目录:
mkdir /opt/coisini/redis-clustercd /opt/coisini/redis-clusterwget https://download.redis.io/releases/redis-7.0.15.tar.gz
3. 解压以及重命名
搭建3台,分别为redis6380、redis6381、redis6382
tar -zxvf redis-7.0.15.tar.gz
进入redis目录:
cd redis-7.0.15/
4. 编译安装
安装 systemd-devel 和其他编译所需的依赖
yum -y install centos-release-scl devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutilsscl enable devtoolset-9 bash systemd-devel make gcc
配置并编译 Redis
make BUILD_WITH_SYSTEMD=yes
等待一会~
编译安装:
make install
如果报以下错误
:
解决办法:
make MALLOC=libc
5. 编辑配置文件
vi /opt/coisini/redis-cluster/redis-7.0.15/redis.conf
(按Esc退出编辑模式下:
/protected-mode
即可搜索,回车后按n
可以跳转搜索下一个,:set number
可以显示行号 )
# 关闭保护模式
protected-mode no
# 守护进程模式开启
daemonize yes
# 绑定IP按需修改,bind指定网段远程访问redis,注释就没有限制了,或者0.0.0.0。
bind 0.0.0.0
# 端口(单机默认,集群按需修改)
port 6380# (搜/requirepass foobared)设置Redis密码
requirepass 123456
# 允许redis后台运行
daemonize yes# systemd接管服务
supervised systemd
# 指定当前工作目录,dir(大概在504行)
dir /opt/coisini/redis-cluster/redis6380/tmp
pidfile /opt/coisini/redis-cluster/redis6380/redis_6380.pid
# redis日志输出(可以配置为./6380.log)
logfile /opt/coisini/redis-cluster/redis6380/redis6380.log
# 校验master主机密码
masterauth 123456
# dump.rdb名字
dbfilename dump6380.rdb
# 配置主从(这个只要从机配置就行了,主机注释掉)
replicaof 192.168.31.16 6380
appendonly yes
#配置主从(这个只要从机配置就行了,主机注释掉)
replicaof 192.168.31.16 6380
:wq保存并退出
6. 拷贝三份
for port in 6380 6381 6382; do cp -r redis-7.0.15 redis$port; done
继续修改6381、6382 两个
文件的配置
vi /opt/coisini/redis-cluster/redis6381/redis.conf
vi /opt/coisini/redis-cluster/redis6382/redis.conf
分别修改以下配置:
(按Esc退出编辑模式下:
/protected-mode
即可搜索,回车后按n
可以跳转搜索下一个,:set number
可以显示行号 )
# 端口(单机默认,集群按需修改)
port 6381
# 指定当前工作目录,dir(大概在504行)
dir /opt/coisini/redis-cluster/redis6381/
pidfile /opt/coisini/redis-cluster/redis6381/redis_6381.pid
# redis日志输出(可以配置为./6381.log)
logfile /opt/coisini/redis-cluster/redis6381/redis6381.log
# dump.rdb名字
dbfilename dump6381.rdb
:wq保存退出
7. 创建日志目录并赋权
useradd -r -s /bin/false redisfor port in 6380 6381 6382; do mkdir -p /opt/coisini/redis-cluster/redis$port/tmptouch /opt/coisini/redis-cluster/redis$port/redis$port.logchown redis:redis /opt/coisini/redis-cluster/redis$port/redis$port.logchown -R redis:redis /opt/coisini/redis-cluster/redis$portchmod -R 770 /opt/coisini/redis-cluster/redis$port
done
8. 编辑启动脚本
vi redis-cluster.sh
添加:
#!/bin/bash# Redis集群节点端口列表
ports=(6380 6381 6382)# Redis的安装路径
redis_path="/opt/coisini/redis-cluster"# Redis的密码
password="123456"# 启动函数
start() {echo "Starting Redis cluster..."for port in "${ports[@]}"; doecho "Starting Redis instance on port $port..."$redis_path/redis$port/src/redis-server $redis_path/redis$port/redis.confdoneecho "Redis cluster started."
}# 停止函数
stop() {echo "Stopping Redis cluster..."for port in "${ports[@]}"; doecho "Stopping Redis instance on port $port..."$redis_path/redis$port/src/redis-cli -a $password -p $port shutdowndoneecho "Redis cluster stopped."
}# 脚本的主逻辑
case "$1" instart)start;;stop)stop;;restart)stopstart;;*)echo "Usage: $0 {start|stop|restart}"exit 1
esac
文件赋权:
chmod +x redis-cluster.sh
9. 开放端口:
sudo firewall-cmd --permanent --add-port=6380/tcp --add-port=6381/tcp --add-port=6382/tcp
sudo firewall-cmd --reload
10. 先不着急启动,设置哨兵模式:
参数解析:
protected-model:安全保护模式
port:端口
daemonize:是否以后台daemon方式运行
pidfile:pid文件路径
logfile:日志文件路径
dir:工作目录
sentinel monitor mymaster 127.0.0.1 6379 2 设置要监控的master服务器
quorum表示最少有几个哨兵认可客观下线,同意故障迁移的法定票数
sentinel down-after-milliseconds mymaster 30000 指定多少毫秒之后,主节点没有应答哨兵,此时哨兵主观上认为主节点下线
sentinel auth-pass master设置了密码,连接master服务的密码
acllog-max-len 128 ACL日志的最大条目长度。
sentinel parallel-syncs mymaster 1 表示允许并行同步的slave个数,当Master挂了后,哨兵会选出新的Master,此时,剩余的slave会向新的master发起同步数据
sentinel failover-timeout mymaster 180000 故障转移的超时时间,进行故障转移时,如果超过设置的毫秒,表示故障转移失败
vi /opt/coisini/redis-cluster/redis6380/sentinel.conf
编辑以下选项:
(按Esc退出编辑模式下:
/protected-mode
即可搜索,回车后按n
可以跳转搜索下一个,:set number
可以显示行号 )
# 哨兵sentinel实例运行的端口
protected-mode no
# 端口
port 26380
# 保护模式
daemonize yes
pidfile /opt/coisini/redis-cluster/redis6380/redis-sentinel6380.pid
logfile /opt/coisini/redis-cluster/redis6380/redis-sentinel6380.log
dir /opt/coisini/redis-cluster/redis6380/tmp
# 哨兵监听的主服务器,只需要2票就可以从机变主机
sentinel monitor mymaster 192.168.31.16 6380 2
# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd 设置主机密码
sentinel auth-pass mymaster 123456
# 设置未得到主机响应时间,5秒未响应视为宕机
sentinel down-after-milliseconds mymaster 5000
# 设置等待主活动时间,10秒主机未活动,则重新选举主机
sentinel failover-timeout mymaster 10000
# 设置连接密码
sentinel auth-pass mymaster 123456
然后分别修改另外两个配置,注意修改端口
vi /opt/coisini/redis-cluster/redis6381/sentinel.conf
vi /opt/coisini/redis-cluster/redis6382/sentinel.conf
除了这个指定主机地址不变
sentinel monitor mymaster 192.168.31.16 6380 2
11. 编辑 Redis Sentinel 启动脚本:
vi redis-sentinel-cluster.sh
#!/bin/bash# Redis集群节点端口列表
ports=(6380 6381 6382)# Redis Sentinel节点的端口列表
sentinel_ports=(26380 26381 26382)# Redis的安装路径
redis_path="/opt/coisini/redis-cluster"# Redis的密码
password="123456"# 启动函数
start() {echo "Starting Redis Sentinel cluster..."for port in "${sentinel_ports[@]}"; doecho "Starting Redis Sentinel on port $port..."$redis_path/redis$((port-20000))/src/redis-server $redis_path/redis$((port-20000))/sentinel.conf --sentinel --daemonize yesdoneecho "Redis Sentinel cluster started."
}# 停止函数
stop() {echo "Stopping Redis Sentinel cluster..."for port in "${sentinel_ports[@]}"; doecho "Stopping Redis Sentinel on port $port..."$redis_path/redis$((port-20000))/src/redis-cli -a $password -p $port shutdowndoneecho "Redis Sentinel cluster stopped."
}# 重启函数
restart() {stopstart
}# 脚本的主逻辑
case "$1" instart)start;;stop)stop;;restart)restart;;*)echo "Usage: $0 {start|stop|restart}"exit 1
esac
文件赋权:
chmod +x redis-sentinel-cluster.sh
12. 先启动Redis
/opt/coisini/redis-cluster/redis-cluster.sh start
停止、重启命令:
/opt/coisini/redis-cluster/redis-cluster.sh stop
/opt/coisini/redis-cluster/redis-cluster.sh restart
13. 查看启动日志:
cat /opt/coisini/redis-cluster/redis6380/redis6380.log
从机连接成功
cat /opt/coisini/redis-cluster/redis6380/redis6381.log
连接主机成功
14. 再启动Redis Sentinel监听主节点的状态
/opt/coisini/redis-cluster/redis-sentinel-cluster.sh start
停止、重启命令:
/opt/coisini/redis-cluster/redis-sentinel-cluster.sh stop
/opt/coisini/redis-cluster/redis-sentinel-cluster.sh restart
15. 查询主从关系
info replication
主机视角,两个从机
从机视角,一个主机
16. 进入主机:
/opt/coisini/redis-cluster/redis6380/src/redis-cli -p 6380 -a 123456set k1 coisini
其它从机会同步数据
17. 我们假设主机宕机:
查看
ps aux | grep redis
结束掉6380进程,
kill 8362
查看主从情况:
info replication
选举 已经从8360转到8362了
接下来我们重启6380机器:
/opt/coisini/redis-cluster/redis6380/src/redis-server /opt/coisini/redis-cluster/redis6380/redis.conf
查看主从情况:
info replication
6380已经变为从机
18. 设置服务开机自启
sudo vi /etc/systemd/system/redis-cluster.service
添加以下内容:
[Unit]
Description=Redis Cluster Service
After=network.target[Service]
Type=forking
ExecStart=/opt/coisini/redis-cluster/redis-cluster.sh start
ExecStop=/opt/coisini/redis-cluster/redis-cluster.sh stop
ExecReload=/opt/coisini/redis-cluster/redis-cluster.sh restart
User=redis
Group=redis
Restart=on-failure[Install]
WantedBy=multi-user.target
sudo vi /etc/systemd/system/redis-sentinel-cluster.service
添加以下内容:
redis-sentinel-cluster.service 服务在redis-cluster.service启动完成之后才启动
[Unit]
Description=Redis Sentinel Cluster Service
After=redis-cluster.service[Service]
Type=forking
ExecStart=/opt/coisini/redis-cluster/redis-sentinel-cluster.sh start
ExecStop=/opt/coisini/redis-cluster/redis-sentinel-cluster.sh stop
ExecReload=/opt/coisini/redis-cluster/redis-sentinel-cluster.sh restart
User=redis
Group=redis
Restart=on-failure[Install]
WantedBy=multi-user.target
重新加载 systemd 配置
sudo systemctl daemon-reload
设置 redis-cluster.service 和 redis-sentinel-cluster.service 开机自启
sudo systemctl enable redis-cluster.service
sudo systemctl enable redis-sentinel-cluster.service
可以 reboot 重启验证
相关命令:
启动 Redis Cluster:sudo systemctl start redis-cluster.service
启动 Redis Sentinel Cluster(会在 Redis Cluster 启动成功后运行)
sudo systemctl start redis-sentinel-cluster.service
查看服务状态
sudo systemctl status redis-cluster.service
sudo systemctl status redis-sentinel-cluster.service