redis
yum install -y gcc-c++ yum install -y wget
第二步:下载并解压缩 Redis 源码压缩包
wget http://download.redis.io/releases/redis-5.0.5.tar.gz tar -zxf redis-5.0.5.tar.gz
cd redis-5.0.5/src make
mkdir /usr/redis -p
make install PREFIX=/usr/redis
前端启动:./redis-server
前端关闭:ctrl+c
cp redis.conf /usr/redis/bin/
# 将`daemonize`由`no`改为`yes` 后台可以运行 daemonize yes # 默认绑定的是回环地址,默认不能被其他机器访问 # bind 127.0.0.1 # 是否开启保护模式,由yes该为no protected-mode no
systemctl stop firewalld(默认) systemctl disable firewalld.service(设置开启不启动)
主从复制
删除目录:rm -rf redis-master
创建主从的目录(在/root/redis下):mkdir redis-master redis-slaver1 redis-slaver2
将安装好的redis文件安装到master下:cd /root/redis/redis-5.0.5/src/
make install prefix=/root/redis/redis-master/
将redis配置文件复制到master下:cp /root/redis/redis-5.0.5/redis.conf /root/redis/redis-master/bin/
将master下的信息复制到slaver:cp -r /root/redis/redis-master/* /root/redis/redis-slaver1/
port 6380 replicaof 127.0.0.1 6379(与master建立关联)
添加哨兵监控
创建3个哨兵的目录(在/root/redis下):mkdir redis-sentinel1 redis-sentinel2 redis-sentinel3
将master下的信息复制到sentinel:cp -r /root/redis/redis-master/* /root/redis/redis-sentinel1/
将redis配置文件复制到sentinel下:cp /root/redis/redis-5.0.5/sentinel.conf /root/redis/redis-sentinel1/bin/
修改sentinel.conf
port 26379 daemonize yes #监控的节点失联超过2就客观上认为主节点失联了 sentinel monitor mymaster 127.0.0.1 6379 2 # 指定多少毫秒之后 主节点没有应答哨兵sentinel 此时 哨兵主观上认为主节点下线 默认30秒,改成3 秒 sentinel down-after-milliseconds mymaster 3000
将sentinel下的信息复制到其他sentinel:cp -r /root/redis/redis-sentinel1/* /root/redis/redis-sentinel2/
启动服务:
#启动redis-master和redis-slaver ./redis-server redis.conf ./redis-server redis.conf ./redis-server redis.conf #启动redis-sentinel ./redis-sentinel sentinel.conf ./redis-sentinel sentinel.conf ./redis-sentinel sentinel.conf
------------恢复内容开始------------
yum install -y gcc-c++ yum install -y wget
第二步:下载并解压缩 Redis 源码压缩包
wget http://download.redis.io/releases/redis-5.0.5.tar.gz tar -zxf redis-5.0.5.tar.gz
cd redis-5.0.5/src make
mkdir /usr/redis -p
make install PREFIX=/usr/redis
前端启动:./redis-server
前端关闭:ctrl+c
cp redis.conf /usr/redis/bin/
# 将`daemonize`由`no`改为`yes` 后台可以运行 daemonize yes # 默认绑定的是回环地址,默认不能被其他机器访问 # bind 127.0.0.1 # 是否开启保护模式,由yes该为no protected-mode no
systemctl stop firewalld(默认) systemctl disable firewalld.service(设置开启不启动)
主从复制
删除目录:rm -rf redis-master
创建主从的目录(在/root/redis下):mkdir redis-master redis-slaver1 redis-slaver2
将安装好的redis文件安装到master下:cd /root/redis/redis-5.0.5/src/
make install PREFIX=/root/redis/redis-master/
将redis配置文件复制到master下:cp /root/redis/redis-5.0.5/redis.conf /root/redis/redis-master/bin/
将master下的信息复制到slaver:cp -r /root/redis/redis-master/* /root/redis/redis-slaver1/
port 6380 replicaof 127.0.0.1 6379(与master建立关联)
添加哨兵监控
创建3个哨兵的目录(在/root/redis下):mkdir redis-sentinel1 redis-sentinel2 redis-sentinel3
将master下的信息复制到sentinel:cp -r /root/redis/redis-master/* /root/redis/redis-sentinel1/
将redis配置文件复制到sentinel下:cp /root/redis/redis-5.0.5/sentinel.conf /root/redis/redis-sentinel1/bin/
修改sentinel.conf
port 26379 daemonize yes #监控的节点失联超过2就客观上认为主节点失联了 sentinel monitor mymaster 127.0.0.1 6379 2 # 指定多少毫秒之后 主节点没有应答哨兵sentinel 此时 哨兵主观上认为主节点下线 默认30秒,改成3 秒 sentinel down-after-milliseconds mymaster 3000
将sentinel下的信息复制到其他sentinel:cp -r /root/redis/redis-sentinel1/* /root/redis/redis-sentinel2/
启动服务:
#启动redis-master和redis-slaver ./redis-server redis.conf ./redis-server redis.conf ./redis-server redis.conf #启动redis-sentinel ./redis-sentinel sentinel.conf ./redis-sentinel sentinel.conf ./redis-sentinel sentinel.conf
redis集群搭建(三主三从)
创建集群目录:mkdir redis-cluster
创建集群文件:mkdir 7001 7002 7003 7004 7005 7006
将安装好的redis文件安装到cluster下:cd /root/redis/redis-5.0.5/src/
make install PREFIX=/root/redis/redis-cluster/7001
将redis配置文件复制到cluster下:cp /root/redis/redis-5.0.5/redis.conf /root/redis/redis-cluster/7001/bin/
修改配置文件:cluster-enabled yes (开启集群节点)
将7001下的信息复制到其他节点:
cp -r /root/redis/redis-cluster/7001/* /root/redis/redis-cluster/7002
cp -r /root/redis/redis-cluster/7001/* /root/redis/redis-cluster/7003
cp -r /root/redis/redis-cluster/7001/* /root/redis/redis-cluster/7004
cp -r /root/redis/redis-cluster/7001/* /root/redis/redis-cluster/7005
cp -r /root/redis/redis-cluster/7001/* /root/redis/redis-cluster/7006
创建启动脚本:vi start.sh
cd 7001/bin ./redis-server redis.conf cd .. cd .. cd 7002/bin ./redis-server redis.conf cd .. cd .. cd 7003/bin ./redis-server redis.conf cd .. cd .. cd 7004/bin ./redis-server redis.conf cd .. cd .. cd 7005/bin ./redis-server redis.conf cd .. cd .. cd 7006/bin ./redis-server redis.conf cd .. cd ..
给sh文件赋权:chmod u+x start.sh (赋写和执行的权限)
cluster-replicas 1(一主一从)
./redis-cli --cluster create 192.168.72.128:7001 192.168.72.128:7002 192.168.72.128:7003 192.168.72.128:7004 192.168.72.128:7005 192.168.72.128:7006 --cluster-replicas 1
查看分配的节点信息:cat nodes.conf
e055b33f3935d5cb9f8acc2bab4cdff41947e3e7 192.168.72.128:7006@17006 slave 07ce8717521cee7de7949515f50b619a71e73ff2 0 1649566187739 6 connected 32505fea96e3ab412ab6dbf35df1b74c822ffd06 192.168.72.128:7004@17004 slave 23d75d20a7b8dfa87c5adbe78b102416a7555266 0 1649566186000 4 connected 23d75d20a7b8dfa87c5adbe78b102416a7555266 192.168.72.128:7001@17001 myself,master - 0 1649566185000 1 connected 0-5460(槽的范围) 07ce8717521cee7de7949515f50b619a71e73ff2 192.168.72.128:7003@17003 master - 0 1649566188746 3 connected 10923-16383 5ddf0c12a80424f5daaef46fd088164bfb9f41df 192.168.72.128:7002@17002 master - 0 1649566187000 2 connected 5461-10922 dcc16840771a6c9a27fc2e085c367893b8343c9d 192.168.72.128:7005@17005 slave 5ddf0c12a80424f5daaef46fd088164bfb9f41df 0 1649566185724 5 connected
主7001从7004 主7003从7006 主7002从7005
在任意bin下以集群方式连接:./redis-cli -h 127.0.0.1 -p 7001 -c
扩容一个新的节点(空的):mkdir 7007
将安装好的redis文件安装到cluster下:cd /root/redis/redis-5.0.5/src/
make install PREFIX=/root/redis/redis-cluster/7007
将redis配置文件复制到cluster下:cp /root/redis/redis-5.0.5/redis.conf /root/redis/redis-cluster/7007/bin/
复制一个从节点:cp -r 7007 7008
启动7007,将7007加入到集群中,进入7007的bin下:
./redis-cli --cluster add-node 192.168.72.128:7007 192.168.72.128:7001
进入客户端查看节点信息:cluster nodes
重新分配槽:
./redis-cli --cluster reshard 192.168.72.128:7007
设置重新分配的槽数:3000
How many slots do you want to move (from 1 to 16384)? 3000
输入接收槽的结点id-->all-->yes
32505fea96e3ab412ab6dbf35df1b74c822ffd06 192.168.72.128:7004@17004 slave 23d75d20a7b8dfa87c5adbe78b102416a7555266 0 1649572665668 1 connected
5ddf0c12a80424f5daaef46fd088164bfb9f41df 192.168.72.128:7002@17002 master - 0 1649572665000 2 connected 6462-10922
dcc16840771a6c9a27fc2e085c367893b8343c9d 192.168.72.128:7005@17005 slave 5ddf0c12a80424f5daaef46fd088164bfb9f41df 0 1649572664000 2 connected
e055b33f3935d5cb9f8acc2bab4cdff41947e3e7 192.168.72.128:7006@17006 slave 07ce8717521cee7de7949515f50b619a71e73ff2 0 1649572665000 3 connected
68af1e781efee83645485dd78de3520e43640ccc 192.168.72.128:7007@17007 myself,master - 0 1649572662000 7 connected 0-998 5461-6461 10923-11921(被分了3份)
07ce8717521cee7de7949515f50b619a71e73ff2 192.168.72.128:7003@17003 master - 0 1649572666663 3 connected 11922-16383
23d75d20a7b8dfa87c5adbe78b102416a7555266 192.168.72.128:7001@17001 master - 0 1649572664657 1 connected 999-5460
启动7008,分配从节点:
./redis-cli --cluster add-node 新节点的ip和端口(7008) 旧节点ip和端口(7007) --cluster-slave --cluster-master-id 主节点id