一.Redis安装方法
1.第一种(安装完整RPM源)
1.安装完整的RPM源(第一种安装的是最新版本)
# yum install -y http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
2.在线安装最新版本redis:(网速不好,下载特别慢)
# yum --enablerepo=remi install redis -y
3.查看redis-server
# which redis-server
4.启动
# redis systemctl start redis
5.查看进程
# ps -ef| grep redis
6.卸载redis
# yum remove redis
2.第二种(离线包安装)
2.1.规划
角色 |
机器地址 |
Port |
主 |
192.168.2.99
|
10000 |
1.安装依赖:
# yum install gcc-c++
2.创建redis目录
# mkdir /redis
3.安装包上传到虚拟机/redis目录下
4.解压软件包
# tar -xf redis-5.0.3.tar.gz
5.进入解压目录
# cd /redis/redis-5.0.3
6.在当前目录下进行编译
# make
7.将redis服务安装到指定目录
# make install PREFIX=/usr/local/redis
8.创建配置文件目录
# mkdir -p /redis/redisconf
# mkdir -p /redis/log
# mkdir -p /redis/data
9.移动配置文件到该目录
# mv /redis/redis-5.0.3/redis.conf /redis/redisconf/
10.启动
# redis /usr/local/redis/bin/redis-server /redis/redisconf/redis.conf
11.修改配置文件(注意:这里利用底行模式 “:/单词” 进行查找修改,若没有就添加)
# vi /redis/redisconf/redis.conf
daemonize yes 是否启动进程
port 10000 端口
bind 192.168.2.99 机器地址
protected-mode no 是否开启保护模式
logfile "/redis/log/redis.log" 指定了纪录日志的文件
dir "/redis/data" 指定持久化数据文件位置
12.再次启动redis
# /usr/local/redis/bin/redis-server /redis/redisconf/redis.conf
13.查看redis进程
# ps -ef| grep redis
14.将redis服务加入到环境变量
# vi /etc/profile
PATH=$PATH:/usr/local/redis/bin export PATH
15.加载环境变量:
# source /etc/profile
16.启动redis
# redis-cli -p 10000
二.Redis主从搭建
2.1.规划
角色 |
机器地址 |
端口 |
主 |
192.168.2.99 |
10000 |
从 |
192.168.2.99 |
10001 |
2.2.搭建
1.创建从服务目录
# mkdir -p /slaveredis/data /slaveredis/log /slaveredis/conf
2.拷贝配置文件
# cp /redis/redisconf/redis.conf /slaveredis/conf/slaveredis.conf
3.修改配置文件(注意:这里利用底行模式 “:/单词” 进行查找修改,若没有就添加)
# vi /slaveredis/conf/slaveredis.conf
daemonize yes
dir /slaveredis/data
logfile /slaveredis/log/slaveredis.log
bind 192.168.2.99
port 10001
slaveof 192.168.2.99 10000
4.启动服务
# redis-server /slaveredis/conf/slaveredis.conf
5.进入slaver-redis验证关系
# redis-cli -h 192.168.2.99 -p 10001
6.验证关系(从服务)
> info replication
7. 验证是否同步(主库)
>redis-cli -h 192.168.2.99 -p 10000
>set lwc 985
>quit
备库
>redis-cli -h 192.168.2.99 -p 10001
>get lwc # 如出现了主库的列值985即搭建成功
三.哨兵集群搭建
3.1.规划
角色 |
机器地址 |
端口 |
主 |
192.168.2.99 |
7000 |
从1 |
192.168.2.99 |
7001 |
从2 |
192.168.2.99 |
7002 |
主哨兵1 |
192.168.2.99 |
25000 |
哨兵2 |
192.168.2.99 |
7001 |
哨兵3 |
192.168.2.99 |
7002 |
1.搭建MASTER REDIS
# mkdir -p /set/master /set/slave1 /set/slave2 /set/sentinel-1 /set/sentinel-2 /set/sentinel-3
2.编辑主库配置文件
# vim /set/master/master.conf
bind 192.168.2.99
daemonize yes
protected-mode no
port 7000
tcp-backlog 511
tcp-keepalive 300
pidfile "/set/master/redis_7000.pid"
loglevel notice
logfile "/set/master/redis.log"
databases 16
always-show-logo yes
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
dir /set/master
3.复制
# cp /set/master/master.conf /set/slave1/slave1.conf
# cp /set/master/master.conf /set/slave2/slave2.conf
4.批量改配置
# sed -i "s/7000/7001/g" /set/slave1/slave1.conf
# sed -i "s/7000/7002/g" /set/slave2/slave2.conf
# sed -i "s/master/slave1/g" /set/slave1/slave1.conf
# sed -i "s/master/slave2/g" /set/slave2/slave2.conf
5.启动redis
# redis-server /set/master/master.conf
# redis-server /set/slave1/slave1.conf
# redis-server /set/slave2/slave2.conf
6.配置一主两从(哨兵)
# redis-cli -h 192.168.2.99 -p 7001 # 登录哨兵一
> slaveof 192.168.2.99 7000 # 选7000端口的哨兵为master监控的哨兵
> quit
# redis-cli -h 192.168.2.99 -p 7002 # 登录哨兵二
> slaveof 192.168.2.99 7000 # 选7000端口的哨兵为master监控的哨兵
> quit
7.配置主哨兵文件
# vim /set/sentinel-1/sentinel.conf
daemonize yes
dir /set/sentinel-1
port 25000
sentinel monitor mymaster 192.168.2.99 7000 2 有两个哨兵
# cp /set/sentinel-1/sentinel.conf /set/sentinel-2/ # 复制
# cp /set/sentinel-1/sentinel.conf /set/sentinel-3/
# sed -i "s/25000/25001/g" /set/sentinel-2/sentinel.conf # 批量修改
# sed -i "s/25000/25002/g" /set/sentinel-3/sentinel.conf
8启动哨兵
# redis-sentinel /set/sentinel-1/sentinel.conf
# redis-sentinel /set/sentinel-2/sentinel.conf
# redis-sentinel /set/sentinel-3/sentinel.conf
9登录主哨兵
# redis-cli -p 25000
> info sentinel 查看信息
10.验证(主机宕机后是否选出slave作为master主机)
# kill -9 主机ip