redis集群实现在centos8简单部署命令

一、安装部署

1.安装redis执行环境Redis是基于c语言编写的需要安装依赖,需要安装gcc:

 yum install gcc-c++

查看gcc版本命令:

 gcc -v


2.进入redis解压后的目录执行命令指定目录 $Redis_Home=/root/dev/redis/redis(不指定目录默认在$Redis_Home=/usr/local/bin下)

make PREFIX=/root/dev/redis/redis install

3.配置集群redis.conf文件 可以是$Redis_Home目录下的也可是自定义conf文件只要服务启动这个文件即可

示例一1.主从集群redis.conf配置   master配置

#主从集群conf
# 监听ip,多个ip用空格分隔  自己机器的ip
bind 192.168.1.120
# 允许后台启动
daemonize yes
# 日志路径
logfile "/root/dev/redis/redis/log/redis.log"
# 数据库备份文件存放目录
dir /root/dev/redis/redis/data
# slave连接master密码,master可省略      
masterauth 123456
# 设置master连接密码,slave可省略      
requirepass 123456
 # 在/root/dev/redis/redis/data 目录生成appendonly.aof文件        
appendonly yes

slave配置另外一个机器的redis.conf

# 监听ip,多个ip用空格分隔自己机器的ip
bind 192.168.1.121
# 允许后台启动
daemonize yes
# 日志路径
logfile "/root/dev/redis/redis/log/redis.log"
# 数据库备份文件存放目录
dir /root/dev/redis/redis/data             
#  replicaof用于追随某个节点的redis,被追随的节点为主节点,追随的为从节点。就是设置master节点
replicaof 192.168.1.120 6379
# slave连接master密码,master可省
masterauth 123456
# 设置master连接密码,slave可省略
requirepass 123456
appendonly yes

示例二哨兵模式是基于主从模式的当master挂掉后有slave当选master节点
1.启动示例一的主从集群全部启动完
2.配置sentinel.conf文件  红颜色根据实际情况配置

#当前机器的ip地址

bind 192.168.1.121

#可以是没有用过的端口
port 26379
#哨兵的启动模式,yes是后台启动
daemonize yes
#哨兵的pid文件存放位置
pidfile /var/run/redis-sentinel.pid
#哨兵的日志文件存放位置
logfile "/root/dev/redis/redis/log/redis-sentinel.log"
#哨兵进程的工作目录,默认就是/tmp
dir /tmp
#哨兵监听的master数据库,node01是为主数据库起的名称,可以随便起个名字后面是master的ip和端口
# 判断master失效至少需要2个sentinel同意,建议设置为n/2+1,n为sentinel个数
sentinel monitor node01 192.168.1.121 6379 3
#配置master的登陆密码,node01是你配置的master名称
sentinel auth-pass node01 123456
#30秒内master无响应则认为master挂掉
sentinel down-after-milliseconds node01 30000
#保持默认即可
acllog-max-len 128
#master重新选举之后,其它节点能同时并行进行数据同步的台数有多少台
#显然该值越大,则所有slave能同步完成的速度越快,但如果此时刚好有人访问slave数据,可能造成读取失败,最保守的值建议设为1
#即同一时间只能有一台进行数据同步,这样其它slave还能继续提供服务,但是所有的slave数据同步完成就会显得缓慢。
sentinel parallel-syncs node01 2
#故障转移超时时间,指在该时间内如果故障转移没有成功,则会再发起一次故障转移
sentinel failover-timeout node01 180000
#保持默认即可
sentinel deny-scripts-reconfig yes
SENTINEL resolve-hostnames no
SENTINEL announce-hostnames no

3.启动哨兵模式$Redis_Home/bin目录下,   ../sentinel.conf  根据实际情况配置路径

redis-sentinel  ../sentinel.conf 

4.查看详情命令$Redis_Home/bin目录下

redis-cli -h 192.168.1.121 -p 26379

192.168.1.120:26379>SENTINEL master node01
192.168.1.120:26379>SENTINEL REPLICAS node01

示例三Cluster(集群)模式redis.conf配置 红颜色表示根据自己实际修改同台机器文件名,端口号要不同

#注:所有节点配置文件全部修改切记需要修改的ip、端口、pid文件...避免冲突。确保所有机器都修改
#每个实例的配置文件修改为对应节点的ip地址
bind 192.168.1.120
#监听端口,运行多个实例时,需要指定规划的每个实例不同的端口号
port 7000
#redis后台运行 
daemonize yes
#pid文件,运行多个实例时,需要指定不同的pid文件
pidfile /var/run/redis_7000.pid
#日志文件位置,运行多实例时,需要将文件修改的不同。
logfile /root/dev/redis/redis/log/redis_7000.log
#存放数据的目录
dir /root/dev/redis/redis/data
#开启AOF持久化,redis会把所接收到的每一次写操作请求都追加到appendonly.aof文件中,当redis重新启动时会从该文件恢复出之前的状态。
appendonly yes

#设置密码  要设置密码不然redis默认是protected(保护模式)没有密码创建不了集群或者关掉protected(保护模式)也可不要密码
masterauth "123456"
requirepass "123456"
#AOF文件名称
appendfilename "appendonly.aof"
#表示对写操作进行累积,每秒同步一次
appendfsync everysec
#以下为打开注释并修改
#启用集群
cluster-enabled yes
#集群配置文件,由redis自动更新,不需要手动配置,运行多实例时请注修改
cluster-config-file nodes-7000.conf
#单位毫秒。集群节点超时时间,即集群中主从节点断开连接时间阈值,超过该值则认为主节点不可以,从节点将有可能转为master
#在进行故障转移的时候全部slave都会请求申请为master,但是有些slave可能与master断开连接一段时间了导致数据过于陈旧,不应该被提升为master。该参数就是用来判断slave节点与master断线的时间是否过长。(计算方法为:cluster-node-timeout * cluster-replica-validity-factor,此处为:5000 * 10 毫秒)
cluster-node-timeout 5000
cluster-replica-validity-factor 10
#一个主机将保持连接的最小数量的从机,以便另一个从机迁移到不再被任何从机覆盖的主机
cluster-migration-barrier 1
#集群中的所有slot(16384个)全部覆盖,才能提供服务
cluster-require-full-coverage yes

集群命令$Redis_Home/bin目录下,  四台机器   每台启动两个端口号  一主一从

#需要创建集群,创建集群

# –cluster-replicas 1: 表示集群的一个主节点有1个从节点,就是一主一从模式

redis-cli -a 123456 --cluster create --cluster-replicas 1 192.168.37.120:7000 192.168.1.120:7001 192.168.1.121:7000 192.168.1.121:7001 192.168.1.122:7000 192.168.1.122:7001 192.168.1.123:7000 192.168.1.123:7001

#登录集群

redis-cli -c -h 192.168.1.120 -p 7001

#集群信息

redis-cli -c -h 192.168.182.129 -p 7001
192.168.1.120:7001> auth 123456
192.168.1.120:7001> CLUSTER INFO
192.168.1.120:7001> CLUSTER NODES

4.进入$Redis_Home/bin目录启动服务../redis.conf这里是相对路径,用绝对路径也可

redis-server  ../redis.conf


5.在$Redis_Home/bin目录下启动redis客户端数据库 红色这个是默认redis.conf配置如果更改需要更换

redis-cli -h 127.0.0.1 -p 6379

6.查看集群信息


查看集群信息
# 交互式
redis-cli -h 192.168.1.120 -a 123456
192.168.1.120:6379> info replication

# 交互式
redis-cli -h 192.168.1.120
192.168.1.120:6379> auth 123456
192.168.1.120:6379> info replication

# 非交互式
redis-cli -h 192.168.1.120 -a 123456 info replication


7.关闭redis客户端命令

 SHUTDOWN
 exit

二、错误解决方法

1.当启动redis客户端(redis-cli )报错[ERR] Node 192.168.37.128:7000 DENIED Redis is running in protected mode because protected mode is enabled and no password is set for the default user. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Set up an authentication password for the default user. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.  就是protected保护模式默认开启了但是redis.conf启动没有配置密码

#解决就是redis.conf开启密码或者关闭保护模式

#设置密码
masterauth "123456"
requirepass "123456"

#关闭保护模式

protected-mode no

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值