redis简介
安装步骤1.需要安装gcc。 yum install gcc-c++ 2.下载redis的源码包,下载地址:http://download.redis.io/releases/redis-3.0.0.tar.gz 3.把源码包上传到Linux服务器。 4.解压源码包 tar -zxvf redis-3.0.0.tar.gz 5.进入redis目录使用make命令 cd redis-3.0.0/ make 6.使用make install指定目录安装redis # 指定安装目录为/usr/local/redis make install PREFIX=/usr/local/redis 启动redis服务器1.前端启动模式 # 默认是前端启动模式,端口为6379 /usr/local/redis/bin/redis-server 2.后端启动 1) 从redis的源码目录中复制redis.conf到redis的安装目录。 cp redis.conf /usr/local/redis/bin/ 2) 修改配置文件redis.conf
将daemonize no 改为 daemonize yes 3) 根据配置文件后端启动 ./redis-server redis.conf redis常用命令启动redis客户端 [root@VM_161_110_centos bin]# ./redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379> set a 1 OK 127.0.0.1:6379> get a "1" 127.0.0.1:6379> keys * 1) "a" 127.0.0.1:6379> incr a (integer) 2 127.0.0.1:6379> decr a (integer) 1 127.0.0.1:6379> del a (integer) 1 127.0.0.1:6379> keys * (empty list or set) redis常用数据类型
redis集群的搭建redis-cluster 架构图
redis-cluster投票:容错
集群的节点规划3个节点的集群,每个节点有一主一备,理论上需要6台服务器(没有辣么多钱)。所以搭建一个伪分布式的集群,使用6个redis实例来模拟。 搭建ruby环境
yum install ruby yum install rubygems # 安装ruby包管理器
gem install redis-3.0.0.gem 集群的搭建第一步:创建6个redis实例,端口号从7001~7006 1. 在/usr/local/创建redis-cluster目录并从/usr/local/redis的redis文件夹复制到redis-cluster文件夹下并将文件夹重名为redis01,以此类推创建redis02、redis03、……redis06。 2. 把创建集群的ruby脚本复制到redis-cluster目录下,即 将redis源码包(src目录)下的redis-trib.rb复制到redis-cluster目录下。 #1.将redis目录下所有文件复制到redis-cluster目录下并将文件夹重名为redis01 cp -r redis /usr/local/redis-cluster/redis01 #2.将redis-trib.rb复制到redis-cluster目录下 cp redis-trib.rb /usr/local/redis-cluster 第二步:修改6个redis实例下的bin目录下的redis.conf配置文件 1、修改端口号 这里是修改了redis01的端口,剩余5个redis实例的端口分别为7002至7006。 2、打开cluster-enable前面的注释。 告诉redis要使用集群。 第三步:启动6个redis实例。 在redis-cluster目录下创建startup.sh用于启动6个redis实例,并给startup.sh赋予执行权限。 #创建startup.sh [root@VM_161_110_centos redis-cluster]# vim startup.sh cd redis01/bin ./redis-server redis.conf cd .. cd .. cd redis02/bin ./redis-server redis.conf cd .. cd .. cd redis03/bin ./redis-server redis.conf cd .. cd .. cd redis04/bin ./redis-server redis.conf cd .. cd .. cd redis05/bin ./redis-server redis.conf cd .. cd .. cd redis06/bin ./redis-server redis.conf cd .. cd .. # 赋予执行权限 chmod +x startup.sh # 启动6个redis实例 ./startup.sh # 查看redis进程运行: ps aux|grep redis redis-cluster创建好的目录结构: 第五步:创建集群。 需要知道Linux服务器的ip地址,可以用ifconfig命令查看。 ./redis-trib.rb create --replicas 1 10.104.161.110:7001 10.104.161.110:7002 10.104.161.110:7003 10.104.161.110:7004 10.104.161.110:7005 10.104.161.110:7006 集群创建的信息: # 主节点有槽,从节点没有槽 >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 10.104.161.110:7001 10.104.161.110:7002 10.104.161.110:7003 Adding replica 10.104.161.110:7004 to 10.104.161.110:7001 Adding replica 10.104.161.110:7005 to 10.104.161.110:7002 Adding replica 10.104.161.110:7006 to 10.104.161.110:7003 M: 131a1d2c7a02cd38e9f61138975863fc8c20d0ea 10.104.161.110:7001 slots:0-5460 (5461 slots) master M: e2a9a28c3842d37aa602182110f93598c2936da5 10.104.161.110:7002 slots:5461-10922 (5462 slots) master M: 5e0096b41ae8ae5b06539a311c5741d01163497e 10.104.161.110:7003 slots:10923-16383 (5461 slots) master S: 0920f150bbd8553414492bb51c1ea03055484ee6 10.104.161.110:7004 replicates 131a1d2c7a02cd38e9f61138975863fc8c20d0ea S: e2439d0425c7abbf437b76b50ebe1db6066f51ef 10.104.161.110:7005 replicates e2a9a28c3842d37aa602182110f93598c2936da5 S: 1127ebb56980ebb23c45d3c5a4c79c92840470df 10.104.161.110:7006 replicates 5e0096b41ae8ae5b06539a311c5741d01163497e 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.104.161.110:7001) M: 131a1d2c7a02cd38e9f61138975863fc8c20d0ea 10.104.161.110:7001 slots:0-5460 (5461 slots) master M: e2a9a28c3842d37aa602182110f93598c2936da5 10.104.161.110:7002 slots:5461-10922 (5462 slots) master M: 5e0096b41ae8ae5b06539a311c5741d01163497e 10.104.161.110:7003 slots:10923-16383 (5461 slots) master M: 0920f150bbd8553414492bb51c1ea03055484ee6 10.104.161.110:7004 slots: (0 slots) master replicates 131a1d2c7a02cd38e9f61138975863fc8c20d0ea M: e2439d0425c7abbf437b76b50ebe1db6066f51ef 10.104.161.110:7005 slots: (0 slots) master replicates e2a9a28c3842d37aa602182110f93598c2936da5 M: 1127ebb56980ebb23c45d3c5a4c79c92840470df 10.104.161.110:7006 slots: (0 slots) master replicates 5e0096b41ae8ae5b06539a311c5741d01163497e [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. 测试集群在redis01至redis06下的bin下有redis-cli,任选其一使用,如: # -h 表示主机ip地址 -p 表示端口(7001-7006) -c 表示集群(cluster) redis01/bin/redis-cli -h 10.104.161.110 -p 7002 -c 测试瞎玩: [root@VM_161_110_centos redis-cluster]# redis01/bin/redis-cli -h 10.104.161.110 -p 7001 -c 10.104.161.110:7001> set b 99 OK 10.104.161.110:7001> get b "99" 10.104.161.110:7001> get a -> Redirected to slot [15495] located at 10.104.161.110:7003 "100" 10.104.161.110:7003> ping PONG 注意:如果你在远程服务器下搭建redis记得开启7001至7006端口和6379端口,则以后好使用redis。 # 打开7001端口 iptables -A INPUT -ptcp --dport 7001 -j ACCEPT (责任编辑:IT) |