redis3.0.7安装和集群详细步骤

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/ItChuangyi/article/details/79609811

什么是redis?

Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库。
它通过提供多种键值数据类型来适应不同场景下的存储需求。
目前为止Redis支持的键值数据类型如下:

 1. 字符串类型
 2. 散列类型
 3. 列表类型
 4. 集合类型
 5. 有序集合类型

redis的应用场景

 1. 缓存(数据查询、短连接、新闻内容、商品内容等等)。(最多使用)
 2. 分布式集群架构中的session分离。
 3. 聊天室的在线好友列表。
 4. 任务队列。(秒杀、抢购、12306等等)
 5. 应用排行榜。
 6. 网站访问统计。

现在进入正题………

一、安装redis

1.1、redis安装环境

    redis是C语言开发,建议在linux上运行, 安装redis需要先将官网下载的源码进行编译,
编译依赖gcc环境,如果没有gcc环境,需要安装gcc:
yum install gcc-c++

1.2、官网下载redis-3.0.7版本,3.*的版本才支持集群模式

http://download.redis.io/releases/redis-3.0.7.tar.gz

1.3、更改权限

chmod +x redis-3.0.7.tar.gz

1.4、解压源码

tar -zxvf redis-3.0.7.tar.gz
-rw-------. 1 root root    1432 3月  19 05:21 anaconda-ks.cfg
-rw-r--r--. 1 root root   26150 3月  19 05:21 install.log
-rw-r--r--. 1 root root    8415 3月  19 05:20 install.log.syslog
drwxrwxr-x. 6 root root    4096 1月  25 2016 redis-3.0.7
-rwxr-xr-x. 1 root root 1375200 3月  19 05:32 redis-3.0.7.tar.gz
[root@redis ~]#

1.5、进入解压后的目录进行编译

cd redis-3.0.7
make

1.6、安装到指定目录,如 /usr/local/redis

cd redis-3.0.7 
make PREFIX=/usr/local/redis install

1.7、redis.conf

redis.conf是redis的配置文件,redis.conf在redis源码目录。
注意修改port作为redis进程的端口,port默认6379。

1.8、拷贝配置文件到安装目录下

进入源码目录,里面有一份配置文件 redis.conf,然后将其拷贝到安装路径下
cd redis-3.0.7
cp redis-3.0.7/redis.conf  /usr/local/redis/bin

-rw-r--r--. 1 root root      28 3月  19 06:17 dump.rdb
-rwxr-xr-x. 1 root root 4169819 3月  19 05:44 redis-benchmark
-rwxr-xr-x. 1 root root   16459 3月  19 05:44 redis-check-aof
-rwxr-xr-x. 1 root root   37691 3月  19 05:44 redis-check-dump
-rwxr-xr-x. 1 root root 4262245 3月  19 05:44 redis-cli
-rwxr-xr-x. 1 root root   41561 3月  19 05:50 redis.conf
lrwxrwxrwx. 1 root root      12 3月  19 05:44 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 5704647 3月  19 05:44 redis-server
[root@redis bin]#

1.9、修改redis.conf配置文件, 以后端模式启动

修改  daemonize yes

执行如下命令启动redis:
cd /usr/local/redis
./bin/redis-server ./redis.conf

停止命令:
强行终止Redis进程可能会导致redis持久化数据丢失。正确停止Redis的方式应该是向Redis发送SHUTDOWN命令,方法为:
cd /usr/local/redis
./bin/redis-cli shutdown

1.10、查看状态

ps aux|grep redis

[root@redis redis]# ps aux|grep redis
root      1801  0.0  0.0   5984   732 pts/0    S+   08:10   0:00 grep redis
root     10444  0.1  0.0  35560  1792 ?        Ssl  05:50   0:12 ./redis-server *:6379 

1.11、redis客户端

    在redis的安装目录中有redis的客户端,即redis-cli(Redis Command Line Interface),
它是Redis自带的基于命令行的Redis客户端。

在同一台虚拟机上的redis服务的方式:
[root@redis redis]# cd bin/
[root@redis bin]# ll
总用量 13920
-rw-r--r--. 1 root root      28 3月  19 06:17 dump.rdb
-rwxr-xr-x. 1 root root 4169819 3月  19 05:44 redis-benchmark
-rwxr-xr-x. 1 root root   16459 3月  19 05:44 redis-check-aof
-rwxr-xr-x. 1 root root   37691 3月  19 05:44 redis-check-dump
-rwxr-xr-x. 1 root root 4262245 3月  19 05:44 redis-cli
-rwxr-xr-x. 1 root root   41561 3月  19 05:50 redis.conf
lrwxrwxrwx. 1 root root      12 3月  19 05:44 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 5704647 3月  19 05:44 redis-server
[root@redis bin]# ./redis-cli 
127.0.0.1:6379>(然后就可以各种set key,get key了)

使用另外一台安装了redis的虚拟机作为客户端:(或者用jedis,相关知识自行脑补):
需要关闭服务器端的的防火墙:
防火墙打开6379端口
/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
/etc/rc.d/init.d/iptables save
/etc/init.d/iptables status

客户端虚拟机中连接:指定连接redis服务的ip和端口:
./redis-cli -h 192.168.25.129 -p 6379

这样,redis终于安装完成了,不容易啊!然而……

二、搭建集群

搭建伪分布式,需要6个redis实例(3个集群,3个备份),运行在不同的端口(7001-7006)

2.1、复制多个实例

创建放集群的目录:
mkdir redis-cluster
[root@redis local]# ll

总用量 44
drwxr-xr-x. 2 root root 4096 9月  23 2011 bin
drwxr-xr-x. 2 root root 4096 9月  23 2011 etc
drwxr-xr-x. 2 root root 4096 9月  23 2011 games
drwxr-xr-x. 2 root root 4096 9月  23 2011 include
drwxr-xr-x. 2 root root 4096 9月  23 2011 lib
drwxr-xr-x. 2 root root 4096 9月  23 2011 libexec
drwxr-xr-x. 3 root root 4096 3月  19 05:44 redis
drwxr-xr-x. 8 root root 4096 3月  19 06:40 redis-cluster
drwxr-xr-x. 2 root root 4096 9月  23 2011 sbin
drwxr-xr-x. 5 root root 4096 3月  19 05:14 share
drwxr-xr-x. 2 root root 4096 9月  23 2011 src

复制实例:
cp -r redis/bin redis-cluster/redis01
cp -r redis/bin redis-cluster/redis02
cp -r redis/bin redis-cluster/redis03
cp -r redis/bin redis-cluster/redis04
cp -r redis/bin redis-cluster/redis05
cp -r redis/bin redis-cluster/redis06
[root@redis redis-cluster]# ll

总用量 32
drwxr-xr-x. 2 root root 4096 3月  19 07:38 redis01
drwxr-xr-x. 2 root root 4096 3月  19 07:38 redis02
drwxr-xr-x. 2 root root 4096 3月  19 07:38 redis03
drwxr-xr-x. 2 root root 4096 3月  19 07:38 redis04
drwxr-xr-x. 2 root root 4096 3月  19 07:38 redis05
drwxr-xr-x. 2 root root 4096 3月  19 07:38 redis06

 [root@redis redis-cluster]#

2.2、需要修改每个实例的redis.conf配置文件

[root@redis bin]# vim redis.conf

修改端口号(7000-7006)
修改端口号

配置文件中还需要把cluster-enabled yes前的注释去掉
cluster-enabled yes前的注释去掉

2.3、创建开启集群的脚本

[root@redis redis-cluster]# vim start-all.sh
内容:
cd redis01
./redis-server redis.conf
cd ..
cd redis02
./redis-server redis.conf
cd ..
cd redis03
./redis-server redis.conf
cd ..
cd redis04
./redis-server redis.conf
cd ..
cd redis05
./redis-server redis.conf
cd ..
cd redis06
./redis-server redis.conf
cd ..

开启:
[root@redis redis-cluster]# ./start-all.sh
[root@redis redis-cluster]# ps aux|grep redis
root       488  0.1  0.1  35560  2224 ?        Ssl  06:40   0:12 ./redis-server *:7001 [cluster]
root       492  0.1  0.1  35560  2248 ?        Ssl  06:40   0:12 ./redis-server *:7002 [cluster]
root       496  0.1  0.1  35560  2156 ?        Ssl  06:40   0:12 ./redis-server *:7003 [cluster]
root       500  0.1  0.1  35560  2148 ?        Ssl  06:40   0:12 ./redis-server *:7004 [cluster]
root       502  0.1  0.1  35560  2148 ?        Ssl  06:40   0:12 ./redis-server *:7005 [cluster]
root       506  0.1  0.1  35560  2136 ?        Ssl  06:40   0:12 ./redis-server *:7006 [cluster]

2.4、创建关闭集群的脚本

[root@redis redis-cluster]# vim shutdown-all.sh 
redis01/redis-cli -p 7001 shutdown
redis01/redis-cli -p 7002 shutdown
redis01/redis-cli -p 7003 shutdown
redis01/redis-cli -p 7004 shutdown
redis01/redis-cli -p 7005 shutdown
redis01/redis-cli -p 7006 shutdown

关闭:
[root@redis redis-cluster]# ./shutdown-all.sh

2.5、使用ruby脚本搭建集群,需要ruby的运行环境

2.5.1、安装ruby
yum install ruby
出现【y/n】,选y
2.5.2、安装rubygems组件
yum install rubygems
出现【y/n】,选y
2.5.3、安装ruby脚本运行使用的包
[root@redis redis-cluster]# gem install redis --version 3.0.7
Successfully installed redis-3.0.7
1 gem installed
Installing ri documentation for redis-3.0.7...
Installing RDoc documentation for redis-3.0.7...
[root@redis redis-cluster]#

注意:gem install redis --version 3.0.7失败的话,修改下载网址
gem sources --remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/

2.6、执行redis的创建集群命令创建集群(这一步之前要开启所有的实例)

2.6.1、到redis的编译目录
[root@redis ~]# cd redis-3.0.7
[root@redis redis-3.0.7]# ll

总用量 152
-rw-rw-r--.  1 root root 36761 1月  25 2016 00-RELEASENOTES
-rw-rw-r--.  1 root root    53 1月  25 2016 BUGS
-rw-rw-r--.  1 root root  1805 1月  25 2016 CONTRIBUTING
-rw-rw-r--.  1 root root  1487 1月  25 2016 COPYING
drwxrwxr-x.  6 root root  4096 3月  19 05:43 deps
-rw-rw-r--.  1 root root    11 1月  25 2016 INSTALL
-rw-rw-r--.  1 root root   151 1月  25 2016 Makefile
-rw-rw-r--.  1 root root  4223 1月  25 2016 MANIFESTO
-rw-rw-r--.  1 root root  5201 1月  25 2016 README
-rw-rw-r--.  1 root root 41560 1月  25 2016 redis.conf
-rwxrwxr-x.  1 root root   271 1月  25 2016 runtest
-rwxrwxr-x.  1 root root   280 1月  25 2016 runtest-cluster
-rwxrwxr-x.  1 root root   281 1月  25 2016 runtest-sentinel
-rw-rw-r--.  1 root root  7109 1月  25 2016 sentinel.conf
drwxrwxr-x.  2 root root  4096 3月  19 05:44 src
drwxrwxr-x. 10 root root  4096 1月  25 2016 tests
drwxrwxr-x.  5 root root  4096 1月  25 2016 utils

[root@redis redis-3.0.7]# cd src/
[root@redis src]# ll *rb
-rwxrwxr-x. 1 root root 60527 1月  25 2016 redis-trib.rb
能找到redis-trib.rb,说明2.5.3步,成功了!

离成功仅有一步之遥……

2.6.2、运行集群命令

[root@redis src]# ./redis-trib.rb create --replicas 1 192.168.25.129:7001 192.168.25.129:7002 192.168.25.129:7003 192.168.25.129:7004 192.168.25.129:7005 192.168.25.129:7006

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.25.129:7001
192.168.25.129:7002
192.168.25.129:7003
Adding replica 192.168.25.129:7004 to 192.168.25.129:7001
Adding replica 192.168.25.129:7005 to 192.168.25.129:7002
Adding replica 192.168.25.129:7006 to 192.168.25.129:7003
M: d52bb51542a81bc5b19c05cdbec2b9449543c9dd 192.168.25.129:7001
   slots:0-5460 (5461 slots) master
M: f9778ba394c79a5e5a51d644a14896d5141f1c5a 192.168.25.129:7002
   slots:5461-10922 (5462 slots) master
M: 91f1f855f34908e7177655e41c1f92f97fd439bf 192.168.25.129:7003
   slots:10923-16383 (5461 slots) master
S: 79a189b5d22fadeb4c512f0623e90b6b60aeab61 192.168.25.129:7004
   replicates d52bb51542a81bc5b19c05cdbec2b9449543c9dd
S: 9f9923000ffbae92dead6fdc13dd342c99d383de 192.168.25.129:7005
   replicates f9778ba394c79a5e5a51d644a14896d5141f1c5a
S: 802209d47ffe5af1002084bf2effa4b3cdfde1f0 192.168.25.129:7006
   replicates 91f1f855f34908e7177655e41c1f92f97fd439bf

选择yes

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 192.168.25.129:7001)
M: d52bb51542a81bc5b19c05cdbec2b9449543c9dd 192.168.25.129:7001
   slots:0-5460 (5461 slots) master
M: f9778ba394c79a5e5a51d644a14896d5141f1c5a 192.168.25.129:7002
   slots:5461-10922 (5462 slots) master
M: 91f1f855f34908e7177655e41c1f92f97fd439bf 192.168.25.129:7003
   slots:10923-16383 (5461 slots) master
M: 79a189b5d22fadeb4c512f0623e90b6b60aeab61 192.168.25.129:7004
   slots: (0 slots) master
   replicates d52bb51542a81bc5b19c05cdbec2b9449543c9dd
M: 9f9923000ffbae92dead6fdc13dd342c99d383de 192.168.25.129:7005
   slots: (0 slots) master
   replicates f9778ba394c79a5e5a51d644a14896d5141f1c5a
M: 802209d47ffe5af1002084bf2effa4b3cdfde1f0 192.168.25.129:7006
   slots: (0 slots) master
   replicates 91f1f855f34908e7177655e41c1f92f97fd439bf
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@redis src]#

搭建redis集群成功!

进行测试

[root@redis redis-cluster]# ./start-all.sh 
[root@redis redis-cluster]# ps aux|grep redis
root      2631  4.5  0.1  35560  2076 ?        Ssl  11:59   0:00 ./redis-server *:7001 [cluster]
root      2633  2.0  0.1  35560  2072 ?        Ssl  11:59   0:00 ./redis-server *:7002 [cluster]
root      2635  1.0  0.1  35560  2076 ?        Ssl  11:59   0:00 ./redis-server *:7003 [cluster]
root      2641  0.0  0.1  35560  2072 ?        Ssl  11:59   0:00 ./redis-server *:7004 [cluster]
root      2643  0.5  0.1  35560  2076 ?        Ssl  11:59   0:00 ./redis-server *:7005 [cluster]
root      2649  0.0  0.1  35560  2076 ?        Ssl  11:59   0:00 ./redis-server *:7006 [cluster]
root      2655  0.0  0.0   5980   748 pts/0    S+   11:59   0:00 grep redis
root     10444  0.1  0.0  35560  1752 ?        Ssl  05:50   0:30 ./redis-server *:6379    
[root@redis redis-cluster]# redis01/redis-cli -p 7001 -c
127.0.0.1:7001> set key1 1
-> Redirected to slot [9189] located at 192.168.25.129:7002
OK
192.168.25.129:7002> get key1
"1"
192.168.25.129:7002> 
[root@redis redis-cluster]# redis01/redis-cli -p 7002 -c
127.0.0.1:7002> get key1
"1"
[root@redis redis-cluster]# redis01/redis-cli -p 7003 -c
127.0.0.1:7003> get key1
-> Redirected to slot [9189] located at 192.168.25.129:7002
"1"
192.168.25.129:7002> 

这是自己在学习项目中的一点点总结,也是第一次发博客,难免有点激动,出现错误还请大佬多多指教……

猜你喜欢

转载自blog.csdn.net/ItChuangyi/article/details/79609811