当前位置:网站首页>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
边栏推荐
- Complete knapsack problem (template)
- The difference between string STR and new string
- What if the programmer's SQL data script coding ability is weak and Bi can't do it?
- 有哪些收益稳定的理财产品,这两个都不错
- 每日刷题记录 (十三)
- MySQL uses the explain tool to view the execution plan
- Ruby tutorial
- Query for Boolean field as "not true" (e.g. either false or non-existent)
- Apifox (postman + swagger + mock + JMeter), an artifact of full stack development and efficiency improvement
- “薪资倒挂”、“毕业生平替” 这些现象说明测试行业已经...
猜你喜欢
BGP comprehensive experiment
ROS command line tool
[development of large e-commerce projects] performance pressure test - Optimization - impact of middleware on performance -40
dotnet-exec 0.6.0 released
SAP UI5 应用开发教程之一百零六 - 如何提高 SAP UI5 应用路由 url 的可读性试读版
小程序直播 + 电商,想做新零售电商就用它吧!
SAP UI5 应用的主-从-从(Master-Detail-Detail)布局模式的实现步骤
Parameter passing mechanism of member methods
【海浪建模1】海浪建模的理论分析和matlab仿真
两个数相互替换
随机推荐
小程序直播 + 电商,想做新零售电商就用它吧!
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
Grabbing and sorting out external articles -- status bar [4]
Daily question brushing record (13)
Robley's global and Chinese markets 2022-2028: technology, participants, trends, market size and share Research Report
【海浪建模2】三维海浪建模以及海浪发电机建模matlab仿真
Playwright之录制
抓包整理外篇——————状态栏[ 四]
大专学历,33岁宝妈又怎样?我照样销售转测试,月入13k+
Two numbers replace each other
【纯音听力测试】基于MATLAB的纯音听力测试系统
[STM32] (I) overview and GPIO introduction
Operator explanation
视频网站手绘
User login function: simple but difficult
College degree, what about 33 year old Baoma? I still sell and test, and my monthly income is 13K+
[microprocessor] VHDL development of microprocessor based on FPGA
【大型电商项目开发】性能压测-优化-中间件对性能的影响-40
Ruby tutorial
dotnet-exec 0.6.0 released