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 

2.select 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:

    $cd
    $wget http://labfile.oss.aliyuncs.com/files0422/redis-2.8.9.tar.gz

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
    $make

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)
    $cd
    $redis-cli

12.enjoy the redis!

B.Redis Data Type

1.Redis Strings

    $sudo service redis-server start
    $redis-cli
1)set & get
    >set mykey somevalue
    >get mykey
2)
    >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
2)hincrby 
    >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
2)sismember(1:true/0:false)
    >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)

link: http://redis.io/topics/data-types-intro
http://www.cnblogs.com/guanjinke/archive/2013/01/21/2870576.html

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
    >randomkey
    >clear
4)Rename & Renamenx
(nx:not exist[if the new name exists=>failed])
    >randomkey  
    >rename mylist newlist  
    >exists mylist  
    >exists newlist
5)Dbsize
(return sum(key))
    >dbsize

2.Redis Time-related Commands
1)Expire(param:seconds)
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
    >CONFIG RESETSTAT

4.Info
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
    >...

link http://redis.io/commands/config-resetstat
http://redisdoc.com/
https://redis.io/commands
https://redis.io/documentation


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

楼主残忍的关闭了评论

bst g22 jinniu lilai opebet orange88 vinbet xbet yuebo zunlong shijiebei bet007 hg0088 ju111 letiantang m88 mayaba qg777 qianyiguoji sbf777 tengbohui tlc ule weilianxier waiweitouzhu xingfayule xinhaotiandi yinheyule youfayule zhongying 2018shijiebei w88 18luck 188bet beplay manbet 12bet 95zz shenbo weide1946 ca88 88bifa aomenxinpujing betway bodog bt365 bwin tongbao vwin weinisiren 88jt fenghuangyule hongyunguoji 918botiantang huanyayule jianada28 jixiangfang libo long8 hongzuyishi zuqiutouzhu