一、安装单机版redis
在线安装gcc gcc-c++
yum install gcc
yum install gcc-c++
redis压缩包 提取码:2t72
下载好了压缩包 上传到我们要安装的目录中
- 压缩包上传到我们的服务器
- 或者在线下载redis包
wget http://download.redis.io/releases/redis-5.0.4.tar.gz
###这个命令解压这个压缩包
tar -zxf redis-5.0.4.tar.gz
- 编译当前压缩包 进入解压的压缩包中
###进入解压完的目录中 编译当前的目录
[root@localhost redis-5.0.4]# make
- 安装 redis 以及要按照的位置
[root@localhost redis-5.0.4]# make PREFIX=/optone/redis install
也可以直接 make 默认安装 /usr/local下面
PREFIX :表示你要安装的目录
- 启动redis
进入/optone/redis 目录下 启动redis
[root@localhost bin]# ./redis-server
redis启动成功,但是这种启动方式需要一直打开窗口,不能进行其他操作,不太方便。
二、redis读取配置文件启动
- 把这个文件拷贝到 我们安装 redis的目录下
- 修改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 #注释掉绑定本机,即可以远程连接访问
- 使用配置文件启动 redis
[root@localhost bin]# ./redis-server ../redis.conf
三、配置服务启动redis
- 创建目录
- 将修改好的配置文件复制到指定目录下
- 将启动脚本复制到/etc/init.d目录下,启动脚本在解压目录utils下
4.修改复制后的配置文件
4. 运行我们所配置的东西
四、搭建redis主从复制
-
首先必须保证每个机器上面都按照了redis ,如果没有安装 照着上面的步骤安装就好了
-
搭建redis主从复制 我们只需要来修改 redis.conf文件里面的内容就好了
-
下面这是我的机器
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
-
配置成功 挨个启动 查看是否搭建成功
-
连接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模式,也就是保护模式,保护模式只允许本地访问