25人参与 • 2026-01-20 • Redis
redis是一款高性能的键值存储系统,支持多种数据结构,常用于缓存、消息队列、分布式锁等场景。根据不同的可用性、扩展性与性能需求,redis提供了四种常见部署模式:
下面将逐步讲解每种部署方式的安装、配置与操作。
# 检查gcc是否安装 gcc --version # 安装gcc yum install gcc -y # centos apt install gcc -y # ubuntu
systemctl stop firewalld.service systemctl disable firewalld # 或使用firewall-cmd放行相应端口
mkdir -p /opt/software/redis cd /opt/software/redis wget https://download.redis.io/redis-stable.tar.gz tar -xzf redis-stable.tar.gz cd redis-stable make install
ll /usr/local/bin/redis-*
应看到以下关键文件:
redis-server:服务端redis-cli:客户端redis-sentinel:哨兵redis-benchmark:性能测试工具编辑 redis.conf:
vim redis.conf
关键修改:
bind * -::* # 允许所有ip连接 protected-mode no # 关闭保护模式 daemonize yes # 后台运行 logfile /opt/software/redis/redis-stable/redis.log dir /opt/software/redis requirepass 1qaz@wsx # 设置密码(可选)
启动服务:
redis-server redis.conf redis-cli -a 1qaz@wsx # 带密码连接
keys * # 查看所有键 auth 密码 # 认证 quit # 退出 redis-cli shutdown # 关闭服务
在从节点的 redis.conf 中添加:
replicaof 192.168.75.129 6379 # 主节点ip与端口 masterauth 1qaz@wsx # 如果主节点有密码
在主节点执行:
redis-cli -a 1qaz@wsx info replication
输出中应看到 connected_slaves 数量及从节点信息。
编辑 sentinel.conf:
protected-mode no port 26379 daemonize yes logfile /opt/software/redis/redis-stable/sentinel.log dir /opt/software/redis sentinel monitor mymaster 192.168.75.129 6379 2 sentinel down-after-milliseconds mymaster 30000 sentinel failover-timeout mymaster 180000
redis-sentinel sentinel.conf
redis-cli -p 26379 info sentinel
每台机器配置两个节点(6379主,6380从)。
配置文件示例 redis_6379.conf:
bind * -::* protected-mode no port 6379 daemonize yes cluster-enabled yes cluster-node-timeout 5000 dir /opt/software/redis/cluster appendonly yes logfile "/opt/software/redis/redis-stable/cluster/redis6379.log" cluster-config-file nodes-6379.conf
# 启动节点 redis-server ./cluster/redis_6379.conf redis-server ./cluster/redis_6380.conf # 创建集群 redis-cli --cluster create \ --cluster-replicas 1 \ 192.168.75.129:6379 192.168.75.129:6380 \ 192.168.75.131:6379 192.168.75.131:6380 \ 192.168.75.132:6379 192.168.75.132:6380
redis-cli cluster info # 集群信息 redis-cli cluster nodes # 节点列表 redis-cli -c # 开启集群模式连接
停止某一主节点,观察其从节点是否自动提升为主节点,并重新加入集群。
/opt/software/redis/redis-stable/redis.conf/opt/software/redis/redis-stable/sentinel.conf/opt/software/redis/redis-stable/cluster/redis_{port}.conf# 服务管理 redis-server xxx.conf redis-cli shutdown ps aux | grep redis # 集群操作 redis-cli --cluster create ... redis-cli cluster nodes redis-cli -c # 数据操作 keys * set/get/del auth 密码 info replication
| 部署方式 | 适用场景 | 优点 | 缺点 |
|---|---|---|---|
| 单机 | 开发、测试 | 简单快捷 | 无高可用,性能受限 |
| 主从 | 读多写少,数据备份 | 读写分离,数据冗余 | 故障需人工干预 |
| 哨兵 | 高可用,自动故障转移 | 自动切换,客户端透明 | 配置复杂,数据可能丢失 |
| 集群 | 大数据量,高并发,高可用 | 数据分片,水平扩展 | 部署复杂,运维成本高 |
maxmemory 和淘汰策略redis的部署方式多样,选择适合业务场景的方案至关重要:
到此这篇关于redis 7.x 部署指南:单机、主从、哨兵、集群的文章就介绍到这了,更多相关redis单机、主从、哨兵、集群内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论