Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Redis learns 7Murray-persistence-related testing RDB method

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1. Redis only does cache configuration.

Comment out the configuration of RDB persistence

Add memory configuration and recycling configuration (maxmemory and maxmemory-policy)

2. Test RDB persistence

2.1. Turn off the RDB persistence test

Edit configuration file

# disable default rdb persistence, comment three lines, and add one line

# save 900 1

# save 300 10

# save 60 10000

Save ""

Restart the redis service

Set two values

Redis-cli-a Redis2019!

Warning: Using a password with'- a 'option on the command line interface may not be safe.

127.0.0.1 6379 > keys *

(empty list or set)

127.0.0.1 6379 > set K1 v1

OK

127.0.0.1 6379 > set K2 v2

OK

127.0.0.1 6379 > keys *

1) "K2"

2) "K1"

Restart the redis service

Found that the previously set value disappeared

Redis-cli-a Redis2019!

Warning: Using a password with'- a 'option on the command line interface may not be safe.

127.0.0.1 6379 > keys *

(empty list or set)

The configuration that does not turn off persistence is in effect.

2.2. Enable RDB persistence test

Restore the persistent configuration of rdb and restart the service

Set two values

Redis-cli-h 192.168.121.121-a Redis2019!

Warning: Using a password with'- a 'option on the command line interface may not be safe.

127.0.0.1 6379 > set K1 v1

OK

127.0.0.1 6379 > set K2 v2

OK

192.168.121.121 6379 > keys *

1) "K2"

2) "K1"

Use shutdown to shut down the service

Redis-cli-a Redis2019! Shutdown

Start the service again

Check the value and find that it exists.

Redis-cli-a Redis2019!

127.0.0.1 6379 > keys *

1) "K2"

2) "K1"

Set two more values

127.0.0.1 6379 > set K3 v3

OK

127.0.0.1 6379 > set K4 v4

OK

127.0.0.1 6379 > keys *

1) "K3"

2) "K4"

3) "K2"

4) "K1"

Use the kill command to kill the redis process

View the value again

Redis-cli-a Redis2019!

Warning: Using a password with'- a 'option on the command line interface may not be safe.

127.0.0.1 6379 > keys *

1) "K1"

2) "K2"

It is found that the value set later is missing, indicating that if you use shutdown to shut down the service, you will not lose data, but killing the process will lose data. Why?

Because stopping redis through redis-cli SHUTDOWN is actually a safe exit mode, redis will immediately generate a complete rdb snapshot of the data in memory and save it to disk when exiting.

The redis process was killed abnormally, and the data was not entered into the dump file, and several pieces of the latest data were lost.

Set save,save 5 1 (if more than one key changes every five seconds, take a dump.rdb snapshot based on the data now in the cache)

Restart the service

Set two values

Redis-cli-a Redis2019!

Warning: Using a password with'- a 'option on the command line interface may not be safe.

127.0.0.1 6379 > set K5 v5

OK

127.0.0.1 6379 > set K6 v6

OK

Wait 5 seconds after setting the value to kill the redis process

Start the service

Look at the value and find that the newly set value exists

Redis-cli-a Redis2019!

Warning: Using a password with'- a 'option on the command line interface may not be safe.

127.0.0.1 6379 > keys *

1) "K5"

2) "K6"

3) "K1"

4) "K2"

2.3.The save bgsave and regular backup of rdb files

Backup redis can manually use the SAVE command, and executing the SAVE command uses the main process to perform snapshot operations, which means that the main process is blocked during the SAVE process.

Another operation is to use BGSAVE. With BGSAVE, redis will fork a child process to perform the snapshot operation without affecting the main process.

Delete all key first

Then set the new value

127.0.0.1 6379 > set K1 v1

OK

127.0.0.1 6379 > set K2 v2

OK

If kill loses the redis process, the set key will disappear.

But execute save

127.0.0.1 purl 6379 > save

OK

Kill the redis process and start the redis service

Check key and find that the value is still there.

127.0.0.1 6379 > keys *

1) "K2"

2) "K1"

The discovery of the existence indicates that save is successful.

Set up a new key and execute bgsave

127.0.0.1 6379 > set K3 v3

OK

127.0.0.1 6379 > set K4 v4

OK

127.0.0.1 purl 6379 > bgsave

Background saving started

Kill the redis process and start the redis service

View key

127.0.0.1 6379 > keys *

1) "K3"

2) "K4"

3) "K1"

4) "K2"

Finding the existence of the newly created key means that bgsave is successful.

Regularly execute scripts to back up rdb files to the remote server (provided that the rsync master / slave has been installed and configured, and the master / slave has been logged in without secret)

Backing up the rdb file to the slave is in case the host redis data is lost, and the data before a certain period of time can be recovered.

Mkdir / data/usr/shell

Vi / data/usr/shell/redis_backrdb.sh

#! / bin/bash

REDIS_DIR=/data/usr/redis-4.0.11/data

Now= "$(date-dudes 0 day' +'% Y% m% d% H% M% S')"

REDIS_PW=Redis2019!

Redis-cli-a $REDIS_PW save

[$?-eq 0] & & {

Cp $REDIS_DIR/dump.rdb $REDIS_DIR/dump_$ {now} .rdb

Rsync-avz $REDIS_DIR/dump_$ {now} .rdb root@192.168.121.122:/backup

}

Write timing tasks and perform backup operations at 00:05 every morning.

Crontab-e

50 * / bin/sh / data/usr/shell/redis_backrdb.sh & > / dev/null

Reference:

Https://blog.csdn.net/baidu_41669919/article/details/79596209

Https://cloud.tencent.com/info/0420774e51445c7dbc75dcb1ff6fe49c.html

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report