NoSQL database case combat--Redis data persistence

Preface

This environment is based on the Centos 7.8 system to build a Redis learning environment. For
specific construction, please refer to Redis-5.0.9 Environment Deployment

For the integrity and security of its own data, although the redis database is based on memory storage. But it still supports data persistence, redis supports two ways of persistence. Next I will believe the introduction


Persistence method:

  • a. Snapshot-based approach: redis is installed for a certain period to synchronize the data in the memory to the disk file.
  • b. File-based addition: redis will record the commands that change the redis data into the log file, and then restart again to execute the operation written to redis in the log file to achieve data restoration.

1. Snapshot-based persistence

Modify the configuration file to enable snapshot-based options

  • save 900 1 #If more than 1 key is modified within 900 seconds, initiate a snapshot save
  • save 300 10 #If more than 10 keys are modified in 300 seconds, a snapshot save will be initiated
  • save 60 10000 #60 seconds if more than 10000 keys are modified, a snapshot save will be initiated. The
    above is the default configuration of the system

Persistent directory

[root@reids_source_code redis]# ll /var/lib/redis/
total 4
-rw-r--r--. 1 redis redis 92 Feb 13 20:00 dump.rdb

Simulate the persistence of dump.rdb files

# 删除dump.rdb文件
[root@reids_source_code redis]# systemctl stop redis
[root@reids_source_code redis]# rm -f /var/lib/redis/dump.rdb 
[root@reids_source_code redis]# ll /var/lib/redis/dump.rdb
# 数据库中写入数据
[root@reids_source_code redis]# redis-cli -h 192.168.5.12 -a 123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.5.12:6379> set name wan
OK
192.168.5.12:6379> set age 18
OK
192.168.5.12:6379> bgsave
Background saving started
192.168.5.12:6379> exit
[root@reids_source_code redis]# ll /var/lib/redis/dump.rdb
-rw-r--r--. 1 redis redis 114 Feb 13 20:27 /var/lib/redis/dump.rdb
# 重启服务,登录数据库,查看数据
[root@reids_source_code redis]# redis-cli -h 192.168.5.12 -a 123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.5.12:6379> get name
"wan"
192.168.5.12:6379> get age
"18"

2. Persistence based on file append

Configuration content

Enable persistence based on the log file append method
appendonly yes
log file
appendfilename “appendonly.aof”
backup file cycle
appendfsync always // force write to disk every time a write command is received, the slowest, but guarantee complete persistence, no It is recommended to use
appendfsync everysec //Forced to write to the disk once every second, it has made a good compromise between performance and persistence, and
appendfsync no is recommended //It is completely dependent on os, the performance is the best, and the persistence is not guaranteed.

Implementation steps

[root@reids_source_code redis]# vim /etc/redis/redis.conf 
appendonly yes
appendfsync everysec
[root@reids_source_code redis]# systemctl restart redis

View additional files

[root@reids_source_code ~]# ll /var/lib/redis/appendonly.aof 
-rw-r--r--. 1 redis redis 0 Feb 13 20:31 /var/lib/redis/appendonly.aof

to sum up

Redis supports the persistence function, which can support persistence through the above two methods.
Persistent saved files (that is, under the redis installation directory)

[root@reids_source_code ~]# ll /var/lib/redis/
total 4
-rw-r--r--. 1 redis redis   0 Feb 13 20:31 appendonly.aof
-rw-r--r--. 1 redis redis 114 Feb 13 20:31 dump.rdb

Guess you like

Origin blog.csdn.net/XY0918ZWQ/article/details/113803200