当前位置:网站首页>Redis: redis configuration file related configuration and redis persistence

Redis: redis configuration file related configuration and redis persistence

2022-07-04 22:51:00 dengfengling999


Catalog :

(1)Redis Configuration file for

(2) General configuration

(3) Security configuration

(4)Redis Persistence strategy of RDB

(5)Redis Persistence strategy of AOF 

(6)Redis The persistence of


 

 

(1)Redis Configuration file for

  1. redis.conf Storage location :

Redis Under the installation root directory of (/opt/redis-5.0.2),Redis This configuration file will be loaded at startup , Work according to the configuration at run time . Sometimes we take out this document , Store in a separate location , When it starts Must be clearly specified Which profile to use , This document will take effect .

After modifying the configuration file , Start up Redis When you need , Add the name of the configuration file

  2.Redis Network related configuration

    (1)bind: binding IP Address , Other machines can use this IP visit Redis, The default binding 127.0.0.1, It can also be modified to the local IP Address .

   (2)port: To configure Redis Ports occupied , The default is 6379.

  (3)tcp-keepalive:TCP Connect the survival strategy , Can pass tcp-keepalive Configuration item to set , The unit is in seconds , If set to 60 second , be server End meeting every 60 Second to connect idle clients once ACK request , To check if the client has hung up , For unresponsive clients, the connection is closed . If set to 0, No life preservation test will be carried out .

  When connecting the client, you must follow the port number specified by him 、 and IP Address connection : You can no longer use the default connection method

 

The port number should also be added when closing the client :

 

(2) General configuration

 

 

Redis The general configuration of

1、loglevel: The level of logging , The development phase can be set to debug, The production phase is usually set to notice perhaps warning.

2、logfile: Specify the log file name , If you don't specify ,Redis Only standard output . To ensure that the directory where the log file is located must exist , The file can not exist . Still in redis Specify the configuration file to use at startup , Otherwise configuration doesn't work .

3、databases: To configure Redis Number of databases , The default is 16 individual .

for example : In the configuration file , Change log information

  Set the log information , Not displayed on the console , Write to log file

 

  Restart Redis: It is found that the console has no output , Printed to log file

 (3) Security configuration

Redis Security configuration

1、requirepass: To configure Redis Access code for . No password is configured by default , That is, access does not require password authentication . This configuration item needs to be in protected-mode=yes It works . Log in to the client with a password :

redis-cli -h ip -p 6379 -a pwd

 

(4)Redis Persistence strategy of RDB

 

 Redis Of RDB To configure

1、save <seconds> <changes>: Configure composite snapshot trigger conditions , namely Redis stay seconds Seconds key change changes Time ,Redis Save the data in the snapshot to disk once . The default policy is :

1 It changed in minutes 1 Ten thousand times

perhaps 5 It changed in minutes 10 Time

perhaps 15 It changed in minutes 1 Time

If you want to disable Redis Persistence of , Then put all the save The configuration is commented out .

2、stop-writes-on-bgsave-error: When bgsave Stop writing data to disk when snapshot operation fails , This ensures the consistency of memory data and disk data , But if you don't care about this consistency , To be in bgsave Continue to write when there is an error in the snapshot operation , It needs to be configured as no.

3、rdbcompression: Set whether snapshots stored on disk are compressed , Set to yes when ,Redis Will be used LZF Algorithm for compression ; If you don't want to consume CPU If you compress , It can be set to no, Turn off this function .

4、rdbchecksum: After storing snapshots , Can also let Redis Use CRC64 Algorithm to check data , But this will consume some performance , If the system pays more attention to the improvement of performance , It can be set to no, Turn off this function .

5、dbfilename:Redis File name generated by persistent data , The default is dump.rdb, You can also configure it yourself .

6、dir:Redis Directory where persistent data generation files are saved , The default is ./ namely redis Start directory for , You can also configure it yourself .

(5)Redis Persistence strategy of AOF 

To make up for the above RDB The defects of , If you just write a piece of data , Trigger conditions are not met , The plane crashed ,  Therefore, the following AOF Strategy , But this efficiency ratio RDB low ,Redis By default RDB Strategy

Redis AOF To configure

1、appendonly: Configure whether to turn on AOF,yes Open for indication ,no Means closing . The default is no.

2、appendfilename:AOF Save filename

3、appendfsync:AOF Asynchronous persistence strategy

always: Synchronous persistence , Every time a data change occurs, it is immediately written to disk . Poor performance but good data integrity ( slow , Security )
everysec: Factory default recommendation , Record asynchronously once per second ( The default value is )
no: No instant synchronization , It's up to the operating system to decide when to synchronize .

4no-appendfsync-on-rewrite: Can I use appendsync, Default no, It can guarantee the security of data .

5auto-aof-rewrite-percentage: Set the base percentage of rewrite

6auto-aof-rewrite-min-size: Set the overridden benchmark

 

(6)Redis The persistence of

redis Is a memory database , It stores data in memory , This not only speeds up the reading speed, but also brings new problems to the data security , When redis After the server goes down ,redis All data in the database will be lost . To solve this problem ,redis Provides persistence ——RDB and AOFAppend Only File.

 

RDB

RDBRedis DataBase) yes Redis Default persistence scheme . Within a specified time interval , Perform the specified number of writes , Data in memory is written to disk . That is to generate a dump.rdb file .Redis Restart will be done by loading dump.rdb File to recover data .

RDB principle :

Redis Will copy a process that is the same as the current process . All the data for the new process ( Variable 、 environment variable 、 Program counter, etc ) The values are consistent with the original process , But it's a whole new process , And as a child of the original process , To persist .

The whole process , The main process is not going to do anything IO Operation of the , This ensures extremely high performance .

If large-scale data recovery is needed , And it's not very sensitive to the integrity of data recovery , that RDB The way is better than AOF It's more efficient .RDB The disadvantage is that the data may be lost after the last persistence .

1.RDB Saved files :

RDB The saved file is dump.rdb file , The location is saved in Redis Start directory for .Redis Each time the data is synchronized to the disk, a dump.rdb file , new dump.rdb Will overwrite the old dump.rdb file .

2. To configure RDB Persistence strategy

stay redis.conf In the snapshot configuration of , To configure RDB Saved policy .

Execute... On the client side FLUSHDB perhaps FLUSHALL perhaps SHUTDOWN when , The data in the snapshot will also be saved to dump.rdb, But this operation has cleared the data , The saved file is also empty , It makes no sense .

3. Save manually RDB snapshot

save Command to perform a synchronous save operation , Will the current Redis All data snapshots of the instance (snapshot) With RDB The file is saved to the hard disk .

because save Instructions will block all clients , So the task of saving the database is usually done by  BGSAVE  Commands are executed asynchronously , and save As a last resort to save data , When the backend process responsible for saving data unfortunately has problems .

4.RDB Data recovery

The script will Redis Produced dump.rdb File backup (cp dump.rdb dump_bak.rdb), Each start Redis front , Put the backup dump.rdb File Replace to Redis The corresponding table of contents ( stay redis.conf Medium matching dir Catalog ) Next ,Redis It will load on startup dump.rdb file , And read the data into memory .

5.RDB Summary ​​​​​​​ 

Redis Default on RDB Persistence mode , It is suitable for large-scale data recovery, but its data consistency and integrity are poor .

AOF

AOF(Append Only File),Redis Not on by default . It appears to make up for RDB Deficiency ( Data inconsistency ), So it takes the form of a log to record every Write operations , and Additional To a file .Redis Restart will execute the write instruction from front to back according to the contents of the log file to complete the data recovery .

​​​​​​​AOF principle

Redis Log every write operation in the form of a log , take Redis All written instructions executed are recorded ( Reading operation does not record ),

Only files can be added but not rewritten ,redis At the beginning of startup, it will read the file and rebuild the data , In other words ,redis In case of restart, execute the write instruction from the front to the back according to the contents of the log file to complete the data recovery .

1.AOF Saved files

AOF The saved file is appendonly.aof file , The location is saved in Redis Start directory for . If it's on AOF,Redis Every record writing operation will go appendonly.aof Add new log contents to the file .

2. To configure AOF Persistence strategy

stay redis.conf Of “APPEND ONLY MODE” In the configuration module , To configure AOF Save strategy .

3.AOF Data recovery

The script will Redis Produced appendonly.aof File backup (cp appendonly.aofappendonly_bak.aof), Each start Redis front , Put the backup appendonly.aof File Replace to Redis The corresponding table of contents ( stay redis.conf Medium matching dir Catalog ) Next , Just turn on AOF The function of ,Redis Each start , The write instruction will be executed from front to back according to the contents of the log file to complete the data recovery .

But in real development , It may be for some reason that appendonly.aof Abnormal file format , This leads to data restore failure , By command redis-check-aof --fix appendonly.aof Make repairs . All write operation logs in the future will be removed from the abnormal part .

4.AOF Rewriting of

AOF By means of document addition , The documents will become larger and larger to avoid this , New rewrite mechanism , When AOF When the file size exceeds the set threshold ,Redis Will start AOF The content of the file is compressed , Keep only the smallest instruction set that can recover data .

       AOF When files continue to grow and become too large , Meeting fork A new process to rewrite the file ( Also write temporary documents first and then rename), Traverse the data in memory of the new process , Each record has one Set sentence . rewrite aof Operation of file , Not reading the old aof file , Instead, the database contents in the whole memory are rewritten with a new command aof file , It's a bit like a snapshot .

Redis It will record the last time it was rewritten AOF size , The default configuration is when AOF The file size is last rewrite Double the size and the file is larger than 64M Trigger when . Of course , It can also be configured in the configuration file .

​​​​​​​AOF Summary :

Redis Manual opening required AOF Persistence mode ,AOF Data integrity ratio of RDB high , But there's more to the record , It will affect the efficiency of data recovery .

About Redis The use of persistence : If you only intend to use Redis Do the cache , You can turn off persistence . If you plan to use Redis The persistence of , Suggest RDB and AOF All on . Actually RDB More suitable for data backup , Keep a back hand .AOF Something is wrong. , also RDB.

AOF And RDB Mode can be enabled at the same time , It's not a conflict . If AOF Is available , that Redis Will load automatically at startup AOF, This file can provide better persistence guarantee .

原网站

版权声明
本文为[dengfengling999]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041957303348.html