centos搭建Redis单机安装以及主从复制和集群

一、安装单机版redis

在线安装gcc gcc-c++

yum install gcc

yum install gcc-c++

redis压缩包 提取码:2t72

下载好了压缩包 上传到我们要安装的目录中

  1. 压缩包上传到我们的服务器
  2. 或者在线下载redis包
wget http://download.redis.io/releases/redis-5.0.4.tar.gz
###这个命令解压这个压缩包
tar -zxf redis-5.0.4.tar.gz
  1. 编译当前压缩包 进入解压的压缩包中
###进入解压完的目录中 编译当前的目录
[root@localhost redis-5.0.4]# make 
  1. 安装 redis 以及要按照的位置
[root@localhost redis-5.0.4]# make PREFIX=/optone/redis install

也可以直接 make 默认安装 /usr/local下面

PREFIX :表示你要安装的目录

  1. 启动redis
    进入/optone/redis 目录下 启动redis
[root@localhost bin]# ./redis-server

在这里插入图片描述
redis启动成功,但是这种启动方式需要一直打开窗口,不能进行其他操作,不太方便。

二、redis读取配置文件启动

  1. 把这个文件拷贝到 我们安装 redis的目录下

在这里插入图片描述

  1. 修改redis.conf文件

daemonize yes #设置后台启动 no 改为yes
在这里插入图片描述

修改peotected-mode yes
改为:protected-mode no.
#protected-mode参数是为了禁止外网访问redis,如果启用了,则只能够通过localhost ip (127.0.0.1)访问Redis
在这里插入图片描述

requirepass 密码 #配置密码
在这里插入图片描述
bind 127.0.0.1 #注释掉绑定本机,即可以远程连接访问
在这里插入图片描述

  1. 使用配置文件启动 redis

[root@localhost bin]# ./redis-server ../redis.conf
在这里插入图片描述

三、配置服务启动redis

  1. 创建目录
    在这里插入图片描述
  2. 将修改好的配置文件复制到指定目录下
    在这里插入图片描述
  3. 将启动脚本复制到/etc/init.d目录下,启动脚本在解压目录utils下
    在这里插入图片描述
    4.修改复制后的配置文件

在这里插入图片描述
在这里插入图片描述
4. 运行我们所配置的东西
在这里插入图片描述
在这里插入图片描述

四、搭建redis主从复制

  1. 首先必须保证每个机器上面都按照了redis ,如果没有安装 照着上面的步骤安装就好了

  2. 搭建redis主从复制 我们只需要来修改 redis.conf文件里面的内容就好了

  3. 下面这是我的机器

    192.168.34.128 主节点
    redis.conf里面的内容

protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile “redis_6379.log”
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump-6379.rdb
dir /opt/redis/redis/data
requirepass 123
masterauth 123
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename “appendonly.aof”
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events “”
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

192.168.34.129 从节点

protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile “redis_6379.log”
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump-6379.rdb
dir /opt/redis/redis/data
requirepass 123
replicaof 192.168.34.128 6379
masterauth 123
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename “appendonly.aof”
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events “”
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

192.168.34.130 从节点

protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile “redis_6379.log”
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump-6379.rdb
dir /opt/redis/redis/data
requirepass 123
replicaof192.168.34.128 6379
masterauth 123
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename “appendonly.aof”
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events “”
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

注意配置文件中的这个

设置你的主节点是谁

replicaof 192.168.34.128 6379
masterauth 123

  1. 配置成功 挨个启动 查看是否搭建成功

  2. 连接Redis的客户端
    192.168.34.128的节点
    在这里插入图片描述
    其他机器的节点
    在这里插入图片描述
    搭建成功

五、搭建RedisSentinel

1.在Redis/bin目录中创建sentinel.conf 创建完成写入下面内容

#Base
#保护模式(线上的话去参考百度)
protected-mode no
#哨兵的端口
port 26379
#是否以后台方式启动
daemonize yes
#设置pid所在的目录
pidfile /var/run/redis-sentinel.pid
#执行哨兵日志 所放在的目录
logfile "redis-sentinel.log"
#sentinel工作的空间
dir "/opt/redis/redis/data"

#Core
#监控你redis的名称 然后就是ip 和redis的端口号(监控你的redis) 最后一个是发现你宕机的票数
sentinel monitor mymaster 192.168.52.128 6379 2
#你redis的名称以及密码
sentinel auth-pass mymaster 123
#哨兵认为你宕机的时间段 就是哨兵想你ping超时的时间那么就认为你宕机了
sentinel down-after-milliseconds mymaster 10000
#和sale的同步数量的数量 设置1个就是一个一个同步
sentinel parallel-syncs mymaster 1
#故障转移的超时时间
sentinel failover-timeout mymaster 180000

把这个文件分别传输到另外两个机器上 然后启动redis-sentinel

启动命令 ./redis-sentinel sentinel.conf

2.查看sentinel是否配置成功

2.1查看主节点的redis进程

在这里插入图片描述
杀掉1917的进程 kill -9 1917 等10秒钟

再次进入客户端查看 info replication

192.168.34.130的变成了主节点
在这里插入图片描述

五、搭建Redis-Cluster集群

敬请期待

六、常见问题

1.只能本机连接redis,别的机器连接不上 原因:当前使用的Protrcted mode模式,也就是保护模式,保护模式只允许本地访问

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

往日时光--

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值