4. redis persistence
Redis
It is a memory database, once the server process exits, the data in the database will be lost, in order to solve this problem, Redis
provides two of a lasting solution to save the data in memory to disk, to avoid loss of data.
4.1 RDB persistence
redis
Provides RDB持久化
functionality, this feature can redis
save the state in memory to the hard disk, it can perform a manual.
Can also be re- redis.conf
configured, regularly perform .
RDB RDB persistence produced a file is compressed in a binary file , the file is saved on the hard disk, redis can restore the state of the database at the time of the adoption of the document.
Advantages: speed, suitable for backup, Master-slave replication is based on the RDB persistence function
配置参数:
touch redis-rdb.conf
加入如下的内容
daemonize yes #后台运行redis
port 6379 #指定redis的端口
logfile /data/6379/redis.log #redis日志文件
dir /data/6379/ #定义持久化文件存储位置,需要手动创建文件夹
dbfilename redis-rdb.rdb #rdb持久化文件
bind 0.0.0.0 #redis启动地址
save 10 5 #10秒内,超过5个修改类的操作,就触发持久化
Manual trigger
127.0.0.1:6379> save
OK
4.2 AOF persistence
AOF (append-only log file)
record of all operation commands executed by the server (e.g., set del, etc.), and when the server starts, be reduced by re-executing the command data sets
AOF commands in the file save all format redis protocol the new command is appended to the end of the file.
Advantages: maximum program to ensure that data is not lost
drawback: very large logging
redis-client 写入数据 > redis-server 同步命令 > AOF文件
Configuration parameters
AOF持久化配置,两条参数
appendonly yes
appendfsync always 总是修改类的操作
everysec 每秒做一次持久化
no 依赖于系统自带的缓存大小机制
touch redis-aof.conf
daemonize yes
port 6379
logfile /data/6379/redis.log
dir /data/6379
appendonly yes
appendfsync everysec
Start redis service
redis-server /etc/redis.conf
Check the redis data directory / data / 6379 / aof whether or not a file
[root@linux 6379]# ls
appendonly.aof dbmp.rdb redis.log
Login redis-cli, write data, real-time check aof file information
[root@linux 6379]# tail -f appendonly.aof
Set up a new key, check aof information, and then close redis, check whether the data persistence
pkill -9 redis
redis-server /etc/redis.conf
redis-cli
4.3 does not restart to switch data aof rdb
Step 1: Prepare a rdb database of redis
Step Two: In redis database, aof activation mode, off mode rdb, the following command
127.0.0.1:6379> CONFIG set appendonly yes
OK
127.0.0.1:6379> CONFIG SET save ""
OK
Modify the command rdb and aof the persistence mechanism, to take effect is only temporary, permanent need to modify the configuration file
5. redis master copy from
principle:
- SYNC command sent from the server to the master server.
- Received the SYNC command master server calls BGSAVE command to create a RDB file, and use all the write command buffer recording executed next.
- When the primary server BGSAVE been executed command, it sends to the server RDB
- And will receive and load the file from the server.
- All the write command buffer primary server will store sent from the server to execute.
1, when the opening from the master copy, using the RDB data synchronization from the master
2 after the synchronization start propagate through the master database command, copy active manner,
mechanisms to achieve PSYNC after 3,2.8 achieve break Reconnection
5.1 Environment Preparation
Preparation 3 redis profile
6380.conf
mkdir /data/638{0..2} # 创建6380 6381 6382文件夹
6380.conf
vim /data/6380/redis.conf
port 6380
daemonize yes
pidfile /data/6380/redis.pid
loglevel notice
logfile "/data/6380/redis.log"
dbfilename dump.rdb
dir /data/6380
protected-mode no
6381.conf
vim /data/6381/redis.conf
port 6381
daemonize yes
pidfile /data/6381/redis.pid
loglevel notice
logfile "/data/6381/redis.log"
dbfilename dump.rdb
dir /data/6381
protected-mode no
6382.conf
port 6382
daemonize yes
pidfile /data/6382/redis.pid
loglevel notice
logfile "/data/6382/redis.log"
dbfilename dump.rdb
dir /data/6382
protected-mode no
Start three instances redis
redis-server /data/6380/redis.conf
redis-server /data/6381/redis.conf
redis-server /data/6382/redis.conf
From the main planning
主节点:6380
从节点:6381、6382
5.2 Configuration master-slave synchronization
6381/6382 Command Line
redis-cli -p 6381
SLAVEOF 127.0.0.1 6380 #指明主库的地址
redis-cli -p 6382
SLAVEOF 127.0.0.1 6380 #指明主库的地址
Check the status of master
From the library:
127.0.0.1:6382> info replication
127.0.0.1:6381> info replication
Main Library:
127.0.0.1:6380> info replication
5.3 switching from manual master copy failure
# 关闭主库6380redis-cli -p 6380
shutdown
Check the information from the master database, this time master_link_status: down
redis-cli -p 6381
info replication
redis-cli -p 6382
info replication
1. Turn off the 6381 identity from the library
redis-cli -p 6381
info replication
slaveof no one
2. The library from 6382 to 6381
6382连接到6381:
[root@linux ~]# redis-cli -p 6382
127.0.0.1:6382> SLAVEOF no one
127.0.0.1:6382> SLAVEOF 127.0.0.1 6381
3. Check the main information from 6382,6381