mkdir -p /app/redis
tar -zxvf redis-5.0.8.tar.gz -C /app/redis
cd /app/redis/redis-5.0.8

编译安装:
make & make install

注意: 安装过程中如果遇到报错提示信息:/bin/sh: cc: command not found, 那是因为gcc和cc命令令未找到执行如下命令:
yum -y install gcc gcc-c++ libstdc++-devel
## 如果不够,执行: yum -y install    gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel zip unzip kernel-headers kernel-devel


单机直接运行即可启动:
cd /app/redis/redis-5.0.8/src
./redis-server /app/redis/redis-5.0.8/redis.conf


三主三从节点搭建:创建集群中各个节点的配置文件存放路径
mkdir -p /app/redis-cluster
cd redis-cluster/
mkdir  1-redis 2-redis 3-redis 4-redis  5-redis 6-redis


配置文件拷贝:
\cp -rf /app/redis/redis-5.0.8/redis.conf   /app/redis-cluster/1-redis/
\cp -rf /app/redis/redis-5.0.8/redis.conf   /app/redis-cluster/2-redis/
\cp -rf /app/redis/redis-5.0.8/redis.conf   /app/redis-cluster/3-redis/
\cp -rf /app/redis/redis-5.0.8/redis.conf   /app/redis-cluster/4-redis/
\cp -rf /app/redis/redis-5.0.8/redis.conf   /app/redis-cluster/5-redis/
\cp -rf /app/redis/redis-5.0.8/redis.conf   /app/redis-cluster/6-redis/

四 修改如下内容
1 改为以守护进程运行:
daemonize yes  #默认是 daemonize no

2 修改端口,6个节点,同一台机器,可以改为使用不同的端口,比如:6381  6382  6383  6384  6385 6386:
port 6381      #默认是 port 6379,

3 指定数据文件存放位置,必须要指定不同的目录位置,不然会丢失数据:
dir /app/redis-cluster/1-redis/      #默认是 dir ./,

4 取消注释,该行默认被#注释: cluster-enabled yes

5 集群节点信息文件,这里文件名最好和port对应上)
cluster-config-file nodes-6381.conf  #默认是:cluster-config-file nodes-6379.conf,并且默认处于注释状态

6 取消注释:cluster-node-timeout 15000

7 bind地址改为:
bind  0.0.0.0

8 protected-mode no (关闭保护模式)

9 修改appendonly:
appendonly yes    #默认 appendonly no

10 修改pid 进程文件:
pidfile  /app/redis-cluster/redis_6381.pid   #默认是 pidfile /var/run/redis_6379.pid

如果要设置密码需要增加如下配置:
11 requirepass redis12345 (设置redis访问密码为: redis12345)
12 masterauth redis12345 (设置集群节点间访问密码,跟上面一致)

13 如果需要打印日志,修改logfile:
mkdir -p /app/redis-cluster/logs
logfile "/app/redis-cluster/logs/redis_6381.log"  ##建议以各自运行端口作为日志后缀


五 分别启动6个redis实例:
/app/redis/redis-5.0.8/src/redis-server  /app/redis-cluster/1-redis/redis.conf
/app/redis/redis-5.0.8/src/redis-server  /app/redis-cluster/2-redis/redis.conf
/app/redis/redis-5.0.8/src/redis-server  /app/redis-cluster/3-redis/redis.conf
/app/redis/redis-5.0.8/src/redis-server  /app/redis-cluster/4-redis/redis.conf
/app/redis/redis-5.0.8/src/redis-server  /app/redis-cluster/5-redis/redis.conf
/app/redis/redis-5.0.8/src/redis-server  /app/redis-cluster/6-redis/redis.conf

查看进程:
ps -ef | grep redis

六 创建集群(说明:主节点在前,从节点在后。其中--cluster-replicas参数用来指定一个主节点带有的从节点个数,如上--cluster-replicas 1即表示 1 个主节点有 1 个从节点。)
/app/redis/redis-5.0.8/src/redis-cli --cluster create 10.33.65.11:6381 10.33.65.11:6382  10.33.65.11:6383 10.33.65.11:6384 10.33.65.11:6385  10.33.65.11:6386 --cluster-replicas 1 -a redis12345
/app/redis/redis-5.0.8/src/redis-cli --cluster create 10.221.20.101:6395 10.221.20.102:6395  10.221.20.101:6396 10.221.20.102:6396 10.221.20.101:6397  10.221.20.102:6397 --cluster-replicas 1 -a RqmRs123++
/app/redis/redis-5.0.8/src/redis-cli -a redis12345 --cluster-replicas 1 --cluster create  10.33.65.11:6381 10.33.65.11:6382  10.33.65.11:6383 10.33.65.11:6384 10.33.65.11:6385  10.33.65.11:6386


验证集群之到任一节点写入键值对:
/app/redis/redis-5.0.8/src/redis-cli -a redis12345 -c -h 10.33.65.11 -p 6381
输入: auth redis12345
输入: set aa  11
输入: get aa      #反查,看有没有赋值成功
输入: exit


验证集群之到其他节点查询键值对:
/app/redis/redis-5.0.8/src/redis-cli -a redis12345 -c -h 10.33.65.11 -p 6382
输入: auth redis12345
输入: get aa
输入: exit

查看集群信息,连接任一个节点:
/app/redis/redis-5.0.8/src/redis-cli -a redis12345 -c -h 10.33.65.11 -p 6382
输入:cluster nodes
输入:cluster info

查看集群信息, 也可以输入连接命令时候直接填写:
/app/redis/redis-5.0.8/src/redis-cli -a redis12345 -c -h 10.33.65.11 -p 6382 cluster nodes
/app/redis/redis-5.0.8/src/redis-cli -a redis12345 -c -h 10.33.65.11 -p 6382 cluster info

10 关闭集群
需要逐个进行关闭,使用命令:
/app/redis/redis-5.0.8/src/redis-cli -a redis12345 -c -h 10.33.65.11 -p 6381 shutdown
/app/redis/redis-5.0.8/src/redis-cli -a redis12345 -c -h 10.33.65.11 -p 6382 shutdown
/app/redis/redis-5.0.8/src/redis-cli -a redis12345 -c -h 10.33.65.11 -p 6383 shutdown
/app/redis/redis-5.0.8/src/redis-cli -a redis12345 -c -h 10.33.65.11 -p 6384 shutdown
/app/redis/redis-5.0.8/src/redis-cli -a redis12345 -c -h 10.33.65.11 -p 6385 shutdown
/app/redis/redis-5.0.8/src/redis-cli -a redis12345 -c -h 10.33.65.11 -p 6386 shutdown


Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐