basic operation of Redis database base
1, Introduction:
The REDIS,K-V database can contain data types that have strings, hashes, lists, collections, and ordered collections.
data types support Push/pop,add/remove, intersection, and set, the difference set and other operations
generally redis can be stored in memory, can also periodically write the updated data
into the disk, or the modified operation to write the record file (log)
2. Installation and operation under Simple Linux
Download Address: https://redis.io/download
installation Square Judge Network has
Redis Service Simple Operation (Default port 6379)
1, start Redis
./usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
//need to specify a profile
2, Client Connection:
./USR/LOCAL/REDIS/BIN/REDIS-CLI
3, stop Redis
pkill/usr/local/redis/bin/redis-server
Shutdown/usr/local/redis/bin/redis-server
4, view process launch: Ps-ef | grep redis
5, viewing ports: NETSTAT-TUNPL | grep 6379
3, data type
1,string
string type, binary security, can store any data, serialize objects, picture binary stream, and so on
operation Method (some and memcache similar) set//Set a value setnx//Set a value, the key exists to return 0, does not exist then set Setex//Set key value pair of colleagues plus time setrange//set replacement Mset//Set Multiple values MSETNX//Set multiple values, there is a return of 0, does not exist then set get Getset//Return to the old value, set the new value getrange//return part mget//Return multiple INCR//return at the same time the increase of Incrby//return at the same time set up Quantity decr Decrby Append//Append to back strlen//return length
Instance: 127.0.0.1:6379> set name Niushaogang OK 127.0.0.1:6379> get name "Niushaogang" 127.0.0.1:6379> Add name (Error) ERR unknown command ' Add ' 127.0.0.1:6379> add name Niushaoji Ng (Error) ERR unknown command ' Add ' 127.0.0.1:6379> get Name "Niushaogang" 127.0.0.1:6
379> Append name niushaojing (integer) 127.0.0.1:6379> get Name "Niushaogangniushaojing" 127.0.0.1:6379> prepend name niuyongding (error) ERR unknown command ' prepend ' 127.0.0.1:6379&G T Prepend 1 (Error) Err unknown command ' prepend ' 127.0.0.1:6379> prepend name 1 (error) Err unkn Own command ' prepend ' 127.0.0.1:6379> get name ' niushaogangniushaojing ' 127.0.0.1:6379> get ID (nil) 127.0.0.1:6379> get Ni (nil) 127.0.0.1:6379> get name "Niushaogangn Iushaojing "127.0.0.1:6379> setns name Sun (error) ERR unknown command ' setns ' 127.0.0.1:6379> setnx name Sun
(integer) 0 127.0.0.1:6379> setex haircolor red OK 127.0.0.1:6379> get Haircolor "Red" 127.0.0.1:6379> get Haircolor (nil) 127.0.0.1:6379> get name "Niushaogang Niushaojing "127.0.0.1:6379> set name (error) ERR wrong number of arguments for ' set ' command 1 27.0.0.1:6379> set name Niushao OK 127.0.0.1:6379> get name "Niushao" 127.0.0.1:6379 > SetRange Name 3 Gang (integer) 7 127.0.0.1:6379> get Name "Niugang" 127.0.0.1:6379 > Mset name1 niushao name2 gangshao ok 127.0.0.1:6379> get name1 "Niushao" 127.0.0.1 :6379> get Name2 "Gangshao" 127.0.0.1:6379> mset name 3 Ganggge (Error) ERR wrong number of
Arguments for Mset 127.0.0.1:6379> mset name3 gangge ok 127.0.0.1:6379> get Name3 "Gangge" 127.0.0.1 :6379> Get Name "Niugang" 127.0.0.1:6379> getset name Niu "Niugang" 127.0.0.1:6379&G T Get name "Niu" 127.0.0.1:6379> getrange Name 2 (error) ERR wrong number of arguments for ' Getra
Nge ' command 127.0.0.1:6379> get range 0 2 (Error) ERR wrong number of arguments for ' get ' command 127.0.0.1:6379> getrange name 0 2 "Niu" 127.0.0.1:6379> getrange name 0 1 "ni" 1 27.0.0.1:6379> mget name name1 name2 1) "NIU" 2) "Niushao" 3 "Gangshao" 127.0.0.1:6379 > Mset id1 3 id2 6 id3 9 ok 127.0.0.1:6379> get Id1 "3" 127.0.0.1:6379> mget ID1 ID2 ID3 1) "3" 2) "6" 3) "9" 127.0.0.1:6379> incr id1 (integer) 4 127.0 .0.1:6379> incr ID2 (integer) 7 127.0.0.1:6379> incr id2 (integer) 8 127.0.0.1:6379> incr Id2 (integer) 9 127.0.0.1:6379> incr id2 (integer) 127.0.0.1:6379> incr id5 (integer)
1 127.0.0.1:6379> incr id5 (integer) 2 127.0.0.1:6379> incrby id5 (integer) 12
127.0.0.1:6379> incrby id5 (integer) 127.0.0.1:6379> decr id5 (integer) 21
127.0.0.1:6379> decr id5 (integer) 127.0.0.1:6379> decrby id5 5 (integer) 15 127.0.0.1:6379> append id5 Niushao (integer) 9 127.0.0.1:6379> get Id5 "15niushao" 1
27.0.0.1:6379> strlen id5 (integer) 9 127.0.0.1:6379>
2,hashes is a mapping table of fields and values. Adding deletes is 0,1. For storage objects,
How to:
hset myhash key value//set a value
hmset myhash key1 value1 key2 value2 ...
Hget myhash key Hmget//hmget myhash key1 key2 key3 hsetnx hincrby
key num//self-Add a value
myhash//Wrong In a key
Hlen //How many keys
hdel //delete hkeys
/ output all the keys hvals// output all the values
Hgetall/ /output all keys and values
Example: 127.0.0.1:6379> hset names:001 Niushaogang (Error) ERR wrong number of arguments for ' hset ' command
127.0.0.1:6379> hset names:001 name Niushaogang (integer) 1 127.0.0.1:6379> get names (nil) 127.0.0.1:6379> Hget name (error) ERR wrong number of arguments for ' hget ' command 127 .0.0.1:6379> hget names::001 (Error) ERR wrong number of arguments for ' hget ' command 127.0.0.1:6379> ; Hget names::001 name (nil) 127.0.0.1:6379> hget names:001 name "Niushaogang" 127.0.0.1: 6379> hsetnx names:002 name niushaojing (integer) 1 127.0.0.1:6379> hset names:002 name (err
OR) ERR wrong number of arguments for ' hset ' command 127.0.0.1:6379> hget names:002 name ' niushaojing ' 127.0.0.1:6379> hmset names:003 name Niushao Sex nan age OK 127.0.0.1:6379> hget names:00
3 Sex "Nan" 127.0.0.1:6379> hmget names:003 name Sex age 1) "Niushao" 2) "Nan" 3) "20" 127.0.
0.1:6379> Hincrby names:003 age 5 (integer) 127.0.0.1:6379> hget names:003 Age "25" 127.0.0.1:6379> hexists names Age (integer) # 0 127.0.0.1:6379> hexists names:003 age (inte GER) 1 127.0.0.1:6379> hlen names:003 (integer) 3 127.0.0.1:6379> hlen names:001 (in Teger) 1 127.0.0.1:6379> hlen names (integer) 0 127.0.0.1:6379> hdel names:003 sex ( Integer) 1 127.0.0.1:6379> hkeys names:003 1) "name" 2) "Age" 127.0.0.1:6379> hvals names:003 1 "Niushao" 2) "127.0.0.1:6379> hgetall names:003 1)" Name "2"
Niushao "3" Age "4" "127.0.0.1:6379>"
3,list //linked lists, frequently used words stacks and queues
How to operate
lpush //from the head into an element
lrange //lrange list 0-1 starts from the head to view a list
Rpush //From the tail
press into an element Linsert //Pressing an element from a specific place Linsert list before list-value list-value
Lset //Replace an existing element by subscript
Lrem ////Lrem list key List-value-del
LTrim//Remove value from the key value after the following equal values ltrim list key1
key2 lpop< c18/>//removes a value from the beginning, returns a
rpop //trailing value, and returns the
Rpoplpush //Operations list, removes the first final value, and presses into the first value
Llen //Returns the number of list elements
Example: [Root@vm_173_6_centos bin]#./REDIS-CLI 127.0.0.1:6379> lpush list1 World (integer) 1 127.0.0.1:6379> lpush list1 Hello (integer) 2 127.0.0.1:6379> lrange list1 0-1 1) "Hel Lo "2" "World" 127.0.0.1:6379> lrange list1-1 0 (empty list or set) 127.0.0.1:6379>
Lpush list1 Niushaogang (integer) 3 127.0.0.1:6379> lrange list1 0-1 1) "Niushaogang" 2 "Hello" 3) "World" 127.0.0.1:6379> rpush list1 Niushao (integer) 4 127.0.0.1:6379&G T
Lrange list1-1 0 (empty list or set) 127.0.0.1:6379> Lrange list1 0-1 1) "Niushaogang"
2 "Hello" 3) "World" 4 "Niushao" 127.0.0.1:6379> rpush list Gang (integer) 1 127.0.0.1:6379> rpush list1 Gang (integer) 5 127.0.0.1:6379> lrange list1 0-1 1) "Niushao Gang "2)" HelLo "3" "World" 4) "Niushao" 5) "gang" 127.0.0.1:6379> Linset List1 before Niushao
Hao (error) ERR unknown command ' linset ' 127.0.0.1:6379> linsert list1 before Niushao
(integer) 6 127.0.0.1:6379> lrange list 0-1 1) "Gang" 127.0.0.1:6379> Lrange list1 0-1
1 "Niushaogang" 2) "Hello" 3 "World" 4 "" Gangshao "5)" Niushao "6)" gang "
127.0.0.1:6379> Lset list1 0 gangge OK 127.0.0.1:6379> lrange list1 0-1 1) "Gangge" 2 "Hello" 3 "World" 4) "Gangshao" 5) "Niushao" 6) "gang" 127.0.0.1:6379& Gt
Lrem list1 1 World (integer) 1 127.0.0.1:6379> lrange list1 0-1 1) "Gangge" 2) "Hello"
3) "Gangshao" 4) "Niushao" 5) "gang" 127.0.0.1:6379> LTrim list1 2 4 OK 127.0.0.1:6379> Lrange list1 0-1 1) "Gangshao" 2) "Niushao" 3) "gang" 127.0.0.1:6379> Lpop list1 "Gangshao" 127.0.0.1:6379> rpop list1 ' gang ' 127.0.0.1:6379> Rpopllpush list1 ganggeding
Tianlidi (Error) ERR unknown command ' rpopllpush ' 127.0.0.1:6379> rpoplpush list1 Ganggenulichenggong "Niushao" 127.0.0.1:6379> lrange list1 0-1 Empty (127.0.0.1:6379> list or set) Lrang
E list1 0-1 (empty list or set) 127.0.0.1:6379> lrange list1 0-1 (empty list or set)
127.0.0.1:6379> lrange list1 0-1 (empty list or set) 127.0.0.1:6379> llen List (integer) 1
127.0.0.1:6379> llen list1 (integer) 0 127.0.0.1:6379> lpush list1 niu (integer) 1 127.0.0.1:6379> lpush list1 Gang (integer) 2 127.0.0.1:6379> lpush List1 Shao (intege R) 3 127.0.0.1:6379> lrange list1 0-1 1) "Shao" 2) "gang" 3) "NIU" 127.0.0.1:6379> lpoplpush list L
Ist1 (Error) ERR unknown command ' lpoplpush ' 127.0.0.1:6379> rpoplpush list list1 ' gang ' 127.0.0.1:6379> lrange list1 0-1 1) "Gang" 2) "Shao" 3) "gang" 4) "NIU" 127.
0.0.1:6379> llen list1 (integer) 4
4,set //unordered collection
How to:
sadd add element
smember view element
Srem Remove specified element
spop Random Delete element
Sdiff Fetch difference Set
Sdiffstore the difference set and save the
sinter intersection
Sinterstore take the intersection and save the
sunion fetch and set
Sunionstore Save the collection
smove Move elements to another collection
SCard return the number of elements in the collection
Sismember Check whether members of the collection
Srandmember randomly output an element
Operation Example: 127.0.0.1:6379> sadd sets1 niu (integer) 1 127.0.0.1:6379> asdd sets1 Shao (error) ERR Unknow n Command ' ASDD ' 127.0.0.1:6379> sadd sets1 Shao (integer) 1 127.0.0.1:6379> sadd sets1 Gang (intege R) 1 127.0.0.1:6379> sadd sets1 Shao (integer) 0 127.0.0.1:6379> smembers sets1 1) "Gang" 2) "sh AO "3" "Niu" 127.0.0.1:6379> Srem Gang (Error) ERR wrong number of arguments for ' srem ' command 127.0.0 .1:6379> Srem sets1 Gang (integer) 1 127.0.0.1:6379> spop sets1 "Shao" 127.0.0.1:6379> smembers Sets1 1) "Niu" 127.0.0.1:6379> sadd sets1 One (integer) 1 127.0.0.1:6379> sadd sets1 Two (intege R) 1 127.0.0.1:6379> sadd sets1 Three (integer) 1 127.0.0.1:6379> sadd sets1 Four (integer) 1 12 7.0.0.1:6379> sadd sets2 Four (integer) 1 127.0.0.1:6379> sadd sets2 Five (integer) 1 127.0.0.1:6379
> Sdiff sets1 sets2 1) "One" 2 "three" 3) "NIU" 4) "two" 127.0.0.1:6379> Sdiff sets2 sets1 1) "Five" 127.0.0.1: 6379> sdiffstore sets3 sets1 sets2 (integer) 4 127.0.0.1:6379> smember sets3 (Error) ERR unknown command ' Smember ' 127.0.0.1:6379> smembers sets3 1 "one" 2) "three" 3) "NIU" 4) "two" 127.0.0.1:6379& Gt Sinter sets1 sets2 1) "Four" 127.0.0.1:6379> sunion sets1 sets2 1) "two" 2) "NIU" 3) "four" 4) "One" 5) "five" 6) "three" 127.0.0.1:6379> smove sets1 sets2 niu (integer) 1 127.0.0.1:6379> SME Mbers sets2 1) "Four" 2) "five" 3) "Niu" 127.0.0.1:6379> scard sets2 (integer) 3 127.0.0.1:6379 > Sinmember sets2 Shao (error) ERR unknown command ' sinmember ' 127.0.0.1:6379> sismember sets2 Shao (int
Eger) 0 127.0.0.1:6379> sismember sets2 niu (integer) 1 127.0.0.1:6379> srandmember sets2 "Niu" 127.0.0.1:6379> sRandmember sets2 "Niu" 127.0.0.1:6379> srandmember set