redis3.0.3 安装与配置

看到redis3版本已出最新 3.0.3 测试了一下它的集群功能。



1.安装:

    $ wget http://download.redis.io/releases/redis-3.0.3.tar.gz
    $ tar xzf redis-3.0.3.tar.gz
    $ cd redis-3.0.3

    #支持包没有,问题真多,安装了gcc,tcl

    [root@t1 redis-3.0.3]# ./runtest
    You need tcl 8.5 or newer in order to run the Redis test

    [root@t1 redis-3.0.3]# yum install tcl
    已加载插件:fastestmirror, product-id, subscription-manager
    Updating certificate-based repositories.
    Unable to read consumer identity
    设置安装进程
    Loading mirror speeds from cached hostfile
    base                                    

    ......

    [root@t2 redis-3.0.3]# make
    cd src && make all
    make[1]: Entering directory `/opt/redis-3.0.3/src'
    rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-dump redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html
    (cd ../deps && make distclean)
    make[2]: Entering directory `/opt/redis-3.0.3/deps'
    (cd hiredis && make clean) > /dev/null || true
    (cd linenoise && make clean) > /dev/null || true
    (cd lua && make clean) > /dev/null || true
    (cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true
    (rm -f .make-*)
    make[2]: Leaving directory `/opt/redis-3.0.3/deps'
    (rm -f .make-*)
    echo STD=-std=c99 -pedantic >> .make-settings
    echo WARN=-Wall -W >> .make-settings
    echo OPT=-O2 >> .make-settings
    echo MALLOC=jemalloc >> .make-settings
    echo CFLAGS= >> .make-settings
    echo LDFLAGS= >> .make-settings
    echo REDIS_CFLAGS= >> .make-settings
    echo REDIS_LDFLAGS= >> .make-settings
    echo PREV_FINAL_CFLAGS=-std=c99 -pedantic -Wall -W -O2 -g -ggdb   -I../deps/hiredis -I../deps/linenoise -I../deps/lua/src -DUSE_JEMALLOC -I../deps/jemalloc/include >> .make-settings
    echo PREV_FINAL_LDFLAGS=  -g -ggdb -rdynamic >> .make-settings
    (cd ../deps && make hiredis linenoise lua jemalloc)
    make[2]: Entering directory `/opt/redis-3.0.3/deps'
    (cd hiredis && make clean) > /dev/null || true
    (cd linenoise && make clean) > /dev/null || true
    (cd lua && make clean) > /dev/null || true
    (cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true
    (rm -f .make-*)
    (echo "" > .make-ldflags)
    (echo "" > .make-cflags)
    MAKE hiredis
    cd hiredis && make static
    make[3]: Entering directory `/opt/redis-3.0.3/deps/hiredis'
    gcc -std=c99 -pedantic -c -O3 -fPIC  -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb  net.c
    make[3]: gcc:命令未找到
    make[3]: *** [net.o] 错误 127
    make[3]: Leaving directory `/opt/redis-3.0.3/deps/hiredis'
    make[2]: *** [hiredis] 错误 2
    make[2]: Leaving directory `/opt/redis-3.0.3/deps'
    make[1]: [persist-settings] 错误 2 (忽略)
            CC adlist.o
    /bin/sh: cc: command not found
    make[1]: *** [adlist.o] 错误 127
    make[1]: Leaving directory `/opt/redis-3.0.3/src'
    make: *** [all] 错误 2


        [root@t2 redis-3.0.3]# yum install gcc
        已加载插件:fastestmirror, product-id, subscription-manager
        Updating certificate-based repositories.
        Unable to read consumer identity
        设置安装进程
        Repository base is listed more than once in the configuration
        Repository updates is listed more than once in the configuration
        Repository extras is listed more than once in the configuration
        Repository centosplus is listed more than once in the configuration
        Repository contrib is listed more than once in the configuration
        Loading mirror speeds from cached hostfile
         * base: ftp.sjtu.edu.cn
         * extras: mirrors.163.com
         * updates: centos.ustc.edu.cn

    $ make

    出错:
        [root@t1 redis-3.0.3]# make
        cd src && make all
        make[1]: Entering directory `/opt/redis-3.0.3/src'
            CC adlist.o
        在包含自 adlist.c:34 的文件中:
        zmalloc.h:50:31: 错误:jemalloc/jemalloc.h:没有那个文件或目录
        zmalloc.h:55:2: 错误:#error "Newer version of jemalloc required"
        make[1]: *** [adlist.o] 错误 1
        make[1]: Leaving directory `/opt/redis-3.0.3/src'
        make: *** [all] 错误 2


        解决:
            To force compiling against libc malloc, use:  
            
                % make MALLOC=libc  

        (错误解决URL:http://www.phperz.com/article/14/1219/42002.html)



2.配置

    2.1、新建6个instance,3个master,3个slave;


    1.    192.168.2.188:6379        
    2.    192.168.2.188:6380
    3.    192.168.2.132:6379        
    4.    192.168.2.132:6380
    5.    192.168.2.134:6379        
    6.    192.168.2.134:6380



    新建实例之前要改一下redis.conf的内容,先改端口号,再改下面的cluster配置项


    # Accept connections on the specified port, default is 6379.
    # If port 0 is specified Redis will not listen on a TCP socket.
    port 6379

    ################################ REDIS CLUSTER ###############################
    #
    # Normal Redis instances can't be part of a Redis Cluster;only nodes that are
    # started as cluster nodes can. In order to start a Redisinstance as a
    # cluster node enable the cluster support uncommenting thefollowing:
    #
    cluster-enabled yes
    # Every cluster node has a cluster configuration file. Thisfile is not
    # intended to be edited by hand. It is created and updated byRedis nodes.
    # Every Redis Cluster node requires a different clusterconfiguration file.
    # Make sure that instances running in the same system does nothave
    # overlapping cluster configuration file names.
    #
    cluster-config-file nodes-6379.conf
    # Cluster node timeout is the amount of seconds a node must beunreachable
    # for it to be considered in failure state.
    # Most other internal time limits are multiplicators of thenode timeout.
    #
    cluster-node-timeout 15
    # In order to setup your cluster make sure to read thedocumentation
    # available at http://redis.io web site.

    #复制配置文件,并修改port: 6380, cluster-config-file nodes-6380.conf
    cp redis.conf redis2.conf


    启动6个实例:

    [root@t1 redis-3.0.3]# src/redis-server redis.conf &
    [root@t1 redis-3.0.3]# src/redis-server redis2.conf &

    [root@t1 redis-3.0.3]# ps -ef|grep redis
    root      3105  2024  0 14:36 pts/0    00:00:02 src/redis-server *:6379 [cluster]
    root      3191  2024  0 15:04 pts/0    00:00:00 src/redis-server *:6380 [cluster]
    root      3197  2024  0 15:04 pts/0    00:00:00 grep redis


    [root@t2 redis-3.0.3]# src/redis-server redis.conf &
    [root@t2 redis-3.0.3]# src/redis-server redis2.conf &
    [root@t2 redis-3.0.3]# ps -ef|grep redis
    root     26035 17602  0 15:05 pts/2    00:00:00 src/redis-server *:6379 [cluster]
    root     26043 17602  0 15:05 pts/2    00:00:00 src/redis-server *:6380 [cluster]
    root     26057 17602  0 15:06 pts/2    00:00:00 grep redis


    [root@localhost redis-3.0.3]# src/redis-server redis.conf &
    [root@localhost redis-3.0.3]# src/redis-server redis2.conf &
    [root@localhost redis-3.0.3]# ps -ef|grep redis
    root      5689  1249  0 15:08 pts/0    00:00:00 src/redis-server *:6379 [cluster]
    root      5692  1249  0 15:08 pts/0    00:00:00 src/redis-server *:6380 [cluster]
    root      5696  1249  0 15:08 pts/0    00:00:00 grep redis



    2.2 建立集群:
        2.2.1 安装ruby 及 rubygems,
    [root@t1 redis-3.0.3]# yum install ruby
    [root@t1 redis-3.0.3]# yum install rubygems
    [root@t1 redis-3.0.3]# gem install redis    #安装ruby 的redis 接口支持包

    2.2.2 测试是否能连接上其它服务器的redis-server
    [root@t1 redis-3.0.3]# src/redis-cli -h 192.168.2.134 -p 6379
    Could not connect to Redis at 192.168.2.134:6379: No route to host
    not connected> exit
    
    2.2.3 关闭其它服务器中的防火墙
    [root@t2 redis-3.0.3]# chkconfig iptables off
    [root@t2 redis-3.0.3]# service iptables stop
    iptables:清除防火墙规则:                                 [确定]
    iptables:将链设置为政策 ACCEPT:filter                    [确定]
    iptables:正在卸载模块:                                   [确定]

    2.2.4 建立集群:

    [root@t1 redis-3.0.3]# src/redis-trib.rb create --replicas 1 192.168.2.188:6379 192.168.2.188:6380 192.168.2.132:6379 192.168.2.132:6380 192.168.2.134:6379 192.168.2.134:6380
    >>> Creating cluster
    Connecting to node 192.168.2.188:6379: OK
    Connecting to node 192.168.2.188:6380: OK
    Connecting to node 192.168.2.132:6379: OK
    Connecting to node 192.168.2.132:6380: OK
    Connecting to node 192.168.2.134:6379: OK
    Connecting to node 192.168.2.134:6380: OK
    >>> Performing hash slots allocation on 6 nodes...
    Using 3 masters:
    192.168.2.134:6379
    192.168.2.132:6379
    192.168.2.188:6379
    Adding replica 192.168.2.132:6380 to 192.168.2.134:6379
    Adding replica 192.168.2.134:6380 to 192.168.2.132:6379
    Adding replica 192.168.2.188:6380 to 192.168.2.188:6379
    M: 811daf9b3f895bf891c65cee5b8e70a9f8fd4c8a 192.168.2.188:6379
         slots:10923-16383 (5461 slots) master
    S: 902fd7e670ece8f4b993b930e1619d3e5ee89b0f 192.168.2.188:6380
         replicates 811daf9b3f895bf891c65cee5b8e70a9f8fd4c8a
    M: cc6c9301a0fc8e97ce7c00bb13529552323f6f60 192.168.2.132:6379
         slots:5461-10922 (5462 slots) master
    S: b55836ddfeb279b4cab60faec8ec53acf0da075e 192.168.2.132:6380
         replicates 9d296647ae6cac139a817d46ab3efef50290def8
    M: 9d296647ae6cac139a817d46ab3efef50290def8 192.168.2.134:6379
         slots:0-5460 (5461 slots) master
    S: 91cab39311b0bd69ccf5de9608778d271064e087 192.168.2.134:6380
         replicates cc6c9301a0fc8e97ce7c00bb13529552323f6f60
    Can I set the above configuration? (type 'yes' to accept): yes
    >>> Nodes configuration updated

    #查看 集群信息
    [root@t1 redis-3.0.3]# src/redis-cli -p 6379
    127.0.0.1:6379> 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:1
    cluster_stats_messages_sent:1305
    cluster_stats_messages_received:1305
    127.0.0.1:6379>

    
    127.0.0.1:6379> set hello tang
    OK
    127.0.0.1:6379> keys *
    1) "hello"
    127.0.0.1:6379> set user_id 1234
    OK
    127.0.0.1:6379> keys *
    1) "user_id"
    2) "hello"
    127.0.0.1:6379> get user_id
    "1234"

    127.0.0.1:6379>





    [root@t2 redis-3.0.3]# src/redis-cli -p 6380
    127.0.0.1:6380> keys *
    1) "user_id"
    2) "hello"
    127.0.0.1:6380> get user_id
    (error) MOVED 3139 192.168.2.134:6379
    127.0.0.1:6380> get hello
    (error) MOVED 866 192.168.2.134:6379
    127.0.0.1:6380> keys *
    1) "user_id"
    2) "hello"
    127.0.0.1:6380> exit

    #当使用参数 -c 登录后,会自动取到其它节点中的数据
    [root@t2 redis-3.0.3]# src/redis-cli -c -p 6380
    127.0.0.1:6380> get user_id
    -> Redirected to slot [3139] located at 192.168.2.134:6379
    "1234"
    192.168.2.134:6379> get hello
    "tang"
    192.168.2.134:6379> keys *
    1) "user_id"
    2) "hello"
    192.168.2.134:6379> get year
    -> Redirected to slot [10071] located at 192.168.2.132:6379
    "2015"
    192.168.2.132:6379> get month
    "08"
    192.168.2.132:6379> get day
    -> Redirected to slot [4110] located at 192.168.2.134:6379
    (nil)
    192.168.2.134:6379>


双击 redis-server.exe运行服务端。 双击 redis-cli.exe 运行一个客户端 此时可以通过一个命令来展示redis的功能。redis的命令如下: 连接控制 QUIT 关闭连接 AUTH (仅限启用时)简单的密码验证 适合全体类型的命令 EXISTS key 判断一个键是否存在;存在返回 1;否则返回0; DEL key 删除某个key,或是一系列key;DEL key1 key2 key3 key4 TYPE key 返回某个key元素的数据类型 ( none:不存在,string:字符,list,set,zset,hash) KEYS pattern 返回匹配的key列表 (KEYS foo*:查找foo开头的keys) RANDOMKEY 随机获得一个已经存在的key,如果当前数据库为空,则返回空字符串 RENAME oldname newname更改key的名字,新键如果存在将被覆盖 RENAMENX oldname newname 更改key的名字,如果名字存在则更改失败 DBSIZE返回当前数据库的key的总数 EXPIRE设置某个key的过期时间(秒),(EXPIRE bruce 1000:设置bruce这个key1000秒后系统自动删除)注意:如果在还没有过期的时候,对值进行了改变,那么那个值会被清除。 TTL查找某个key还有多长时间过期,返回时间秒 SELECT index 选择数据库 MOVE key dbindex 将指定键从当前数据库移到目标数据库 dbindex。成功返回 1;否则返回0(源数据库不存在key或目标数据库已存在同名key); FLUSHDB 清空当前数据库中的所有键 FLUSHALL 清空所有数据库中的所有键 处理字符串的命令 SET key value 给一个键设置字符串值。SET keyname datalength data (SET bruce 10 paitoubing:保存key为burce,字符串长度为10的一个字符串paitoubing到数据库),data最大不可超过1G。 GET key获取某个key 的value值。如key不存在,则返回字符串“nil”;如key的值不为字符串类型,则返回一个错误。 GETSET key value可以理解成获得的key的值然后SET这个值,更加方便的操作 (SET bruce 10 paitoubing,这个时候需要修改bruce变成1234567890并获取这个以前的数据paitoubing,GETSET bruce 10 1234567890) MGET key1 key2 … keyN 一次性返回多个键的值 SETNX key value SETNX与SET的区别是SET可以创建与更新key的value,而SETNX是如果key不存在,则创建key与value数据 MSET key1 value1 key2 value2 … keyN valueN 在一次原子操作下一次性设置多个键和值 MSETNX key1 value1 key2 value2 … keyN valueN 在一次原子操作下一次性设置多个键和值(目标键不存在情况下,如果有一个以上的key已存在,则失败) INCR key 自增键值 INCRBY key integer 令键值自增指定数值 DECR key 自减键值 DECRBY key integer 令键值自减指定数值 处理 lists 的命令 RPUSH key value 从 List 尾部添加一个元素(如序列不存在,则先创建,如已存在同名Key而非序列,则返回错误) LPUSH key value 从 List 头部添加一个元素 LLEN key 返回一个 List 的长度 LRANGE key start end从自定的范围内返回序列的元素 (LRANGE testlist 0 2;返回序列testlist前0 1 2元素) LTRIM key start end修剪某个范围之外的数据 (LTRIM testlist 0 2;保留0 1 2元素,其余的删除) LINDEX key index返回某个位置的序列值(LINDEX testlist 0;返回序列testlist位置为0的元素) LSET key index value更新某个位置元素的值 LREM key count value 从 List 的头部(count正数)或尾部(count负数)删除一定数量(count)匹配value的元素,返回删除的元素数量。 LPOP key 弹出 List 的第一个元素 RPOP key 弹出 L
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值