redis持久化存储

【centos6.5】

redis/

关机重启后,之前redis已经搭建完成

[root@master ~]# setenforce 0

[root@master ~]# iptables -F

[root@master ~]# redis-server /usr/local/redis/conf/redis.conf &【重启主机后启动报错】

[1] 2270

[root@master ~]# [2270] 25 May 15:27:28.876 * Increased maximum number of open files to 10032 (it was originally set to 1024).

                _._                                                 

           _.-``__ ''-._                                            

      _.-``    `.  `_.  ''-._           Redis 2.8.9 (00000000/0) 64 bit

  .-`` .-```.  ```\/    _.,_ ''-._                                  

 (    '      ,       .-`  | `,    )     Running in stand alone mode

 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379

 |    `-._   `._    /     _.-'    |     PID: 2270

  `-._    `-._  `-./  _.-'    _.-'                                  

 |`-._`-._    `-.__.-'    _.-'_.-'|                                 

 |    `-._`-._        _.-'_.-'    |           http://redis.io       

  `-._    `-._`-.__.-'_.-'    _.-'                                  

 |`-._`-._    `-.__.-'    _.-'_.-'|                                 

 |    `-._`-._        _.-'_.-'    |                                 

  `-._    `-._`-.__.-'_.-'    _.-'                                   

      `-._    `-.__.-'    _.-'                                      

          `-._        _.-'                                          

              `-.__.-'                                              

 

[2270] 25 May 15:27:28.880 # Server started, Redis version 2.8.9

[2270] 25 May 15:27:28.881 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

[2270] 25 May 15:27:28.892 * DB loaded from disk: 0.011 seconds

[2270] 25 May 15:27:28.892 * The server is now ready to accept connections on port 6379

[root@master ~]# pkill redis【杀死进程redis

[2270 | signal handler] (1558769373) Received SIGTERM, scheduling shutdown...

[root@master ~]# [2270] 25 May 15:29:33.530 # User requested shutdown...

[2270] 25 May 15:29:33.530 * Saving the final RDB snapshot before exiting.

[2270] 25 May 15:29:33.544 * DB saved on disk

[2270] 25 May 15:29:33.544 # Redis is now ready to exit, bye bye...

 

[1]+  Done                    redis-server /usr/local/redis/conf/redis.conf

[root@master ~]# sysctl vm.overcommit_memory=1【值改为1

vm.overcommit_memory = 1

[root@master ~]# redis-server /usr/local/redis/conf/redis.conf &

[1] 2284

[root@master ~]# [2284] 25 May 15:30:40.210 * Increased maximum number of open files to 10032 (it was originally set to 1024).

                _._                                                 

           _.-``__ ''-._                                            

      _.-``    `.  `_.  ''-._           Redis 2.8.9 (00000000/0) 64 bit

  .-`` .-```.  ```\/    _.,_ ''-._                                  

 (    '      ,       .-`  | `,    )     Running in stand alone mode

 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379

 |    `-._   `._    /     _.-'    |     PID: 2284

  `-._    `-._  `-./  _.-'    _.-'                                  

 |`-._`-._    `-.__.-'    _.-'_.-'|                                 

 |    `-._`-._        _.-'_.-'    |           http://redis.io       

  `-._    `-._`-.__.-'_.-'    _.-'                                  

 |`-._`-._    `-.__.-'    _.-'_.-'|                                 

 |    `-._`-._        _.-'_.-'    |                                 

  `-._    `-._`-.__.-'_.-'    _.-'                                  

      `-._    `-.__.-'    _.-'                                      

          `-._        _.-'                                           

              `-.__.-'                                              

 

[2284] 25 May 15:30:40.232 # Server started, Redis version 2.8.9

[2284] 25 May 15:30:40.233 * DB loaded from disk: 0.000 seconds

[2284] 25 May 15:30:40.233 * The server is now ready to accept connections on port 6379

[root@master ~]# ps -ef|grep redis

root       2284   2208  0 15:30 pts/1    00:00:00 redis-server *:6379                         

root       2288   2208  0 15:31 pts/1    00:00:00 grep redis

[root@master ~]# redis-cli

127.0.0.1:6379> auth yunjisuan

OK

127.0.0.1:6379> set user:01:passwd 99999

OK

127.0.0.1:6379> get user:01:passwd

"99999"

 

1、数据类型

1.1  string 字符串类型

[root@master ~]# redis-cli -a yunjisuan set work ">9000"

OK

[root@master ~]# redis-cli -a yunjisuan get work

">9000"

 

1.1.1 常规的string字符串类型

[root@master ~]# redis-cli -a yunjisuan set work ">9000"

OK

[root@master ~]# redis-cli -a yunjisuan get work

">9000"

 

1.1.2  string类型也可以用来存储数字,并支持数字的加减操作

[root@master ~]# redis-cli -a yunjisuan set counter 1

OK

[root@master ~]# redis-cli -a yunjisuan incr counter【自增1

(integer) 2

[root@master ~]# redis-cli -a yunjisuan incr counter

(integer) 3

[root@master ~]# redis-cli -a yunjisuan get counter

"3"

[root@master ~]# redis-cli -a yunjisuan incrby counter 2【自增指定数值】

(integer) 5

[root@master ~]# redis-cli -a yunjisuan incrby counter 2

(integer) 7

[root@master ~]# redis-cli -a yunjisuan get counter

"7"

[root@master ~]# redis-cli -a yunjisuan decr counter【】自减1

(integer) 6

[root@master ~]# redis-cli -a yunjisuan decr counter

(integer) 5

[root@master ~]# redis-cli -a yunjisuan get counter

"5"

[root@master ~]# redis-cli -a yunjisuan decrby counter 2【自减指定数值】

(integer) 3

[root@master ~]# [2284] 25 May 15:42:17.254 * 10 changes in 300 seconds. Saving...

[2284] 25 May 15:42:17.257 * Background saving started by pid 2339

[2339] 25 May 15:42:17.298 * DB saved on disk

[2339] 25 May 15:42:17.299 * RDB: 6 MB of memory used by copy-on-write

[2284] 25 May 15:42:17.363 * Background saving terminated with success

[root@master ~]# redis-cli -a yunjisuan decrby counter 2

(integer) 1

[root@master ~]# redis-cli -a yunjisuan get counter

"1"

 

1.1.3  key设置新值并且返回原值

[root@master ~]# redis-cli -a yunjisuan

127.0.0.1:6379> set user01 zhangshan【设置新key-value

OK

127.0.0.1:6379> get user01

"zhangshan"

127.0.0.1:6379> getset user01 wangwu【设置新数据并返回旧数据】

"zhangshan"

127.0.0.1:6379> getset user01 liliu【设置新数据并返回旧数据】

"wangwu"

127.0.0.1:6379> getset user01 gongli【设置新数据并返回旧数据】

"liliu"

127.0.0.1:6379> get user01

"gongli"

 

1.1.4   string类型还支持批量读写操作

127.0.0.1:6379> mset name zhangsan age 44

OK

127.0.0.1:6379> mget name age

1) "zhangsan"

2) "44"

 

1.1.5  string类型还支持其部分的修改和获取操作

127.0.0.1:6379> set images flower

OK

127.0.0.1:6379> get images

"flower"

127.0.0.1:6379> append images .jpg【追加字符串】

(integer) 10

127.0.0.1:6379> get images

"flower.jpg"

127.0.0.1:6379> strlen images

(integer) 10

127.0.0.1:6379> substr images 0 6

"flower."

127.0.0.1:6379> substr images 0 5

"flower"

 

命令使用帮助

查看单个命令help + 命令名

127.0.0.1:6379> help set

  SET key value [EX seconds] [PX milliseconds] [NX|XX]

  summary: Set the string value of a key

  since: 1.0.0

  group: string

 

127.0.0.1:6379> help mset

  MSET key value [key value ...]

  summary: Set multiple keys to multiple values

  since: 1.0.1

  group: string

 

 

2 list类型

redis能够将数据存储成一个列表,并能对这个列表进行丰富的操作

127.0.0.1:6379> lpush students "zhangsan"【将元素zhangsan放在students列表最左边】

(integer) 1

127.0.0.1:6379> lpush students "wangwu"【将元素wangwu插入列表的最左边】

(integer) 2

127.0.0.1:6379> lpush students "liliu"【将元素liliu插入列表的最左边】

(integer) 3

127.0.0.1:6379> lrange students 0 2【查看序列是02的元素】

1) "liliu"

2) "wangwu"

3) "zhangsan"

127.0.0.1:6379> rpush students "wangyue"【将元素wnagyue插入列表扥最右边】

(integer) 4

127.0.0.1:6379> lrange students 0 3【查看序列是03的元素】

1) "liliu"

2) "wangwu"

3) "zhangsan"

4) "wangyue"

127.0.0.1:6379> llen students【查看列表元素的个数】

(integer) 4

127.0.0.1:6379> lpop students【移除最左边的元素】

"liliu"

127.0.0.1:6379> rpop students【移除右边的元素】

"wangyue"

127.0.0.1:6379> lrange students 0 3【列表里只剩下两个元素】

1) "wangwu"

2) "zhangsan"

127.0.0.1:6379> rpush students zhagnsan

(integer) 3

127.0.0.1:6379> rpush students zhagnsan

(integer) 4

127.0.0.1:6379> rpush students zhagnsan

(integer) 4

127.0.0.1:6379> lrange students 0 3

1) "wangwu"

2) "zhangsan"

3) "zhagnsan"

4) "zhagnsan"

127.0.0.1:6379> lrem students 2 "zhangsan"【删除列表里是zhangsan的元素,删除两次(从左向右删)】

(integer) 1

127.0.0.1:6379> lrange students 0 3

1) "wangwu"

2) "zhagnsan"

3) "zhagnsan"

127.0.0.1:6379> rpush students zhangsan

(integer) 4

127.0.0.1:6379> rpush students zhagnsan

(integer) 5

127.0.0.1:6379> lrem students 1 "zhagnsan"【删除列表里的元素zhagnsan一次】

(integer) 1

127.0.0.1:6379> lrange students 0 3

1) "wangwu"

2) "zhagnsan"

3) "zhagnsan"

127.0.0.1:6379> lrem students 0 "zhagnsan"【清空列表所有的zhagnsan

(integer) 2

127.0.0.1:6379> lrange students 0 3

1) "wangwu"

 

redis也支持很多修改操作

linsert 在列表里的某个值的前后插入元素

127.0.0.1:6379> lrange students 0 5

1) "wangwu"

127.0.0.1:6379> lpush students a b c d 【左插入元素abcd

(integer) 5

127.0.0.1:6379> lrange students 0 5

1) "d"

2) "c"

3) "b"

4) "a"

5) "wangwu"

127.0.0.1:6379> linsert students before b xxx【在元素b的前边插入元素xxx

(integer) 6

127.0.0.1:6379> lrange students 0 9

1) "d"

2) "c"

3) "xxx"

4) "b"

5) "a"

6) "wangwu"

127.0.0.1:6379> linsert students after b xxx【在元素b的后边插入元素xxx

(integer) 7

127.0.0.1:6379> lrange students 0 9

1) "d"

2) "c"

3) "xxx"

4) "b"

5) "xxx"

6) "a"

7) "wangwu"

 

帮助 help @list

127.0.0.1:6379> help @list

 

  BLPOP key [key ...] timeout

  summary: Remove and get the first element in a list, or block until one is available

  since: 2.0.0

 

  BRPOP key [key ...] timeout

  summary: Remove and get the last element in a list, or block until one is available

  since: 2.0.0

 

  BRPOPLPUSH source destination timeout

  summary: Pop a value from a list, push it to another list and return it; or block until one is available

  since: 2.2.0

 

  LINDEX key index

  summary: Get an element from a list by its index

  since: 1.0.0

 

  LINSERT key BEFORE|AFTER pivot value

  summary: Insert an element before or after another element in a list

  since: 2.2.0

 

  LLEN key

  summary: Get the length of a list

  since: 1.0.0

 

  LPOP key

  summary: Remove and get the first element in a list

  since: 1.0.0

 

  LPUSH key value [value ...]

  summary: Prepend one or multiple values to a list

  since: 1.0.0

 

  LPUSHX key value

  summary: Prepend a value to a list, only if the list exists

  since: 2.2.0

 

  LRANGE key start stop

  summary: Get a range of elements from a list

  since: 1.0.0

 

  LREM key count value

  summary: Remove elements from a list

  since: 1.0.0

 

  LSET key index value

  summary: Set the value of an element in a list by its index

  since: 1.0.0

 

  LTRIM key start stop

  summary: Trim a list to the specified range

  since: 1.0.0

 

  RPOP key

  summary: Remove and get the last element in a list

  since: 1.0.0

 

  RPOPLPUSH source destination

  summary: Remove the last element in a list, append it to another list and return it

  since: 1.2.0

 

  RPUSH key value [value ...]

  summary: Append one or multiple values to a list

  since: 1.0.0

 

  RPUSHX key value

  summary: Append a value to a list, only if the list exists

  since: 2.2.0

 

3、集合(sets)类型

127.0.0.1:6379> sadd users laoda【项集合users里添加一个元素laoda

(integer) 1

127.0.0.1:6379> sadd users laoer laosan【向结合users里天界俩个元素laoerlaoda

(integer) 2

127.0.0.1:6379> smembers users【查看集合里的所有元素】

1) "laoer"【可以看到集合里的元素是无序的】

2) "laosan"

3) "laoda"

向集合中添加三个元素,并让redis返回所有元素。

127.0.0.1:6379> sismember users laoda【查看laoda是否存在与集合users

(integer) 1【存在】

127.0.0.1:6379> sismember users laoer【查看元素laoer是否存在于集合users

(integer) 1【存在】

127.0.0.1:6379> sismember users laosan【查看元素laosan是否存在于集合users

(integer) 1【存在】

127.0.0.1:6379> sismember users laosi【查看元素laosi是否存在于集合users

(integer) 0【不存在】

在新闻ID1000被加了几个标签tag1、2、5、77,就可以设置下面俩集合

[root@master ~]# redis-cli -a yunjisuan sadd news:1000:tags 1

(integer) 1

[root@master ~]# redis-cli -a yunjisuan sadd news:1000:tags 2

(integer) 1

[root@master ~]# redis-cli -a yunjisuan sadd news:1000:tags 5

(integer) 1

[root@master ~]# redis-cli -a yunjisuan sadd news:1000:tags 77

(integer) 1

[root@master ~]# redis-cli -a yunjisuan sadd tag:1:objects 1000

(integer) 1

[root@master ~]# redis-cli -a yunjisuan sadd tag:2:objects 1000

(integer) 1

[root@master ~]# redis-cli -a yunjisuan sadd tag:5:objects 1000

(integer) 1

[root@master ~]# redis-cli -a yunjisuan sadd tag:77:objects 1000

(integer) 1

 

[root@master ~]# redis-cli -a yunjisuan smembers news:1000:tags

1) "1"【获取集合为news:1000:tags的所有元素

2) "2"

3) "5"

4) "77"

[root@master ~]# redis-cli -a yunjisuan smembers tag:5:objects

1) "1000"【获取集合为tag:5:objects的所有元素

 

[root@master ~]# redis-cli -a yunjisuan sadd tag:1:objects 500

(integer) 1【向集合tag:1:objects里添加元素锕500

[root@master ~]# redis-cli -a yunjisuan smembers tag:1:objects

1) "500"【查看集合tag:1:objects里的所有元素

2) "1000"

[root@master ~]# redis-cli -a yunjisuan smembers tag:2:objects

1) "1000"【查看集合tag:2:objects里的所有元素

[root@master ~]# redis-cli -a yunjisuan  sinter tag:1:objects tag:2:objects tag:5:objects tag:77:objects

1) "1000"【求集合tag:1:objects tag:2:objects tag:5:objects tag:77:objects里所有元素的交集

 

 

4、有序集合(sorted sets)类型

向一个有序集合里添加元素

[root@master ~]# redis-cli -a yunjisuandays是有序集合名,0是序号,mon是值】

127.0.0.1:6379> ZADD days 0 mon

(integer) 1

127.0.0.1:6379> ZADD days 0 tue

(integer) 1

127.0.0.1:6379> ZADD days 2 web

(integer) 1

127.0.0.1:6379> ZADD days 3 thu

(integer) 1

127.0.0.1:6379> ZADD days 4 fri

(integer) 1

127.0.0.1:6379> ZADD days 5 sat

(integer) 1

127.0.0.1:6379> ZADD days 6 sun

(integer) 1

127.0.0.1:6379> zrange days 0 6【查看集合索引06的元素】

1) "mon"

2) "tue"

3) "web"

4) "thu"

5) "fri"

6) "sat"

7) "sun"

从上面我们可以看出,ZADD创建的集合是有序集合

 

查看有序集合days的具体值的排序

127.0.0.1:6379> zscore days mon

"1"

127.0.0.1:6379> zscore days web

"2"

127.0.0.1:6379> zscore days fri

"4"

127.0.0.1:6379> zcount days 3 6

(integer) 4

127.0.0.1:6379> ZRANGEBYSCORE days 3 6

1) "thu"

2) "fri"

3) "sat"

4) "sun"

有序集合是通过一个dual-ported数据结构实现的

127.0.0.1:6379> zadd hackers 1940 "1940-Alan Kay"

(integer) 1

127.0.0.1:6379> zadd hackers 1953 "1953-Richard Stallman"

(integer) 1

127.0.0.1:6379> zadd hackers 1965 "1965-Yukihiro Matsumoto"

(integer) 1

127.0.0.1:6379> zadd hackers 1916 "1916-Claude Shannon"

(integer) 1

127.0.0.1:6379> zadd hackers 1969 "1969-Linux Torvalds"

(integer) 1

127.0.0.1:6379> zadd hackers 1912 "1912-ALAN TURING"

(integer) 1 

 

127.0.0.1:6379> zrange hackers 0 6【利用zrange进行排序查询】

1) "1912-ALAN TURING"

2) "1916-Claude Shannon"

3) "1940-Alan Kay"

4) "1953-Richard Stallman"

5) "1965-Yukihiro Matsumoto"

6) "1969-Linux Torvalds"

127.0.0.1:6379> zrevrange hackers 0 -1【利用zrevrange进行方向查询】

1) "1969-Linux Torvalds"

2) "1965-Yukihiro Matsumoto"

3) "1953-Richard Stallman"

4) "1940-Alan Kay"

5) "1916-Claude Shannon"

6) "1912-ALAN TURING"

5hash 类型

redis 能够存储key对多个属性的数据(比如user1uname user1.passwd

存储一个hash类型test,他的属性是name ,属性数据是yunjisuan

127.0.0.1:6379> hset test name yunjisuan

(integer) 1

存储一个hash类型test,它的属性是age,属性数据是35

127.0.0.1:6379> hset test age 35

(integer) 1

存储一个hash类型test。它的属性是age,属性数据是nan

127.0.0.1:6379> hset test sex nan

(integer) 1

查看hash类型test的所有属性值

127.0.0.1:6379> hvals test

1) "yunjisuan"

2) "35"

3) "nan"

查看hash类型test的所有属性及属性所有对应的值

127.0.0.1:6379> hgetall test

1) "name"

2) "yunjisuan"

3) "age"

4) "35"

5) "sex"

6) "nan"

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值