Linux version redis installation and cluster configuration

1. Install Linux version of redis

1. Installation and testing

1: Install wget environment

yum -y install wget

2: Upload the redis compressed package

3: Execute the command (install the c language environment)

yum install gcc

4: After decompression, enter the redis root directory and execute make compilation

make

4: After successful compilation, enter: make install

make install

5: Start redis (enter src in the redis directory and execute the following command) Use the redis.conf file to start

./redis-server ../redis.conf

2. Redis persistence

Redis is an in-memory database. When the redis server is restarted or the computer is restarted, the data will be lost. We can persist the data in the redis memory to a file on the hard disk.

Redis provides two persistence methods:

RDB: snapshot, saved and persisted from the server

​ AOF: Log, the operation generates relevant logs, and recovers data through the logs. couchDB does not modify the data content, but only appends it, so the file itself is a log and no data will be lost.

1. The full name of RDB is redis database backup file (redis data backup file) persistence.

​ Write the snapshot of the data set in the memory to the disk within the specified time interval, which is the Snapshot snapshot in the jargon. When it is restored, the snapshot file is read directly into the memory. Redis will create (fork) a sub-process separately. For persistence, the data will first be written to a temporary file. After the persistence process is completed, this temporary file will be used to replace the last persisted file. During the entire process, the main process does not perform any IO operations, which ensures extremely high performance. If large-scale data recovery is required and the integrity of data recovery is not very sensitive, the RDB method is more efficient than the AOF method. of high efficiency. The disadvantage of RDB is that data after the last persistence may be lost.

Note: The function of fork is to copy a process that is the same as the current process. All the data (variables, environment variables, program counters, etc.) of the new process have the same values ​​as the original process, but it is a completely new process and serves as a child process of the original process. Every time the redis server starts, it will automatically dump All key-value pairs of the .rdb file are read into memory.

Insert image description here

Edit the redis.conf configuration file

RDB快照相关参数:
	save 900 1    #刷新快照到硬盘中,必须满足两者要求才会触发,即900秒之后至少1个关键字发生变化。
	save 300 10  #必须是300秒之后至少10个关键字发生变化。
	save 60 10000 #必须是60秒之后至少10000个关键字发生变化。
	上面三个参数屏闭后,rdb方式就关闭了
	
	stop-writes-on-bgsave-error yes    #后台存储错误停止写。
	rdbcompression yes    #使用LZF压缩rdb文件。
	rdbchecksum yes    #存储和加载rdb文件时校验。
	dbfilename dump.rdb    #设置rdb文件名。
	dir ./    #设置工作目录,rdb文件会写入该目录。

Insert image description here

2. AOF log persistence

config set appendonly yes
或者进入redis.conf 修改 appendonly = yes 默认是no

AOF log principle

Insert image description here

Idea: Every time an item is written to the memory, it is backed up. The time interval is 1 second. Disadvantages: The file is large and write operations are frequent.

  • Record each write operation in the form of a log, and record all write instructions executed by Redis (read operations are not recorded),

  • Only files can be appended but not rewritten. When redis starts, it will read the file (aof file) and reconstruct the data. In other words, when redis restarts, it will execute the write instructions from front to back according to the contents of the log file to complete the data. Return to work

  • aof saves the appendonly.aof file

AOF日志相关参数:
	appendonly no # 是否打开aof日志功能  no:不开启   yes:开启日志
	appendfsync always   # 每1个命令,都立即同步到aof. 安全,速度慢
				everysec # 折衷方案,每秒写1次
				no      # 写入工作交给操作系统,由操作系统判断缓冲区大小,统一写入到aof. 同步频率低,速度快
	no-appendfsync-on-rewrite  no # 正在导出rdb快照的过程中,要不要停止同步aof

Configure to enable AOF log

Insert image description here

Configure storage plan

Insert image description here

3. AOF rewriting

Thinking: If multiple operations are performed on the same key, how to display the operation records in the AOF log, one or n?

​Case : Create age and change the value five times

Insert image description here

Insert image description here

The log will record every step of the operation. If a key is operated multiple times, there will be only one performance in the data but n records in the log. How to find the correct value when data is lost and needs to be retrieved?

Aof rewriting is to reverse the key and value in the memory into redis commands and save them in the log, which is like a summary of the operations performed.

aof重写相关参数:
	no-appendfsync-on-rewrite  no # 正在导出rdb快照的过程中,要不要停止同步aof
	auto-aof-rewrite-percentage 100 #aof文件大小比起上次重写时的大小,增长率100%时,重写
	auto-aof-rewrite-min-size 64mb #aof文件,至少超过64M时,重写

Question: During the dump rdb process, if the aof stops synchronizing, will it be lost?

Answer: No, all operations are cached in the memory queue. After the dump is completed, the operations are unified.

Q: What does aof rewriting mean?

Answer: Aof rewriting refers to converting the data in the memory into commands and writing them into the aof log to solve the problem of the aof log being too large.

Question: If both the rdb file and the aof file exist, which one should be used first to recover the data?

Answer: aof

Question: Can the two types be used at the same time?

Answer: Yes, and it is recommended

Question: Which one is faster to restore, rdb or aof?

Answer: RDB is fast because it is a memory mapping of data and is loaded directly into the memory, while AOF is a command and needs to be executed one by one.

问题思考:在使用rdb做持久化时,我们关掉了redis服务,然后重新打开,保存的数据还在。但在做aof的时候我们将redis服务关闭后再打开数据就没有了。在上面不是配置过rdb持久化吗,为什么没起作用?
答:当rdb中有数据,并开启了AOF选项,重启redis服务后会产生一个空的aof文件,当rdb和aof文件都存在,会以aof文件来恢复数据。

3. Redis message publishing and subscription

Redis publish and subscribe (pub/sub) is a message communication model: the sender (pub) sends messages and the subscribers (sub) receive messages.

Redis clients can subscribe to any number of channels.

Insert image description here

Case: Create a message subscription terminal

./redis-cli --raw   --打开redis客户端
SUBSCRIBE woniu     --订阅蜗牛频道信息

Case: Create a message publisher

PUBLISH woniu '你好'  --发布 ”你好“ 信息

4. redis cluster configuration

1. redis master-slave replication (Master/Slave)

a. Cluster structure

Three nodes: one master node and two slave nodes

b. Prepare instances and configurations

Open three redis instances in the virtual machine to simulate the master-slave cluster mode. The information is as follows:

ip post Role
127.0.0.1 7001 master
127.0.0.1 7002 slave
127.0.0.1 7003 slave

1. Create three folders in the redis directory:

2. Copy redis.conf to three folders:

 cp /redis/redis-6.0.6/redis.conf /redis/7001
 cp /redis/redis-6.0.6/redis.conf /redis/7002
 cp /redis/redis-6.0.6/redis.conf /redis/7003

3. Modify the port in the file, the RDB file storage location, and the IP declared by the redis instance.

For example 7001 instance:

dir ../../7001
port 7001

4. Start three services and enter the src directory.

For example, start instance 7001:

./redis-server ../../7001/redis.conf

5. Connect to redis client

For example, connect to 7001 instance:

./redis-cli -p 7001

c. Open the master-slave relationship

Establish a master-slave relationship through commands:

For example, connect to the 7002 client and execute the command. 7002 becomes the slave node of 7001.

redis-cli -p 7002 quickly opens the client of a certain port

SLAVEOF 127.0.0.1 7001
d. Verify the master-slave relationship

Enter the client of 7001 and enter the command

info replication

Master-slave synchronization principle

Principle: The first time the slave node sends a request, it brings its own replid and offset. The master node judges whether it is consistent with its own replica and passes it to the slave node. The slave node saves the version information. The master node executes bgsave to generate the RDB file, and Synchronize to the slave node, and the slave node loads the RDB file to achieve data synchronization. At this time, if a modification operation occurs on the master node, a repl-baklog log will be generated.

2. Sentinel mode (sentinel)

What if the master node goes down?

Monitoring: sentinel continuously checks whether your master and slave are performing as expected

Automatic failure recovery: If the master fails, sentinel will promote a slave to the master. When the failed instance recovers, the new master will be the master.

Notification: Sentinel acts as a service discovery source for the redis client. When a failover occurs in the cluster, the latest information will be pushed to the redis client.

Sentinel service status detection, sending a ping command to each instance of the cluster every second

Subjective offline: If a sentinel node finds that an instance does not respond within the specified time, the instance is considered to be subjectively offline.

Objective offline: If all sentinels exceeding the number of executions (quornum) consider the instance offline, the instance will be objectively offline (the number of quornum is preferably more than half of the total number of instances)

ip post Role
127.0.0.1 27001 master
127.0.0.1 27002 slave
127.0.0.1 27003 slave

1. Create three folders in the redis directory:

Insert image description here

2. Create a new file sentinel.conf

Take 27001 as an example:

port 27001
sentinel monitor mymaster 127.0.0.1 7001 2
sentinel down-after-milliseconds mymaster 50000
sentinel failover-timeout mymaster 180000
dir "/ding/s1"

3. Start and enter the src directory

redis-sentinel ../../s1/sentinel.conf

Guess you like

Origin blog.csdn.net/lanlan112233/article/details/130134302