CentOS7单机和集群安装配置Redis教程

下载

以3.2.9版本为例:http://download.redis.io/releases/redis-3.2.9.tar.gz

单机

安装

  • 利用xftp上传到/usr/local,并进行解压
  • 下载gcc和tcl,否则后面会报错的(若已报错,则重新解压吧)
  • 编译安装测试
  • 创建日志、数据目录
[root@localhost local]# tar zxvf redis-3.2.9.tar.gz

[root@localhost local]# yum -y install gcc
[root@localhost local]# yum -y install tcl
// --------查看是否安装--------
[root@localhost redis-3.2.4]# rpm -qa|grep gcc
    gcc-4.8.5-36.el7_6.1.x86_64
    libgcc-4.8.5-36.el7_6.1.x86_64
[root@localhost local]# rpm -qa|grep tcl

// 编译安装测试
[root@localhost local]# cd redis-3.2.9
[root@localhost redis-3.2.9]# make && make test

// 创建数据和日志目录
[root@localhost ~]# mkdir -p /data/redis/data
[root@localhost ~]# mkdir -p /data/redis/logs

配置实例cache

[root@localhost ~]# mkdir /data/redis/data/cache
[root@localhost ~]# mkdir /usr/local/redis-3.2.9/conf
[root@localhost ~]# touch /usr/local/redis-3.2.9/conf/cache.conf
[root@localhost ~]# vim /usr/local/redis-3.2.9/conf/cache.conf

cache.conf写入下面数据(需要修改bind参数值为自己的ip)

# pid文件、日志文件
pidfile /data/redis/data/redis-cache.pid
logfile /data/redis/logs/redis-cache.log
# 本地数据库文件名
dbfilename cache-dump.rdb
# 本地数据库存放目录
dir /data/redis/data/cache
# 指定绑定的主机,请自行修改
bind 192.168.230.128
# 指定redis监听端口号
port 6601
#####################下面为优化项#####################
daemonize yes
maxclients 1000
#masterauth <master-password>
#requirepass <master-password>
tcp-backlog 511
timeout 0
tcp-keepalive 60
loglevel notice
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
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
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-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128

配置YDFIND实例

[root@localhost ~]# mkdir /data/redis/data/ydfind
[root@localhost ~]# touch /usr/local/redis-3.2.9/conf/ydfind.conf
[root@localhost ~]# vim /usr/local/redis-3.2.9/conf/ydfind.conf

ydfind.conf文件输入(优化参数项,把上面的复制下来)

pidfile /data/redis/data/redis-ydfind.pid
logfile /data/redis/logs/redis-ydfind.log
dbfilename ydfind-dump.rdb
dir /data/redis/data/ydfind
bind 192.168.230.128
# 指定redis监听端口号-----和上面的不一样
port 6603
# 主从同步时需要密码
masterauth 123456
# 登录密码
requirepass 123456
#####################下面为优化项---和上面一样#####################

启动

启动两个实例

[root@localhost ~]# /usr/local/redis-3.2.9/src/redis-server /usr/local/redis-3.2.9/conf/cache.conf
[root@localhost ~]# /usr/local/redis-3.2.9/src/redis-server /usr/local/redis-3.2.9/conf/ydfind.conf
[root@localhost ~]# 

验证

查看两个实例进程

[root@localhost ~]# ps -ef |grep redis
root      18095      1  0 01:43 ?        00:00:00 /usr/local/redis-3.2.9/src/redis-server 192.168.230.128:6601
root      18099      1  0 01:43 ?        00:00:00 /usr/local/redis-3.2.9/src/redis-server 192.168.230.128:6603
root      18103  18017  0 01:44 pts/0    00:00:00 grep --color=auto redis
[root@localhost ~]# 

命令验证,看输入info后能否看到信息,且ydfind是否需要密码连接

[root@localhost ~]# /usr/local/redis-3.2.9/src/redis-cli -h 192.168.230.128 -p 6601
192.168.230.128:6601> info
...                                                     // 输入info后看到信息,说明成功

[root@localhost ~]# /usr/local/redis-3.2.9/src/redis-cli -h 192.168.230.128 -p 6603
192.168.230.128:6603> info
NOAUTH Authentication required.  // 没有输入密码的缘故
192.168.230.128:6603> 
[root@localhost ~]# /usr/local/redis-3.2.9/src/redis-cli -h 192.168.230.128 -p 6603 -a 123456
192.168.230.128:6603> info
# Server
redis_version:3.2.9
......

集群

安装配置

1.三台服务器192.168.230.128、192.168.230.129、192.168.230.130,按上面单机先按照(注意上面配置文件中bind参数!!!),其中128服务器为master

2.修改两台从服务器129、130配置文件cache.conf、ydfind

vim /usr/local/redis-3.2.9/conf/cache.conf
vim /usr/local/redis-3.2.9/conf/ydfind.conf

加入

# slave配置:master的地址
slaveof 192.168.230.128 6601
# 注释掉masterauth
# masterauth abc123

3.ydfind实例一样
4.重启,查看状态

// 先关闭
[root@localhost ~]# cd /usr/local/redis-3.2.9/src
[root@localhost src]# ./redis-cli -h 192.168.230.130 -p 6601 shutdown
[root@localhost src]# ./redis-cli -h 192.168.230.130 -p 6603 -a 123456 shutdown
[root@localhost src]# ./redis-cli -h 192.168.230.129 -p 6601 shutdown
[root@localhost src]# ./redis-cli -h 192.168.230.129 -p 6603 -a 123456 shutdown

// 再启动
/usr/local/redis-3.2.9/src/redis-server /usr/local/redis-3.2.9/conf/cache.conf
/usr/local/redis-3.2.9/src/redis-server /usr/local/redis-3.2.9/conf/ydfind.conf

// 连接cache实例查看集群状态
[root@localhost src]# /usr/local/redis-3.2.9/src/redis-cli -h 192.168.230.128 -a 123456 -p 6601 info Replication
.......// 具体如下下图所示

在这里插入图片描述

哨兵模式

1.创建sentinel.conf并输入内容,下面以128服务器为例

[root@localhost src]# touch /usr/local/redis-3.2.9/conf/sentinel.conf
[root@localhost src]# vim /usr/local/redis-3.2.9/conf/sentinel.conf

port 26550
bind 192.168.230.128
dir "/data/redis/data/"
sentinel monitor cache 192.168.230.128 6601 2
sentinel down-after-milliseconds cache 30000
sentinel parallel-syncs cache 1
sentinel failover-timeout cache 18000

sentinel monitor ydfind 192.168.230.128 6603 2
sentinel down-after-milliseconds ydfind 30000
sentinel parallel-syncs ydfind 1
sentinel failover-timeout ydfind 18000
sentinel auth-pass ydfind 123456

参数解释

# 哨兵模式
port 26550
# 当前节点
bind 192.168.230.128
dir "/data/redis/data/"
# 当2个节点认为master失效时,才算失效
sentinel monitor cache 192.168.230.128 6601 2
# 超时未响应,认为master失效
sentinel down-after-milliseconds cache 30000
# fail over时对新master同步的slave个数,此值较小,意味集群故障转移期间,多个slave还在使用旧数据 
sentinel parallel-syncs cache 1
# fail over过期时间,fail over开始后,仍没有触发任何fail over操作,认为此次fail over失败
sentinel failover-timeout cache 18000

2.129及130服务器也同样设置,注意相应ip修改自己ip

bind 192.168.230.128

3.启动

[root@localhost ~]# nohup /usr/local/redis-3.2.9/src/redis-sentinel /usr/local/redis-3.2.9/conf/sentinel.conf >>/dev/null &
[1] 9074
[root@localhost ~]# nohup: 忽略输入重定向错误到标准输出端

[root@localhost ~]# netstat -anp | grep 26550
tcp        0      0 192.168.230.128:26550   0.0.0.0:*               LISTEN      9074/redis-sentinel

可以看到128服务器已经启动了,129、130两条服务器同样启动
4.验证

[root@localhost ~]# /usr/local/redis-3.2.9/src/redis-cli -h 192.168.230.128 -p 26550 info
....
# Sentinel
sentinel_masters:2
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=ydfind,status=ok,address=192.168.230.128:6603,slaves=0,sentinels=3
master1:name=cache,status=ok,address=192.168.230.128:6601,slaves=4,sentinels=3
[root@localhost ~]# 

slaves、sentinels比较奇怪,是不是bug待研究!!!

报错

1.Could not connect to Redis at 192.168.230.128:6601: No route to host

[root@localhost ~]# /usr/local/redis-3.2.9/src/redis-cli -h 192.168.230.128 -a 123456 -p 6601 info Replication
Could not connect to Redis at 192.168.230.128:6601: No route to host

检查下192.168.230.128 -p 6601中ip和端口是否正确

常用命令

  • 关闭实例:
[root@localhost src]# ./redis-cli -h 192.168.230.129 -p 6601 shutdown
[root@localhost src]# ./redis-cli -h 192.168.230.129 -p 6603 -a 123456 shutdown
  • 关闭redis:
[root@localhost redis-3.2.9]# ./src/redis-cli shutdown

配置文件-参数解释

# 基本参数
port 6601: 监听端口
bind 10.0.23.110:绑定主机
dir /data/redis/data/cache:本地数据库存放目录
dbfilename cache-dump.rdb:本地数据库文件名
logfile /data/redis/logs/redis-cache.log:日志文件
pidfile /data/redis/data/redis-cache.pid:pid文件
### 优化参数
daemonize yes       #启用守护进程
maxclients 1000     #设置同一时间最大客户端连接数,数字为0时默认无限制
#masterauth <master-password>       #slave服务连接master的密码
#requirepass <master-password>  #客户端连接redis密码
tcp-backlog 511     #TCP连接中已完成队列(完成三次握手之后)的长度,默认是511,
timeout 0       #当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能
tcp-keepalive 60        #开启长连接设置时间为60秒,为0时关闭长连接
loglevel notice     #设置日志级别notice适合生产环境使用
databases 16        #设定redis所允许的最大”db簇”的个数,默认为16个簇.
save 900 1  #设置快照触发点为900秒中至少有一个key的变更会处罚快照
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes 如果 redis 最后一次的后台保存失败,redis 将停止接受写操作,
这样以一种强硬的方式让用户知道数据不能正确的持久化到磁盘,否则就会没人注意到灾难的发生。
rdbcompression yes      #是否在 dump .rdb 数据库的时候使用 LZF 压缩字符串,默认都设为yes
rdbchecksum yes     #是否校验rdb文件
slave-serve-stale-data yes      #主从复制时让当前节点继续对客户端的请求返回消息,但返回的数据可能是过时或者数据可能是空的在第一次同步的时候会发生这种问题
slave-read-only yes     #可以在主服务器失败的时候,将从属服务器用作新的主服务器,从而实现无间断运行
repl-diskless-sync no       #不使用无磁盘方式同步
repl-diskless-sync-delay 5      #无磁盘diskless方式在进行数据传递之前会有一个时间的延迟,以便slave端能够进行到待传送的目标队列中,这个时间默认是5秒
repl-disable-tcp-nodelay no     是否启用TCP_NODELAY,如果启用则会使用少量的TCP包和带宽去进行数据传输到slave端,当然速度会比较慢;如果不启用则传输速度比较快,但是会占用比较多的带宽。 
slave-priority 100      #slave端的优先级设置,值是一个整数,数字越小表示优先级越高。当master故障时将会按照优先级来选择slave端进行恢复,如果值设置为0,则表示该slave永远不会被选择。
appendonly no       #启用AOF模式 
appendfilename "appendonly.aof"     #设置AOF记录的文件名
appendfsync everysec        #每秒钟刷写一次,折衷方法,所谓的redis可以只丢失1秒钟的数据就是源于此处 
no-appendfsync-on-rewrite no        #当主进程在进行向磁盘的写操作时,将会阻止其它的fsync调用
auto-aof-rewrite-percentage 100 #aof文件触发自动rewrite的百分比,值为0则表示禁用自动rewrite 
auto-aof-rewrite-min-size 64mb  # aof文件触发自动rewrite的最小文件size 
aof-load-truncated yes      #是否加载不完整的aof文件来进行启动
lua-time-limit 5000     #设置lua脚本的最大运行时间,单位为毫秒
slowlog-log-slower-than 10000       #告诉redis执行时间,这个时间是微秒级的(1秒=1000000微秒),这是为了不遗漏命令。
slowlog-max-len 128     #设置slowlog的长度,当一个新的命令被记录时,最旧的命令将会从命令记录队列中移除。
latency-monitor-threshold 0     #延迟监控,用于记录等于或超过了指定时间的操作,默认是关闭状态,即值为0。
notify-keyspace-events ""       #事件通知,默认不启用,具体参数查看配置文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值