centos6安装redis3.2.5

一。redis介绍和安装

1.redis介绍:

      Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010年3月15日起,Redis的开发工作由VMware主持。从2013年5月开始,Redis的开发由Pivotal赞助,edis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。

2.安装redis :

 下载 redist最新版本: http://download.redis.io/releases/redis-3.2.5.tar.gz    

 tar zxvf redis-3.2.5.tar.gz   
 cd redis3.2.5
 make

4安装错误解决

 redis提供的是源代码 需要使用gcc和tcl库

    

 异常一: make[2]: cc: Command not found
     异常原因:没有安装gcc
    解决方案:yum install gcc
    
 异常二:zmalloc.h:51:31: error: jemalloc/jemalloc.h: No such file or directory
    异常原因:一些编译依赖或原来编译遗留出现的问题
    解决方案:make distclean。清理一下,然后再make。

在make成功以后,需要make test。在make test出现异常。

 异常三:couldn't execute "tclsh8.5": no such file or directory
   异常原因:没有安装tcl
  解决方案:yum install tcl。

安装完成后  在redis目录src目录下 会生成很多文件 其中 redis-server是启动命令   redis-cli是客户端命令

在redis根目录下 存在redis.conf 是默认的配置文件

二。启动和关闭edis

    1启动redis 

可以将redis-server和redis-cli脚本拷贝到 /usr/local/bin目录 直接可以运行  

        [root@localhost src]# cp redis-server /usr/bin
        [root@localhost src]# cp redis-cli /usr/bin

       拷贝默认的配置文件到指定目录 比如

       cp ~/redis3.2.5/redis.conf    /etc/redis.conf
       nohup redis-server /etc/redis.conf &

       这里使用 nohup &将当前进程切换为守护进程 也可以修改 redis.conf 将参数 daemonize no 修改为yes 自动进入守护进程

     使用客户端登录后 

   redis-cli 
      ping  出现pong即可
    127.0.0.1:6379> set a 1   设置一个键值对
      OK
    127.0.0.1:6379> get a     通过键获取值
      "1"    

  2关闭redis

  [root@localhost redis-3.2.5]# redis-cli
127.0.0.1:6379> shutdown
not connected> exit  

二。远程连接redis

  1.在window上安装redis的客户端  

  下载地址:https://github.com/dmajkic/redis/downloads  下载后包含了32位和64位版本 将带有exe文件的目录 加入到环境变量path中

   打开cmd 执行 

redis-cli -h   redis-server的ip地址

  此时报错无法连接 :

 》 需要修改 redis.conf

  将绑定的回环地址 bind 127.0.0.1 绑定添加局域网的地址 

 比如修改为: bind 127.0.0.1 192.168.8.130

 》 刷新防火墙 或者关闭防火墙

    iptables --flush  或者 service iptables stop

2.设置服务器的密码

  默认用户可以直接指定ip就可以远程连接到服务器  可以修改 redis-server配置找到参数  

       requirepass 需要设置的密码

客户端不设置密码 会出现

   C:\Users\jiaozi>redis-cli -h 192.168.58.130
redis 192.168.58.130:6379> get a
(error) NOAUTH Authentication required.

 客户端此时登录需要指定 requirepass 设置的密码

   edis-cli -h 启动 redis-server的ip地址  -a 密码


数据类型参考

    http://www.runoob.com/redis/redis-strings.html


三。redis主从配置

   模拟环境

        主 192.168.58.144

        从  192.168.58.145

   主机和从机都安装redis 过程参考章节二

   修改主机和从机目录下的redis.conf

  主机(58.144)配置

daemonize yes  #后台运行
bind 192.168.58.144 #本机的ip
port 6379 #主机端口
 从机(58.145)配置

daemonize yes
bind 192.168.58.145 #本机的ip
port 6379 #主机端口
slaveof 192.168.58.145 6379

分别启动主从redis

 redis-server ~/redis-3.2.5/redis.conf
登陆 58.144 设置键值对

[root@bogon redis-3.2.5]# redis-cli -h 192.168.58.144
192.168.58.144:6379> ping
PONG
192.168.58.144:6379> set a 1
OK
登陆58.145获取 a成功   从机只能读不能写

[root@bogon redis-3.2.5]# redis-cli -h 192.168.58.145
192.168.58.145:6379> get a
"1"
192.168.58.145:6379> set b 1
(error) READONLY You can't write against a read only slave.


测试主从切换

  测试停止 主机 58.145  

redis-cli -h 192.168.58.144 shutdown
  操作从机 发现仍然是无法修改
[root@bogon redis-3.2.5]# redis-cli -h 192.168.58.145
192.168.58.145:6379> get a
"1"
192.168.58.145:6379> set a 1
(error) READONLY You can't write against a read only slave.
将从机切换为主机 让从机接替主机继续工作 可以写入
[root@bogon redis-3.2.5]# redis-cli -h 192.168.58.145
192.168.58.145:6379> slaveof NO ONE
OK
操作从机发现可以写入

[root@bogon redis-3.2.5]# redis-cli -h 192.168.58.145
192.168.58.145:6379> set b 1
OK

如果主机恢复了需要重新恢复 需要将从机dump文件拷贝到主机  
[root@bogon redis-3.2.5]# redis-cli -h 192.168.58.145
192.168.58.145:6379> save  #将从机的数据备份到dum文件
OK
192.168.58.145:6379> exit
[root@bogon redis-3.2.5]# ll
total 208
-rw-rw-r--  1 root root 78892 Oct 26  2016 00-RELEASENOTES
-rw-rw-r--  1 root root    53 Oct 26  2016 BUGS
-rw-rw-r--  1 root root  1805 Oct 26  2016 CONTRIBUTING
-rw-rw-r--  1 root root  1487 Oct 26  2016 COPYING
drwxrwxr-x  7 root root  4096 Sep 11 03:05 deps
-rw-r--r--  1 root root    76 Sep 11 03:31 dump.rdb
-rw-rw-r--  1 root root    11 Oct 26  2016 INSTALL
-rw-rw-r--  1 root root   151 Oct 26  2016 Makefile
-rw-rw-r--  1 root root  4223 Oct 26  2016 MANIFESTO
-rw-rw-r--  1 root root  6834 Oct 26  2016 README.md
-rw-rw-r--  1 root root 46695 Oct 26  2016 redis.conf
-rwxrwxr-x  1 root root   271 Oct 26  2016 runtest
-rwxrwxr-x  1 root root   280 Oct 26  2016 runtest-cluster
-rwxrwxr-x  1 root root   281 Oct 26  2016 runtest-sentinel
-rw-rw-r--  1 root root  7606 Oct 26  2016 sentinel.conf
drwxrwxr-x  2 root root  4096 Sep 11 03:06 src
drwxrwxr-x 10 root root  4096 Oct 26  2016 tests
drwxrwxr-x  7 root root  4096 Oct 26  2016 utils
[root@bogon redis-3.2.5]# scp ./dump.rdb root@192.168.58.144:/root/redis-3.2.5/ 将58.145redis下dump.rdb拷贝会主机144 因为从机有可能之后又写入数据
The authenticity of host '192.168.58.144 (192.168.58.144)' can't be established.
ECDSA key fingerprint is bd:90:ae:1c:0f:d8:d8:6d:81:8d:3d:4b:cd:2e:1f:21.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.58.144' (ECDSA) to the list of known hosts.
root@192.168.58.144's password: 
dump.rdb                                                
重启 58.144 58.145 重新测试发现主机可写从机可读 

四。redis集群

   redis主从是对主的拷贝对于数据量的问题 无法解决 所以redis3之后出现了 redis集群设计 

   redis cluster 考虑到了去中心化,去中间件,也就是说,集群中的每个节点都是平等的关系,都是对等的,每个节点都保存各自的数据和整个集群的状态。每个节点都和其他所有节点连接,而且这些连接保持活跃,这样就保证了我们只需要连接集群中的任意一个节点,就可以获取到其他节点的数据。
redis 集群没有使用传统的一致性哈希来分配数据,而是采用另外一种叫做哈希槽 (hash slot)的方式来分配的。redis cluster 默认分配了 16384 个slot,当我们set一个key 时,会用CRC16算法来取模得到所属的slot,然后将这个key 分到哈希槽区间的节点上,具体算法就是:CRC16(key) % 16384。所以我们在测试的时候看到set 和 get 的时候,直接跳转到了7000端口的节点。


Redis 集群会把数据存在一个 master 节点,然后在这个 master 和其对应的salve 之间进行数据同步。当读取数据时,也根据一致性哈希算法到对应的 master 节点获取数据。只有当一个master 挂掉之后,才会启动一个对应的 salve 节点,充当 master 。


    模拟环境(每台机器开启两个redis 端口是6379,6380)

        机器1 192.168.58.144

        机器2  192.168.58.145

        机器3  192.168.58.146

   三台机器分别安装redis  (redis安装目录  /root/redis-3.2.5)

   三台机器安装目录下 新建cluster目录 cluster目录下新建 6379和6380目录 分别拷贝redis.conf到该目录下

   编辑6379和6380下的redis.conf

    vi 6379/reids.conf 修改

daemonize yes
port 6379   
bind 192.168.58.144 #本机的ip 每台机器设置自己的ip
cluster-enabled yes
cluster-config-file nodes-6379.conf  #集群配置文件redis启动时自动生成
cluster-node-timeout 15000 #集群redis互相连接超时时间单位ms 默认 15s
pidfile /var/run/redis_6379.pid #一台机器开启两个redis所有将两个reids的pid文件指向不同位置
      vi 6380/reids.conf 修改

daemonize yes
port 6380   
bind 192.168.58.144 #本机的ip 
cluster-enabled yes
cluster-config-file nodes-6380.conf  #集群配置文件redis启动时自动生成
cluster-node-timeout 15000 #集群redis互相连接超时时间单位ms 默认 15s
pidfile /var/run/redis_6380.pid #一台机器开启两个redis所有将两个reids的pid文件指向不同位置
依次启动每台机器的两个redis服务

[root@bogon 6380]# redis-server ~/redis-3.2.5/cluster/6379/redis.conf
[root@bogon 6380]# redis-server ~/redis-3.2.5/cluster/6380/redis.conf
[root@bogon 6380]# ps -ef | grep redis
root      11228      1  0 05:13 ?        00:00:00 redis-server 192.168.58.146:6379 [cluster]
root      11232      1  0 05:13 ?        00:00:00 redis-server 192.168.58.146:6380 [cluster]
尝试操作任意一台

[root@bogon 6380]# redis-cli -h 192.168.58.144
192.168.58.144:6379> set a 1
(error) CLUSTERDOWN Hash slot not served  表示集群还没有服务 
这里需要通过 redis提供的redis-trib.rb 创建集群 分成主从后 才能提供服务

 6台redis 只有三台是master  三台是slave 集群中三台master对外提供 三台slave负载备份高可用

执行该命令

[root@bogon src]# pwd
/root/redis-3.2.5/src
[root@bogon src]# ./redis-trib.rb
/usr/bin/env: ruby: No such file or directory  需要ruby环境
安装ruby
 yum -y install ruby ruby-devel

安装ruby的redis支持包

gem install redis
    ERROR:  Error installing redis:
            redis requires Ruby version >= 2.2.2 这里可以看到 需要ruby2.2.2以上版本

系统版本为

[root@bogon 6380]# ruby --version
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
可以使用rvm管理安装ruby

  gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
  ll
  curl -L get.rvm.io | bash -s stable
  source /usr/local/rvm/scripts/rvm
  rvm list known
  rvm install 2.3.4
  rvm use 2.3.4
  rvm use 2.3.4 --default
  rvm remove 2.0.0
  ruby --version
  gem install redis

再次测试 可以看到该命令的帮助出现

[root@bogon src]# ./redis-trib.rb 
Usage: redis-trib <command> <options> <arguments ...>

  create          host1:port1 ... hostN:portN
                  --replicas <arg>
  check           host:port
  info            host:port
  fix             host:port
                  --timeout <arg>
  reshard         host:port
                  --from <arg>
                  --to <arg>
                  --slots <arg>
                  --yes
                  --timeout <arg>
                  --pipeline <arg>
  rebalance       host:port
                  --weight <arg>
                  --auto-weights
                  --use-empty-masters
                  --timeout <arg>
                  --simulate
                  --pipeline <arg>
                  --threshold <arg>
  add-node        new_host:new_port existing_host:existing_port
                  --slave
                  --master-id <arg>
  del-node        host:port node_id
  set-timeout     host:port milliseconds
  call            host:port command arg arg .. arg
  import          host:port
                  --from <arg>
                  --copy
                  --replace
  help            (show this help)
创建集群 在任意一台机器上执行该命令即可

[root@bogon src]# ./redis-trib.rb  create  --replicas  1  192.168.58.144:6379 192.168.58.144:6380 192.168.58.145:6379 192.168.58.145:6380 192.168.58.146:6379 192.168.58.146:6380
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.58.144:6379
192.168.58.145:6379
192.168.58.146:6379
Adding replica 192.168.58.145:6380 to 192.168.58.144:6379
Adding replica 192.168.58.144:6380 to 192.168.58.145:6379
Adding replica 192.168.58.146:6380 to 192.168.58.146:6379
输入 yes  表示确认 6379全部是主机  6380全是从机  replicas  1表示每个master都需要分配一个从 

测试是否成功 客户端必须加入 -c 选项 表示集群某则会抛出错误

[root@bogon src]# redis-cli -c -h 192.168.58.146
192.168.58.146:6379> set hello good
-> Redirected to slot [866] located at 192.168.58.144:6379  通过hash槽自动重定向到58.144上存储去了
OK












转载于:https://www.cnblogs.com/liaomin416100569/p/9331231.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值