当前位置:网站首页>Redis master-slave replication cluster and recovery ideas for abnormal data loss # yyds dry goods inventory #
Redis master-slave replication cluster and recovery ideas for abnormal data loss # yyds dry goods inventory #
2022-07-05 01:04:00 【Jiangxiaolong's technology blog】
Redis Master slave replication cluster and recovery of abnormal data loss
1.redis Master slave replication principle
1. Send a synchronization request from the library to the main library
2. The master library receives the synchronization request sent from the library
3. The main library is in use bgsave Generate rdb file
4. Main library rdb After the file is generated, it is saved to disk , Let go rdb The file is sent to the slave library
5. Receive from the main library rdb file , take rdb File loading memory
When the slave database synchronizes with the master database , It will empty all data from the library , So I'm doing redis When the master-slave, try to choose the one without any data redis
Architecture diagram
Environmental preparation
IP | service | role |
---|---|---|
192.168.81.210 | redis-1 | Main library |
192.168.81.220 | redis-2 | Slave Library |
2. Deploy two redis
2.1.192.168.81.210 To configure
2.2.192.168.81.220 To configure
because redis-1 A set has been deployed redis, We can copy it directly
3.redis Master slave replication configuration
3.1. Batch create in the main library key
3.2. Configure the slave library to connect to the master library
Two configuration methods are implemented Redis Master slave copy :
- Configuration file implementation
- The command line executes the command to achieve
3.2.1. Set in the configuration file
3.2.2. Hot update configuration on the command line
3.3. Check whether the slave database synchronizes the master database data
3.4. The principle of master-slave synchronization in viewing logs
The master database and slave database have detailed logs recording replication
4.redis Master slave copy dangerous operation
4.1. Misoperation using hot update configuration
redis If master-slave replication uses hot update configuration , Sometimes the slave database is mistaken for the master database because the wrong host is selected , As a result, on the main library slaveof, This will cause the data on the main database to be emptied , Because there is no data from the database
When the slave database synchronizes with the master database, it will empty all its own data
Misoperation process
4.2. Avoid misoperation of hot update configuration
1. Do not use hot updates , Configure the master and slave directly in the configuration file
2. In execution slaveof When it's time to execute bgsave, Back up the data manually , Then in the data directory , take rdb Copy a file into another file , Make a backup , In this way, even if there is a problem, you can recover
bgsave After that, you don't have to restart , Direct backup rdb File can
5. simulation redis Master slave replication error data recovery
simulation redis Master slave synchronization misoperation data recovery
General train of thought :
1. Empty two redis The data of
2. Create some data on the main library , And then use bgsave command , Save data to disk , Then put the disk rdb File backup
3. Then synchronize the data from the library , Simulation master database data loss
4. from rdb Backup file restore database
The main purpose of this experiment is to operate redis Backup restore
5.1. Empty data
Two sets of redis All need to be operated
First close, then delete data, and then start
5.2. Batch create data in the main library and back up
5.3. Synchronizing data from the library results in data loss
At this time, the data from the database should be empty
Master database synchronizes data from slave database , After synchronization, the master database data is lost , In this way, the data loss of the main database is simulated
5.4. Restore the data of the main library
Turn off first redis, Restore , Finally, open redis
6. Simulated online environment, main library downtime and fault recovery
Ideas :
1. First, ensure that the master-slave synchronization has been configured , Both master and slave databases have data
2. close redis Main library , simulation redis Main library down
3. see redis Whether data still exists from the library , Whether it is readable or writable ( Can't write , Only read )
4. Turn off the slaveof, Stop master-slave synchronization , Connect the application redis Change your address to from library , Ensure business continuity
5. Repair the main library , After the main library goes online , Establish a master-slave replication relationship with the slave Library , The original slave Library (redis-2) It becomes the main library , Now the master library becomes the slave Library (redis-1) At this time Close the application , Stop data writing
6. Then the main library will now (redis-2) Data synchronization to the current slave Library (redis-1)
7. Close now from the library (redis-1) Of slaveof, Stop master-slave replication , Then the main library will now (redis-2) To configure salveof, Synchronize the original master library (redis-1)
8. Data synchronization finished , The original master database is restored from the database
A simple and clear sentence : The main library is down for some reason and cannot provide services , Directly switch from the library to the main library to provide services , Then, after the original primary database is restored, synchronize the data of the current primary database , Then stop all programs running online , Synchronize the current primary database with the restored primary database , Regenerate the master-slave relationship .
6.1. Configure the master-slave analog online environment
Before configuring master-slave, ensure that there is data on the master database
6.2. Simulate the downtime of the master library and verify whether the slave library is readable and writable
Once the main library goes down , The slave library will always output a log indicating that it is unable to connect to the main library
6.3. Close the master-slave replication of the slave library to ensure uninterrupted business
Now the main library is not available , You can only read from the library, not write , But there's only one piece of data , We can only turn off master-slave replication on the slave Library , Make the slave library become the master library , Reconfigure the business redis Address , First of all, we must ensure that the business is not interrupted
6.4. Repair the failed master database and synchronize the data of the original slave database
After repairing the main library , There are already data , Why synchronize data from the library , Because the moment the main library hangs up , The master-slave configuration is removed from the library , I have become the main library , It also provides data writing for a period of time , At this time, the data from the library is the most complete
Synchronize the main library now ( The original slave Library ) When the data is , First turn off the app , Don't write data in
In the main warehouse ( The original from the library ) Write several new data , New data generated by simulation
Configure master-slave synchronization on the main library that is going online again , Make yourself from the library , Synchronize the main library's ( The original slave Library ) data
After synchronization , Now from the library ( The repaired main database already has the latest data )
6.5. Go back online from the main library
The "re online from the database" here refers to the original failed main database , Now it has been synchronized to the latest data , Therefore, it is necessary to go online and become the main database , He was chosen as the master library because his performance is higher than that of the slave library , Avoid future failures due to performance , So switch
6.6. To be applied redis The address is changed to the address of the main library again
At present, the main database has been restored , And the master-slave synchronization relationship is re established before the master-slave , Now you can apply redis The address is modified to be the main library , Just start the application
边栏推荐
- Playwright recording
- Open3d uses GICP to register point clouds
- [Yocto RM]10 - Images
- Safety learning week4
- There is a new Post-00 exam king in the testing department. I really can't do it in my old age. I have
- [wave modeling 3] three dimensional random real wave modeling and wave generator modeling matlab simulation
- 【海浪建模3】三维随机真实海浪建模以及海浪发电机建模matlab仿真
- “薪資倒掛”、“畢業生平替” 這些現象說明測試行業已經...
- Introduction to the gtid mode of MySQL master-slave replication
- 【C】 (written examination questions) pointer and array, pointer
猜你喜欢
College degree, what about 33 year old Baoma? I still sell and test, and my monthly income is 13K+
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
Binary conversion problem
2022.07.03 (lc_6111_counts the number of ways to place houses)
Implementation steps of master detail detail layout mode of SAP ui5 application
实战模拟│JWT 登录认证
[STM32] (I) overview and GPIO introduction
Innovation leads the direction. Huawei Smart Life launches new products in the whole scene
Pycharm professional download and installation tutorial
揭露测试外包公司,关于外包,你或许听到过这样的声音
随机推荐
Ruby tutorial
MySQL uses the explain tool to view the execution plan
Poap: the adoption entrance of NFT?
【海浪建模3】三维随机真实海浪建模以及海浪发电机建模matlab仿真
Discrete mathematics: reasoning rules
"Upside down salary", "equal replacement of graduates" these phenomena show that the testing industry has
[development of large e-commerce projects] performance pressure test - Performance Monitoring - heap memory and garbage collection -39
Take you ten days to easily complete the go micro service series (IX. link tracking)
||Interview questions you will encounter
[development of large e-commerce projects] performance pressure test - Optimization - impact of middleware on performance -40
Research Report on the overall scale, major producers, major regions, products and application segmentation of agricultural automatic steering system in the global market in 2022
2022.07.03 (LC 6109 number of people who know secrets)
【海浪建模1】海浪建模的理论分析和matlab仿真
The performance of major mainstream programming languages is PK, and the results are unexpected
Call Huawei order service to verify the purchase token interface and return connection reset
skimage: imread & imsave & imshow
Insert sort of sort
1189. Maximum number of "balloons"
SAP UI5 应用开发教程之一百零七 - SAP UI5 OverflowToolbar 容器控件介绍的试读版
Basic operation of database and table ----- phased test II