redis- 4.0安装

来源:互联网 发布:电信网络新型违法犯罪 编辑:程序博客网 时间:2024/06/06 15:41
一、单机版
1、前提条件
yum install gcc-c++  

2、安装redis

wget http://download.redis.io/releases/redis-4.0.1.tar.gz

tar -zxvf redis-xxxx.tar.gz  

cd redis-xxxx
make
make install

3、测试Redis是否安装成功
两种启动redis方式
(一)前端启动
    ./redis-server  
(二)后台启动
①将redis.conf从安装文件夹中拷贝到/usr/local/redis/bin路径下
cp /usr/local/software/redis-3.2.8/redis.conf /usr/local/redis/bin/  

②编辑/usr/local/redis/bin/下的redis.conf
将 daemonize 后面的no改为yes  
③后台启动redis
    ./redis-server redis.conf  
④查看redis进程
⑤给redis设置登录密码,修改redis.conf
requirepass 123456

4、测试Redis连接客户端
    redis-cli -p 端口 -h ip地址 <-c>连接集群时使用此参数  
默认端口:6379
Ip:localhost(127.0.0.1)
测试连接,放入一个值,并查看
redis-cli  
set a 100

get a


二、redis集群

二、集群搭建

1、redis服务器说明

192.168.1.216:6379

192.168.1.216:6380

192.168.1.216:6381


192.168.1.215:6382

192.168.1.215:6383

192.168.1.215:6384

注:测试环境可以用两台机器,生产环境建议使用6台服务器。


①集群中应该至少有三个节点,每个节点有一备份节点。需要6台服务器。在user/local路径下新建文件夹redis-cluster,复制六份redis 到这个路径下(redis01、redis02、redis03、redis04、redis05、redis06)

2、配置REDIS

修改每一个下面的redis.conf文件

port 6379  
pidfile /var/run/redis-6379.pid  
dbfilename dump-6379.rdb  
appendfilename "appendonly-6379.aof"  
cluster-config-file nodes-6379.conf  
cluster-enabled yes  
cluster-node-timeout 5000  
appendonly yes 

copy配置文件,并修改端口
    # cp redis.conf /etc/redis/redis-6379.conf  
    # cp redis.conf /etc/redis/redis-6380.conf  
    # cp redis.conf /etc/redis/redis-6381.conf  
      
    # scp redis.conf 192.168.10.220:/etc/redis/redis-6382.conf  
    # scp redis.conf 192.168.10.220:/etc/redis/redis-6383.conf  
    # scp redis.conf 192.168.10.220:/etc/redis/redis-6384.conf  
      
    # sed -i "s/6379/6380/g" /etc/redis/redis-6380.conf  
    # sed -i "s/6379/6381/g" /etc/redis/redis-6381.conf  
      
    # sed -i "s/6379/6382/g" /etc/redis/redis-6382.conf  
    # sed -i "s/6379/6383/g" /etc/redis/redis-6383.conf  
    # sed -i "s/6379/6384/g" /etc/redis/redis-6384.conf


 
    cluster-require-full-coverage no。默认是yes,只要有结点宕机导致16384个槽没全被覆盖,整个集群就全部停止服务,所以一定要改为no  
注:把所有的节点名添加到/ETC/HOSTS

②需要一个ruby脚本在redis源码文件夹下的src目录下。redis-trib.rb,将此文件拷贝到/usr/local/redis-cluster下
 cp /usr/local/software/redis-3.2.8/src/redis-trib.rb /usr/local/redis-cluster/  
③执行ruby脚本需要安装ruby环境
yum install ruby  
yum install rubygems  
④安装redis需要依赖的gem包
redis-3.2.2.gem 下载地址:http://download.csdn.net/detail/qq_34021712/9819438
将redis-3.2.2.gem拷贝到linux的/usr/local/redis-cluster/下,然后执行安装
gem install redis-3.2.2.gem  

注:安装 ruby-2.4.0.tar.gz
           1)  解压并编译 ./configure
           2)  安装 make &make install
           3)安装 rubygems-2.6.8.tgz.tar
              ruby setup.rb   
        4. 安装 redis-3.0.0.gem, 必须是3.0.0.gem , 高版本可能有错误。
            gem install redis-3.0.0.gem
root@test-2:/opt/ruby-2.4.1/ext/zlib#gem install redis
ERROR:  Loading command: install (LoadError)
    cannot load such file -- zlib
ERROR:  While executing gem ... (NoMethodError)
    undefined method `invoke_with_build_args' for nil:NilClass

3、启动REDIS

root@test-3:/root#redis-server /opt/redis-4.0.1/redis-cluster/7005/redis.conf &
[3] 20584
root@test-3:/root#20584:C 08 Aug 13:29:09.178 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
20584:C 08 Aug 13:29:09.178 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=20584, just started
20584:C 08 Aug 13:29:09.178 # Configuration loaded
20584:M 08 Aug 13:29:09.178 * Increased maximum number of open files to 10032 (it was originally set to 1024).
20584:M 08 Aug 13:29:09.179 * No cluster configuration found, I'm 72f9b3d96f2e211d375e45a45d90a7025227e0d9
                _._                                                
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.1 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in cluster mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6383
 |    `-._   `._    /     _.-'    |     PID: 20584
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                          


总结错误信息

1:安装Ruby之后,因redis-trib.rb依赖于gem包redis,因此需要执行命令:gem  install redis,报错:
    ERROR:  Loading command: install (LoadError)  
            cannot load such file -- zlib  
    ERROR:  While executing gem ... (NoMethodError)  
        undefined method `invoke_with_build_args' for nil:NilClass  

解决方法:
    apt-get install zlib1g-dev  
    cd /root/redis/ruby-2.2.3/ext/zlib  
    ruby ./extconf.rb  
    make  
    make install  


2:再次执行命令gem  install  redis,再次报错:
    ERROR:  While executing gem ... (Gem::Exception)  
        Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources  
解决方法:
    apt-get install libssl-dev  
    cd /root/redis/ruby-2.2.3/ext/openssl  
    ruby ./extconf.rb  
    ln -s /root/redis/ruby-2.2.3/include /  
    make  
    make install  


3:再次执行命令gem  install  redis,竟然还是报错,错误信息是:
    Errno::ECONNRESET: Connection reset by peer  
查了一下原因,竟然是伟大的墙做的贡献(https://ruby.taobao.org/),解决办法如下:
    #gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/  
    https://ruby.taobao.org/ added to sources  
    https://rubygems.org/ removed from sources  
      
    #gem sources -l  
    *** CURRENT SOURCES ***  
      
    https://ruby.taobao.org/  

4:再次执行命令gem  install  redis,终于成功,此时,就可以运行redis-trib.rb脚本了。

注:ssh互相信任

 1.在两台机器上生成认证文件,就是ssh互信的认证文件,需要放在用户的home目录中,所以我们需要先创建这个目录,且保证这个目录的全县时755
[root@test-3 ~]# mkdir ~/.ssh
[root@test-3 ~]# chmod 755 ~/.ssh
[root@test-3 ~]# /usr/bin/ssh-keygen -t rsa #下面默认回车就行
[root@test-3 ~]# /usr/bin/ssh-keygen -t dsa


[root@test-1 ~]# mkdir ~/.ssh
[root@test-1 ~]# chmod 755 ~/.ssh
[root@test-1 ~]# /usr/bin/ssh-keygen -t rsa
[root@test-1 ~]# /usr/bin/ssh-keygen -t dsa
2.必须将每个主机上的公共密钥文件id_rsa.pub和id_dsa.pub的内容复制到其他每一个主机的~/.ssh/authorized_keys文件中。注意,当您第一次使用ssh访问远程主机时,其RSA密钥是未知的,所以提示确认一下,确认完毕后SSH将纪录远程主机的RSA密钥,以后连接该主机将不再做出相应的提示。

[root@test-3 ~]# cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
[root@test-3 ~]# cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys
[root@test-3 ~]# ssh root@test-1 cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
[root@test-3 ~]# ssh root@test-1 cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

 

3.经过上述两步,在test-3上存了一份完整的key认证文件,我们只需要把这个目录拷贝到test-1这台机器的对应目录里去就可以了,别忘了把authorized_keys 的权限设置成600

[root@test-3 ~]# scp ~/.ssh/authorized_keys test-1:~/.ssh/authorized_keys
[root@test-3 ~]# chmod 600 ~/.ssh/authorized_keys



 4、启动REDIS所有节点


root@test-3:/opt/redis-4.0.1/redis-cluster#ps -ef|grep redis
root     20667 13770  0 13:35 pts/0    00:00:00 redis-server 127.0.0.1:6383 [cluster]
root     20672 13770  0 13:35 pts/0    00:00:00 redis-server 127.0.0.1:6382 [cluster]
root     20676 13770  0 13:36 pts/0    00:00:00 redis-server 127.0.0.1:6379 [cluster]
root     20681 13770  0 13:36 pts/0    00:00:00 grep --color=auto redis
root@test-3:/opt/redis-4.0.1/redis-cluster#netstat  -tpnl|grep redis
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      20676/redis-server  
tcp        0      0 127.0.0.1:6382          0.0.0.0:*               LISTEN      20672/redis-server  
tcp        0      0 127.0.0.1:6383          0.0.0.0:*               LISTEN      20667/redis-server  
tcp        0      0 127.0.0.1:16379         0.0.0.0:*               LISTEN      20676/redis-server  
tcp        0      0 127.0.0.1:16382         0.0.0.0:*               LISTEN      20672/redis-server  
tcp        0      0 127.0.0.1:16383         0.0.0.0:*               LISTEN      20667/redis-server  
root@test-3:/opt/redis-4.0.1/redis-cluster#

root@test-1:/opt/redis-4.0.1/src#
root@test-1:/opt/redis-4.0.1/src#ps -ef|grep redis
root     19399 13207  0 14:05 pts/0    00:00:00 redis-server 127.0.0.1:6379 [cluster]
root     19403 13207  0 14:05 pts/0    00:00:00 redis-server 127.0.0.1:6380 [cluster]
root     19407 13207  0 14:05 pts/0    00:00:00 redis-server 127.0.0.1:6381 [cluster]
root     19423 13207  0 14:05 pts/0    00:00:00 grep --color=auto redis
root@test-1:/opt/redis-4.0.1/src#
root@test-1:/opt/redis-4.0.1/src#
root@test-1:/opt/redis-4.0.1/src#netstat  -anltp|grep redis
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      19399/redis-server  
tcp        0      0 127.0.0.1:6380          0.0.0.0:*               LISTEN      19403/redis-server  
tcp        0      0 127.0.0.1:6381          0.0.0.0:*               LISTEN      19407/redis-server  
tcp        0      0 127.0.0.1:16379         0.0.0.0:*               LISTEN      19399/redis-server  
tcp        0      0 127.0.0.1:16380         0.0.0.0:*               LISTEN      19403/redis-server  
tcp        0      0 127.0.0.1:16381         0.0.0.0:*               LISTEN      19407/redis-server  

5、创建redis集群

 redis-trib.rb create --replicas 1 192.168.1.216:6379 192.168.1.216:6380 192.168.1.216:6381 192.168.1.215:6382 192.168.1.215:6383 192.168.1.215:6384
root@test-1:/opt/redis-4.0.1/src# ./redis-trib.rb create --replicas 1 192.168.1.216:6379 192.168.1.216:6380 192.168.1.216:6381 192.168.1.215:6382 192.168.1.215:6383 192.168.1.215:6384
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.216:6379
192.168.1.215:6382
192.168.1.216:6380
Adding replica 192.168.1.215:6383 to 192.168.1.216:6379
Adding replica 192.168.1.216:6381 to 192.168.1.215:6382
Adding replica 192.168.1.215:6384 to 192.168.1.216:6380
M: 4fe30cd6d7f68496bd0182cb4e290c6807d5914b 192.168.1.216:6379
   slots:0-5460 (5461 slots) master
M: 879dcd925764ba491f439a04547184d00b9251ce 192.168.1.216:6380
   slots:10923-16383 (5461 slots) master
S: 31ce801811e6ba2dfd2cc520c3272eb41d52034b 192.168.1.216:6381
   replicates eb3821bab3cb2e6baec6993bb56764874696ffde
M: eb3821bab3cb2e6baec6993bb56764874696ffde 192.168.1.215:6382
   slots:5461-10922 (5462 slots) master
S: d59ac73c11e068b3b1b14e45a1e3b6f6d4f0c2d6 192.168.1.215:6383
   replicates 4fe30cd6d7f68496bd0182cb4e290c6807d5914b
S: 26a70b0b02e76131d82ce9f4a80f7a4f357025db 192.168.1.215:6384
   replicates 879dcd925764ba491f439a04547184d00b9251ce
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
19995:M 08 Aug 15:05:11.718 # configEpoch set to 1 via CLUSTER SET-CONFIG-EPOCH
20007:M 08 Aug 15:05:11.718 # configEpoch set to 2 via CLUSTER SET-CONFIG-EPOCH
20011:M 08 Aug 15:05:11.718 # configEpoch set to 3 via CLUSTER SET-CONFIG-EPOCH
>>> Sending CLUSTER MEET messages to join the cluster





原创粉丝点击