Redis Installation & Basic Commands

Redis,技术帖,数据库知识 2017-11-15

A.Redis Installation

1.set root password:

    $sudo passwd
    input the password already changed and get the root authority:
    $su the redis version:

    for example:version-2.8.9 for Redis version-2.4.2 for Jedis(redis for java) 

3.get into thr root directory and download the redisJar:


4.unzip the redisJar:

    $tar -xzvf redis-2.8.9.tar.gz

5.move into the new directory redis-2.8.9 and compile it:

    $cd redis-2.8.9

6.check whether redis has been installed successfully:

    $make test

7.use ls command to check important files:

    default server:src/redis-server
    default client:src/redis-cli
    default configuration file:redis.conf

8.copy the excutable files into $path for saving time:

    $cp redis-server  /usr/local/bin/
    $cp redis-cli    /usr/local/bin/

9.start redis-server(default port:6379):

    $redis-server(Ctrl+Shift+t to open another terminal)

10.check the redis:

    $ps -ef | grep redis
    $netstat -nlt | grep 6379

11.start redis-client:

    $su(input the password)

12.enjoy the redis!

B.Redis Data Type

1.Redis Strings

    $sudo service redis-server start
1)set & get
    >set mykey somevalue
    >get mykey
    >set mykey newval nx
    >set mykey newval xx
3)incr & decr
    >set counter 100
    >incr counter
    >incr counter
    >incrby counter 50
4)mset & mget
    >mset a 10 b 20 c 30
    >mget a b c

2.Redis Lists
1)(l/r)push & lrange

    >rpush mylist A
    >rpush mylist B
    >lpush mylist first
    >rpush mylist 1 2 3 4 5 "foo bar"
    >lrange mylist 0 -1(0:the first one,-1:the last one)
2)del & pop(l/r)
    >del mylist
    >rpush mylist a b c 
    >rpop mylist
    >lrange mylist 0 -1
    >lpop mylist
    >lrange mylist 0 -1

3.Redis Hashes
1)hmset & hget & hmget

    >hmset user:1000 username antirez birthyear 1977 verified 1
    >hget user:1000 username
    >hget user:1000 birthyear
    >hgetall user:1000
    >hmget user:1000 username birthyear no-such-filed
    >hincrby user:1000 birthyear 10
    >hincrby user:1000 birthyear 10

4.Redis Sets(no sequence)

    1)sadd & smembers
    >sadd myset 1 2 3
    >smembers myset
    >sismember myset 3
    >sismember myset 30
    >sismember mys 3

5.Redis Sets(sequenced)
1)zadd & zrange & zrevrange(0:the first one -1:the last one)

    >zadd hackers 1940 "aac"
    >zadd hackers 1950 "aab"
    >zadd hackers 1989 "zzz"
    >zrange hackers 0 -1
    >zrevrange hackers 0 -1
    >zrange hackers 0 -1 withscores(return with param)


C.Redis System Management

1.Commonly Used Commands
1)Exists & Del

    >set mykey hello
    >exists mykey
    >del mykey
    >exists mykey
2)Type & Keys
    >set mykey x
    >type mykey
    >keys my*
    >del mykey
    >keys my*
    >type mykey
3)Randomkey & Clear
4)Rename & Renamenx
(nx:not exist[if the new name exists=>failed])
    >rename mylist newlist  
    >exists mylist  
    >exists newlist
(return sum(key))

2.Redis Time-related Commands
set keys' survive time(it could avoid plenty of "del" commands)

    >set key some-value
    >expire key 10
    >get key (immediately)
    >get key (10 seconds later)
2)TTL(to query how long does the key remain)
    >set key 100 ex 30
    >ttl key
    >ttl key
3)Delete key
    >flushDB(clear current database)
    >flushAll(clear all)

3.Redis Configuration-related Commands
1)Config Get & Config Set & Auth

    >config get requirepass(to see the password)
    >config set requirepass test123(set the password "test123") 
    >config get repuirepass(failed=>no authentication)
    >auth test123(authenticated)
    >config get requirepass

We can also change the password by updating file "redis.conf".

    >config get *max-*-entries*
2)Config Resetstat

Info [section]:we can almost check everything about redis.

    >info keyspace
    >info server
    >info client
    >info memory
    >info persistence
    >info stats 
    >info replication
    >info cpu
    >info cluster
    >info all
    >info default


本文由 BF 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。