2023年2月24日
Redis 集群部署
数据库 · Redis
在三台服务器上部署 Redis,用于分布式计算。
Redis源码编译安装:
安装必要的编译工具:
sudo yum install gcc make -y
sudo yum install gcc make -y
sudo yum install gcc make -y
上传 Redis 6.2.6源代码并解压:
# 上传 scp ./redis-6.2.6.tar.gz root@master:/opt/softs/ cd /opt/softs/ tar -zxvf redis-6.2.6.tar.gz mv redis-6.2.6 /opt/module
# 上传 scp ./redis-6.2.6.tar.gz root@master:/opt/softs/ cd /opt/softs/ tar -zxvf redis-6.2.6.tar.gz mv redis-6.2.6 /opt/module
# 上传 scp ./redis-6.2.6.tar.gz root@master:/opt/softs/ cd /opt/softs/ tar -zxvf redis-6.2.6.tar.gz mv redis-6.2.6 /opt/module
编译并安装Redis:
cd redis-6.2.6 make sudo make PREFIX=/usr/local/redis install
cd redis-6.2.6 make sudo make PREFIX=/usr/local/redis install
cd redis-6.2.6 make sudo make PREFIX=/usr/local/redis install
添加环境变量:
# 添加环境变量 # 编辑 etc/profile export REDIS_HOME=/usr/local/redis export PATH=$PATH:$REDIS_HOME/bin source /etc/profile
# 添加环境变量 # 编辑 etc/profile export REDIS_HOME=/usr/local/redis export PATH=$PATH:$REDIS_HOME/bin source /etc/profile
# 添加环境变量 # 编辑 etc/profile export REDIS_HOME=/usr/local/redis export PATH=$PATH:$REDIS_HOME/bin source /etc/profile
创建 Redis 相关目录:
# 创建目录 mkdir -p /data/redis/{conf,log,data}
# 创建目录 mkdir -p /data/redis/{conf,log,data}
# 创建目录 mkdir -p /data/redis/{conf,log,data}
复制配置文件并修改:
# 复制配置文件 cp /opt/module/redis-6.2.6/redis.conf /data/redis/conf/ # 修改配置文件 vi /data/redis/conf/redis.conf # 修改如下内容: bind ***.*.*.* # 注释此行 daemonize no # 修改为 daemonize yes protected-mode yes # 修改为 protected-mode no logfile "" # 修改为 logfile /data/redis/log/redis.log dir ./ # 修改为 dir /data/redis/data dbfilename dump.rdb # 修改为 dbfilename dump_6379.rdb appendfilename "appendonly.aof" # 修改为 appendfilename "appendonly_6379.aof" cluster-enabled yes # 取消注释 cluster-config-file nodes.conf # 取消注释 cluster-node-timeout 15000 # 取消注释 appendonly yes # 取消注释 改为yes
# 复制配置文件 cp /opt/module/redis-6.2.6/redis.conf /data/redis/conf/ # 修改配置文件 vi /data/redis/conf/redis.conf # 修改如下内容: bind ***.*.*.* # 注释此行 daemonize no # 修改为 daemonize yes protected-mode yes # 修改为 protected-mode no logfile "" # 修改为 logfile /data/redis/log/redis.log dir ./ # 修改为 dir /data/redis/data dbfilename dump.rdb # 修改为 dbfilename dump_6379.rdb appendfilename "appendonly.aof" # 修改为 appendfilename "appendonly_6379.aof" cluster-enabled yes # 取消注释 cluster-config-file nodes.conf # 取消注释 cluster-node-timeout 15000 # 取消注释 appendonly yes # 取消注释 改为yes
# 复制配置文件 cp /opt/module/redis-6.2.6/redis.conf /data/redis/conf/ # 修改配置文件 vi /data/redis/conf/redis.conf # 修改如下内容: bind ***.*.*.* # 注释此行 daemonize no # 修改为 daemonize yes protected-mode yes # 修改为 protected-mode no logfile "" # 修改为 logfile /data/redis/log/redis.log dir ./ # 修改为 dir /data/redis/data dbfilename dump.rdb # 修改为 dbfilename dump_6379.rdb appendfilename "appendonly.aof" # 修改为 appendfilename "appendonly_6379.aof" cluster-enabled yes # 取消注释 cluster-config-file nodes.conf # 取消注释 cluster-node-timeout 15000 # 取消注释 appendonly yes # 取消注释 改为yes
启动 Redis 服务:
# 启动 Redis 服务 redis-server /data/redis/conf/redis.conf
# 启动 Redis 服务 redis-server /data/redis/conf/redis.conf
# 启动 Redis 服务 redis-server /data/redis/conf/redis.conf
查看 Redis 服务状态:
# 查看 Redis 服务状态 ps -ef | grep redis
# 查看 Redis 服务状态 ps -ef | grep redis
# 查看 Redis 服务状态 ps -ef | grep redis
至此,Redis 6.2.6源码编译安装完成。
创建集群:
在任意一台机器上执行以下命令:
redis-cli --cluster create 192.168.25.10:6379 192.168.25.20:6379 192.168.25.30:6379 --cluster-replicas 0
redis-cli --cluster create 192.168.25.10:6379 192.168.25.20:6379 192.168.25.30:6379 --cluster-replicas 0
redis-cli --cluster create 192.168.25.10:6379 192.168.25.20:6379 192.168.25.30:6379 --cluster-replicas 0
按照提示输入yes,确认创建集群。
检查是否启动成功:
redis-cli --cluster info master:6379
redis-cli --cluster info master:6379
redis-cli --cluster info master:6379
master:6379 (fa0129cc...) -> 0 keys | 5461 slots | 0 slaves. 192.168.25.30:6379 (f072e7f8...) -> 0 keys | 5461 slots | 0 slaves. 192.168.25.20:6379 (13a70411...) -> 0 keys | 5462 slots | 0 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average
master:6379 (fa0129cc...) -> 0 keys | 5461 slots | 0 slaves. 192.168.25.30:6379 (f072e7f8...) -> 0 keys | 5461 slots | 0 slaves. 192.168.25.20:6379 (13a70411...) -> 0 keys | 5462 slots | 0 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average
master:6379 (fa0129cc...) -> 0 keys | 5461 slots | 0 slaves. 192.168.25.30:6379 (f072e7f8...) -> 0 keys | 5461 slots | 0 slaves. 192.168.25.20:6379 (13a70411...) -> 0 keys | 5462 slots | 0 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average