redis :RDB、AOF 及redis数据类型

RDB介绍:

全称 Redis DataBase

数据持久化方式之一

Ls /var/lib/redis/6379/

Dump.rdb

[root@host58 ~]# ls /var/lib/redis/6379/

Dump.rdb  //硬盘数据

[root@host58 ~]# ss -utnlp | grep redis

[root@host58 ~]# /etc/init.d/redis_6379  start

Starting Redis server...

[root@host58 ~]# ss -utnlp | grep redis

tcp    LISTEN     0      128    192.168.4.58:6358                  *:*                   users:(("redis-server",pid=4482,fd=6))

[root@host58 ~]# redis-cli -h 192.168.4.58 -p 6358

192.168.4.58:6358> ping

PONG

192.168.4.58:6358> keys *

1) "name"

2) "shcool"  //内存数据

3) "pay"

192.168.4.58:6358> 

vi /etc/redis/6379.conf 

254 dbfilename "dump.rdb"

    217 #   save ""   //禁用

    218 

    219 save 900 1

    220 save 300 10

221 save 60 10000

[root@host58 ~]# redis-cli -h 192.168.4.58 -p 6358

192.168.4.58:6358> ping

PONG

192.168.4.58:6358> save   //阻写存    bgsave不阻写存

OK

192.168.4.58:6358> flushall    //注意:清除内存数据

OK

192.168.4.58:6358> shutdown   //退出时会往硬盘dump.rdb存,没有数据

not connected> exit

[root@host58 6379]# ls ./dump.rdb -l

-rw-r--r--. 1 root root 253 7月   6 15:08 ./dump.rdb

[root@host58 6379]# 

[root@host58 ~]# redis-cli -h 192.168.4.58 -p 6358

192.168.4.58:6358> keys *

(empty list or set)

192.168.4.58:6358> 

192.168.4.58:6358> set k1 v1

OK

192.168.4.58:6358> set k2 v2

OK

192.168.4.58:6358> set k3 v3

OK

192.168.4.58:6358> set k4 v4

OK

192.168.4.58:6358> set k5 v5

OK

192.168.4.58:6358> set k6 v6 

OK

192.168.4.58:6358> set k7 v7 

OK

192.168.4.58:6358> set k8 v8

OK

192.168.4.58:6358> set k9 v9

OK

192.168.4.58:6358> set k10 v10

OK

192.168.4.58:6358> set k11 v11

OK

192.168.4.58:6358> save 

[root@host58 6379]# cp dump.rdb dump.rdb.bak

[root@host58 6379]# ls

dump.rdb  dump.rdb.bak

192.168.4.58:6358> keys *

 1) "k2"

 2) "k8"

 3) "k5"

 4) "k3"

 5) "k10"

 6) "k4"

 7) "k9"

 8) "k6"

 9) "k1"

10) "k11"

11) "k7"

//192.168.4.58:6358> FLUSHALL

//OK

192.168.4.58:6358> shutdown    //清空数据

//恢复数据

[root@host58 6379]# ls

dump.rdb  dump.rdb.bak

[root@host58 6379]# rm -rf dump.rdb

[root@host58 6379]# cp dump.rdb.bak dump.rdb

[root@host58 6379]# ls

dump.rdb  dump.rdb.bak

[root@host58 6379]# 

[root@host58 ~]# redis-cli -h 192.168.4.58 -p 6358

192.168.4.58:6358> keys *

(empty list or set)

192.168.4.58:6358> 

192.168.4.58:6358> shutdown

192.168.4.58:6358> exit

[root@host58 ~]# redis-cli -h 192.168.4.58 -p 6358

192.168.4.58:6358> keys *

AOF操作:记录所有除查之外的操作:

vim /etc/redis/6379.conf

appendonly yes

# The name of the append only file (default: "appendonly.aof")

appendfilename "appendonly.aof"

 /etc/init.d/redis_6379  stop

Stopping ...

Redis stopped

/etc/init.d/redis_6379  start

Starting Redis server...

ls

appendonly.aof  dump.rdb  dump.rdb.bak

[root@host58 ~]# redis-cli -h 192.168.4.58 -p 6358

192.168.4.58:6358> ping

PONG

192.168.4.58:6358> set a 1

OK

192.168.4.58:6358> set a2

(error) ERR wrong number of arguments for 'set' command

192.168.4.58:6358> set a2 a

OK

192.168.4.58:6358> save

OK

192.168.4.58:6358> 

[root@host58 6379]# cat appendonly.aof 

*2

$6

SELECT

$1

0

*3

$3

set

$1

a

$1

1

*3

$3

set

$2

a2

$1

a

 vim /etc/redis/6379.conf 

# appendfsync always  //有新记录就立即记录

appendfsync everysec  //每秒记录一次

# appendfsync no  //不执行

cp appendonly.aof appendonly.aof.bak

 [root@host58 ~]# redis-cli -h 192.168.4.58 -p 6358

192.168.4.58:6358> FLUSHALL

OK

192.168.4.58:6358> SHUTDOWN

not connected> exit

[root@host58 ~]# /etc/init.d/redis_6379  status

cat: /var/run/redis_6379.pid: 没有那个文件或目录

Redis is running ()

[root@host58 ~]# /etc/init.d/redis_6379  start

Starting Redis server...

[root@host58 ~]# redis-cli -h 192.168.4.58 -p 6358

192.168.4.58:6358> keys *

(empty list or set)

192.168.4.58:6358> 

会先读取appendonly.aof

[root@host58 6379]# vim appendonly.aof

[root@host58 ~]# /etc/init.d/redis_6379  start

Starting Redis server...

[root@host58 ~]# /etc/init.d/redis_6379  status

Redis is not running

[root@host58 ~]# 

[root@host58 6379]# redis-check-aof --fix appendonly.aof   //把文件恢复到最后一次正确操作

0x              56: Expected \r\n, got: 5553

AOF analyzed: size=102, ok_up_to=78, diff=24

This will shrink the AOF from 102 bytes, with 24 bytes, to 78 bytes

Continue? [y/N]: y

Successfully truncated AOF

[root@host58 6379]# cat appendonly.aof

*2

$6

SELECT

$1

0

*3

$3

set

$1

a

$1

1

*3

$3

set

$2

a2

$1

a

Killall -9 redis-server

[root@host58 ~]# /etc/init.d/redis_6379  status

Redis is not running

[root@host58 ~]# /etc/init.d/redis_6379  start

/var/run/redis_6379.pid exists, process is already running or crashed

[root@host58 ~]# 

[root@host58 ~]# cat /var/run/redis_6379.pid 

5712

[root@host58 ~]# rm -rf /var/run/redis_6379.pid  //删除加载脚本

[root@host58 ~]# /etc/init.d/redis_6379  start

Starting Redis server...

[root@host58 ~]# /etc/init.d/redis_6379  status

Redis is running (5808)

[root@host58 ~]# 

Redis数据类型:

Redis(内存、硬盘) 和 memcached(内存) 的区别

  Redis:  string 

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

              Set name test ex  90  

              Set name1 test1 px 90000

              Set name2 test2 nx 不存在时改变

              Set name3 test3 xx 存在时改变

192.168.4.58:6358> set tel 187010188889 xx

OK

192.168.4.58:6358> set tel 187010188888 nx

(nil)

192.168.4.58:6358> keys *

1) "tel"

2) "a2"

3) "a"

192.168.4.58:6358> APPEND tel yaya

(integer) 16

192.168.4.58:6358> get tel

"187010188889yaya"

192.168.4.58:6358> APPEND tel yaya

(integer) 20

192.168.4.58:6358> APPEND  tes d

(integer) 1

192.168.4.58:6358> SETRANGE tel 5 *****

(integer) 20

192.168.4.58:6358> get tel

"18701*****89yayayaya"

192.168.4.58:6358>

猜你喜欢

转载自blog.csdn.net/weixin_40018205/article/details/80943783
今日推荐