redis

第一步:安装 C 语言需要的 GCC 环境
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
第三步:编译 Redis 源码,进入 redis-5.0.5 目录,执行编译命令(make)
cd redis-5.0.5/src 
make
第四步:安装 Redis ,需要通过 PREFIX 指定安装路径
mkdir /usr/redis -p
make install PREFIX=/usr/redis

前端启动:./redis-server

前端关闭:ctrl+c

后端启动(守护进程启动)
第一步:拷贝 redis-5.0.5/redis.conf 配置文件到 Redis 安装目录的 bin 目录
cp redis.conf /usr/redis/bin/  
第二步:修改 redis.conf   vi redis.conf
  
# 将`daemonize`由`no`改为`yes` 后台可以运行 
daemonize yes 
# 默认绑定的是回环地址,默认不能被其他机器访问 
# bind 127.0.0.1 
# 是否开启保护模式,由yes该为no
protected-mode no
第三步:启动服务 
./redis-server redis.conf
后端启动的关闭方式
./redis-cli shutdown
Redis命令行客户端:
./redis-cli -h 127.0.0.1 -p 6379
关闭防火墙
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

  

------------恢复内容开始------------

第一步:安装 C 语言需要的 GCC 环境
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
第三步:编译 Redis 源码,进入 redis-5.0.5 目录,执行编译命令(make)
cd redis-5.0.5/src 
make
第四步:安装 Redis ,需要通过 PREFIX 指定安装路径
mkdir /usr/redis -p
make install PREFIX=/usr/redis

前端启动:./redis-server

前端关闭:ctrl+c

后端启动(守护进程启动)
第一步:拷贝 redis-5.0.5/redis.conf 配置文件到 Redis 安装目录的 bin 目录
cp redis.conf /usr/redis/bin/  
第二步:修改 redis.conf   vi redis.conf
  
# 将`daemonize`由`no`改为`yes` 后台可以运行 
daemonize yes 
# 默认绑定的是回环地址,默认不能被其他机器访问 
# bind 127.0.0.1 
# 是否开启保护模式,由yes该为no
protected-mode no
第三步:启动服务 
./redis-server redis.conf
后端启动的关闭方式
./redis-cli shutdown
Redis命令行客户端:
./redis-cli -h 127.0.0.1 -p 6379
关闭防火墙
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 (赋写和执行的权限)

启动RedisCluster:./start.sh
在任意bin下创建Redis集群:前三是主  后三是从 
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

What is the receiving node ID?(输入上述查询节点信息7007的id)68af1e781efee83645485dd78de3520e43640ccc
查看分配后的情况:
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

  

注意:如果原来该结点在集群中的配置信息已经生成到cluster-confifig-fifile指定的配置文件中(如果
cluster-confifig-fifile没有指定则默认为nodes.conf),这时可能会报错:
[ERR] Node XXXXXX is not empty. Either the node already knows other nodes (check
with CLUSTER NODES) or contains some key in database 0
解决方法是删除生成的配置文件nodes.conf,删除后再执行./redis-cli --cluster add-node 指令
缩容
./redis-cli --cluster del-node 192.168.127.128:7008
6be94480315ab0dd2276a7f70c82c578535d6666
删除已经占有hash槽的结点会失败,报错如下:
[ERR] Node 192.168.127.128:7008 is not empty! Reshard data away and try again.
需要将该结点占用的hash槽分配出去。
posted @ 2022-04-04 23:08  a快乐码农  阅读(210)  评论(0编辑  收藏  举报