redis5.0安装、配置、集群

1. redis下载地址

redis下载地址

2.检查安装环境

redis是c语言开发,安装redis需要先将官网下载的源码进行编译,编译依赖gcc环境。如果没有gcc环境,需要安装gcc

[root@gs-server-6013 redis-5.0.2]# rpm -qa | grep gcc
gcc-gfortran-4.8.5-16.el7_4.1.x86_64
gcc-c++-4.8.5-16.el7_4.1.x86_64
libgcc-4.8.5-16.el7_4.1.i686
libgcc-4.8.5-16.el7_4.1.x86_64
gcc-4.8.5-16.el7_4.1.x86_64

3. 解压安装

 tar -zxvf redis-5.0.2.tar.gz 
cd redis-5.0.2
make
cd src
[root@gs-server-6013 src]# make PREFIX=/usr/local/redis install

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
[root@gs-server-6013 src]# make test

查看安装目录

[root@gs-server-6013 src]# ls /usr/local/redis/bin/
redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis-sentinel  redis-server

把解压后目录中的配置文件拷贝到安装目录

[root@gs-server-6013 ~]# mkdir /usr/local/redis/bin/conf -p
[root@gs-server-6013 ~]# cp redis-5.0.2/redis.conf /usr/local/redis/bin/conf

修改配置文件

 vim  /usr/local/redis/bin/conf/redis.conf 
 修改为
daemonize yes

4. 启动测试

[root@gs-server-6013 ~]# cd /usr/local/redis/bin/
[root@gs-server-6013 bin]# ./redis-server conf/redis.conf 
34362:C 11 Apr 2019 15:59:38.541 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
34362:C 11 Apr 2019 15:59:38.541 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=34362, just started
34362:C 11 Apr 2019 15:59:38.541 # Configuration loaded
[root@gs-server-6013 bin]# 
[root@gs-server-6013 bin]# ./redis-cli 
127.0.0.1:6379> set name redistest
OK
127.0.0.1:6379> get name
"redistest"
127.0.0.1:6379> 

5. 集群搭建

  • 5.0版本开始不用ruby,不需要安装
  • 目录创建
mkdir /usr/local/redis/cluster/{7001,7002,7003}/data -p
  • 拷贝安装目录的配置文件至集群目录
cp /root/redis-5.0.2/redis.conf /usr/local/redis/cluster/7001/
  • 修改配置文件
protected-mode no(开启远程访问)
port 9001(每个节点的端口号)
daemonize yes(后台运行)
bind 192.168.119.131(绑定当前机器 IP)
dir /usr/local/redis-cluster/9001/data/(数据文件存放位置)
pidfile /var/run/redis_9001.pid(pid 9001和port要对应)
cluster-enabled yes(启动集群模式)
cluster-config-file nodes-9001.conf(9001和port要对应)
cluster-node-timeout 15000
appendonly yes
masterauth 123456 和 requirepass 123456(设置redis集群密码 )
  • 拷贝配置文件至7002和7003目录并修改
cp /usr/local/redis/cluster/7001/redis.conf  /usr/local/redis/cluster/7002/redis.conf
cp /usr/local/redis/cluster/7001/redis.conf  /usr/local/redis/cluster/7003/redis.conf
vim  /usr/local/redis/cluster/7003/redis.conf
:%s/7001/7002/g
  • 编写启动脚本
vim /usr/local/redis/redis_cluster_start_all.sh

/usr/local/redis/bin/redis-server /usr/local/redis/cluster/7001/*.conf
/usr/local/redis/bin/redis-server /usr/local/redis/cluster/7002/*.conf
/usr/local/redis/bin/redis-server /usr/local/redis/cluster/7003/*.conf

  • 添加执行权限
chmod +x  /usr/local/redis/redis_cluster_start_all.sh
  • 启动3个实例
[root@gs-server-6012 redis]#  /usr/local/redis/redis_cluster_start_all.sh
25961:C 12 Apr 2019 14:18:19.823 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
25961:C 12 Apr 2019 14:18:19.823 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=25961, just started
25961:C 12 Apr 2019 14:18:19.823 # Configuration loaded
25963:C 12 Apr 2019 14:18:19.827 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
25963:C 12 Apr 2019 14:18:19.827 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=25963, just started
25963:C 12 Apr 2019 14:18:19.827 # Configuration loaded
25965:C 12 Apr 2019 14:18:19.830 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
25965:C 12 Apr 2019 14:18:19.830 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=25965, just started
25965:C 12 Apr 2019 14:18:19.830 # Configuration loaded

在这里插入图片描述

6.第二台机器部署服务

再找一台服务器按照 一(安装 redis)二(拷贝文件)三(修改配置) 四(服务开启)步骤部署

7.创建集群

/usr/local/redis/bin/redis-cli --cluster create 10.200.40.230:7001 10.200.40.230:7002 10.200.40.230:7003 10.200.40.224:7001 10.200.40.224:7002 10.200.40.224:7003 --cluster-replicas 1 -a 123456

运行成功会要求输入yes,如下

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.200.40.224:7002 to 10.200.40.230:7001
Adding replica 10.200.40.230:7003 to 10.200.40.224:7001
Adding replica 10.200.40.224:7003 to 10.200.40.230:7002
M: 9c78bf759e35316623c336c7fd6df576230a2826 10.200.40.230:7001
   slots:[0-5460] (5461 slots) master
M: a3e9c3a205329894f5954c128d5dc18e8145758b 10.200.40.230:7002
   slots:[10923-16383] (5461 slots) master
S: 82af9ffc8bc6bbe146a4ecb49b971517867c61f8 10.200.40.230:7003
   replicates 0284744a187b1f18faa17f72ea728bfb92419ea0
M: 0284744a187b1f18faa17f72ea728bfb92419ea0 10.200.40.224:7001
   slots:[5461-10922] (5462 slots) master
S: f03bc1ea5878eadb1117cd8a2db242cafe622876 10.200.40.224:7002
   replicates 9c78bf759e35316623c336c7fd6df576230a2826
S: 808693468676503ead4353f038b70190a213d06f 10.200.40.224:7003
   replicates a3e9c3a205329894f5954c128d5dc18e8145758b
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 10.200.40.230:7001)
M: 9c78bf759e35316623c336c7fd6df576230a2826 10.200.40.230:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: f03bc1ea5878eadb1117cd8a2db242cafe622876 10.200.40.224:7002
   slots: (0 slots) slave
   replicates 9c78bf759e35316623c336c7fd6df576230a2826
S: 82af9ffc8bc6bbe146a4ecb49b971517867c61f8 10.200.40.230:7003
   slots: (0 slots) slave
   replicates 0284744a187b1f18faa17f72ea728bfb92419ea0
M: 0284744a187b1f18faa17f72ea728bfb92419ea0 10.200.40.224:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 808693468676503ead4353f038b70190a213d06f 10.200.40.224:7003
   slots: (0 slots) slave
   replicates a3e9c3a205329894f5954c128d5dc18e8145758b
M: a3e9c3a205329894f5954c128d5dc18e8145758b 10.200.40.230:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

8.测试

/usr/local/redis/bin/redis-cli  -c -h 10.200.40.230 -p 7002 -a 123456

10.200.40.230:7003> set ll 89
-> Redirected to slot [9069] located at 10.200.40.224:7001
OK
10.200.40.224:7001> get ll
"89"
10.200.40.224:7001> cluster info 
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:4
cluster_stats_messages_ping_sent:1451
cluster_stats_messages_pong_sent:1479
cluster_stats_messages_meet_sent:2
cluster_stats_messages_sent:2932
cluster_stats_messages_ping_received:1476
cluster_stats_messages_pong_received:1453
cluster_stats_messages_meet_received:3
cluster_stats_messages_received:2932

9.设置开机自启

vim /etc/rc.d/rc.local

添加如下代码

/usr/local/redis/cluster/redis_cluster_start_all.sh
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值