A method for recovering control files of a database and a storage medium
By determining whether the control file is abnormal when the database is started, and generating new control files based on the wal log and data files, the problem of database control files is solved, and the database recovery and high reliability are achieved.
Patent Information
- Application Number
- CN202110881162.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-08-02
- Publication Date
- 2025-05-02
- Estimated Expiration
- 2041-08-02
AI Technical Summary
When the control files of the database are lost or corrupted, the database cannot be started and used normally, and the existing technology lacks effective recovery methods.
When the database is started, it is determined whether the control file is abnormal. If it is abnormal, the control file will be regenerated. The method is to generate the control file based on the database's wal log and data file. By comparing the consistency between the wal log and the data file, find the preset redo point of the wal log, and start playing back the wal log from this redo point to generate a new control file.
It effectively solves the problem of the database being unable to be used normally due to loss or corruption of control files, realizes rapid database detection and data recovery, and improves the reliability and user experience of the database.
Smart Images

Figure CN113590387B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method for recovering a control file of a database and a storage medium. Background Art
[0002] The reliability of a database is a very critical feature. The data in the database cannot be lost or damaged. The database can check the consistency of the data and reliably restore the data after discovering a problem.
[0003] In the era of big data, the amount of data is getting bigger and bigger. It is very time-consuming to fully verify all the data in the database. Therefore, in engineering, the database will maintain a control file (contol file) to save the database status and data-related information. By reading and checking this control file, the status of the database can be quickly determined. Only when an abnormality is found will the data be verified and repaired. However, in the case of loss of the control file, the database cannot know the status of the data, which makes the database unable to start and use normally. Summary of the invention
[0004] The present invention provides a method for recovering a control file of a database and a storage medium, so as to solve the problem that the control file of the database cannot be realized in the prior art.
[0005] In a first aspect, the present invention provides a method for recovering a control file of a database, the method comprising: when the database is started, determining whether the control file in the database is abnormal, wherein the control file stores the status of the database and relevant information of the data in the database; when it is determined that the control file is abnormal, regenerating the control file.
[0006] Optionally, determining whether the control file in the database is abnormal includes: judging whether the control file in the database can be read, and if not, determining that the control file is abnormal.
[0007] Optionally, the control file abnormality in the database includes: the control file is lost, the control file is damaged, or the control file is deleted by mistake.
[0008] Optionally, the regenerating the control file includes: generating the control file based on a WAL log of the database and a data file in the database.
[0009] Optionally, generating the control file based on the wal log of the database and the data file in the database includes: comparing the latest wal log with the corresponding data file in the database, determining whether the wal log is consistent with the data file, and if they are consistent, directly generating the control file based on the wal log; otherwise, searching for a preset redo point of the wal log, and generating the control file based on the wal log after the redo point.
[0010] Optionally, determining whether the wal log is consistent with the data file includes: based on the file name, block number and log sequence number that have changed in the latest wal log record, reading the log sequence number of the block number under the file name in the database, and determining whether the log sequence number in the wal log is the same as the log sequence number in the database; if they are the same, determining that the data are consistent; otherwise, determining that the data are inconsistent.
[0011] Optionally, searching for a preset redo point of the wal log includes: searching for a preset redo point of the wal log so that data recorded in the wal log reaches a consistent state with data in a database.
[0012] Optionally, searching for a preset redo point of the wal log so that the wal log record data reaches a consistent state with the data in the database includes: obtaining a log sequence number LSN(k1) in a redo point redo of the wal log, reading a log sequence number LSN(k2) of a corresponding modification in the database, and comparing LSN(k1) and LSN(k2), until a redo point redo whose LSN(k1) is less than LSN(k2) is found as the preset redo point.
[0013] Optionally, generating the control file based on the WAL log after the redo point includes: replaying the WAL log starting from the preset redo point to generate the control file.
[0014] In a second aspect, the present invention provides a computer-readable storage medium storing a computer program for signal mapping, wherein when the computer program is executed by at least one processor, the method for recovering the control file of the database described in any one of the above is implemented.
[0015] The beneficial effects of the present invention are as follows:
[0016] The present invention uses technical means to recover and restore the control file of the database after the control file of the database is lost or damaged, so that the database can be used normally, thereby effectively solving the existing problem that the database cannot be used due to control file loss and the like.
[0017] The above description is only an overview of the technical solution of the present invention. In order to more clearly understand the technical means of the present invention, it can be implemented according to the contents of the specification. In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, the specific implementation methods of the present invention are listed below. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present invention. Moreover, the same reference symbols are used throughout the accompanying drawings to represent the same components. In the accompanying drawings:
[0019] Figure 1 It is a flowchart of a method for recovering a control file of a database provided by the first embodiment of the present invention;
[0020] Figure 2 It is a flowchart of another method for recovering a control file of a database provided by the first embodiment of the present invention;
[0021] Figure 3 It is a flowchart of determining the consistency between the WAL log and the data file provided by the first embodiment of the present invention;
[0022] Figure 4 It is a flowchart of a method for finding redo points provided in the first embodiment of the present invention. DETAILED DESCRIPTION
[0023] The embodiment of the present invention aims at the situation that the database lacks protection for the control file itself. After the control file is lost, damaged or deleted by mistake, the database cannot know the status of the data, which will cause the database to be unable to start and use normally. After determining that the control file of the database is lost or damaged, the control file is restored by technical means, so that the database can return to normal. The present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described here are only used to explain the present invention and do not limit the present invention.
[0024] There are many backup and recovery technologies for data itself, but there is no timely recovery of control files. However, after the control file of the database is damaged, the database will still be in a state of unavailability and data loss, which has serious consequences. That is, the industry generally believes that the control file is very small and the probability of errors is small, so there is a lack of protection and recovery technology for it. However, in production, control files are often lost or damaged due to user misoperation, accidental deletion or file overwriting. Based on this, the solution of the present invention is specially proposed, and the solution of the present invention will be explained and illustrated in detail below:
[0025] The first embodiment of the present invention provides a method for restoring a control file of a database. Figure 1 , the method comprising:
[0026] S101. When a database is started, determining whether a control file in the database is abnormal, wherein the control file stores information related to the state of the database and data in the database;
[0027] The database in the embodiment of the present invention may be any database storing data, and the present invention does not specifically limit this.
[0028] In specific implementation, the embodiment of the present invention determines whether the control file in the database is abnormal by judging whether the control file can be read.
[0029] The control file abnormality in the database includes: the control file is lost, the control file is damaged, or the control file is deleted by mistake, etc.
[0030] S102: When it is determined that the control file is abnormal, regenerate the control file.
[0031] That is to say, in the embodiment of the present invention, when the control file is abnormal, the control file is generated based on the wal log of the database and the data files in the database.
[0032] Specifically, when there is a control file, the database can quickly know the status of the database by reading the information in the control file at startup. If the database status is normal, it means that the data is correct and consistent, and the database starts working normally. If the database status is abnormal, the redo point information is read through the control file, and then the log is replayed from the redo point based on the wal log. After the data finally reaches a consistent and correct state, the database starts working.
[0033] like Figure 2 As shown, in a specific implementation, the embodiment of the present invention compares the latest wal log with the corresponding data file in the database to determine whether the wal log is consistent with the data file. If they are consistent, the control file is directly generated based on the wal log. Otherwise, the preset redo point of the wal log is searched, and the control file is generated based on the wal log after the redo point.
[0034] In this embodiment of the present invention, determining whether the wal log is consistent with the data file includes:
[0035] Based on the file name, block number and log sequence number that have changed in the latest wal log record, the log sequence number of the block number under the file name is read in the database to determine whether the log sequence number in the wal log is the same as the log sequence number in the database. If they are the same, the data is determined to be consistent, otherwise, the data is determined to be inconsistent.
[0036] It can be seen that the method described in the embodiment of the present invention can be fast and efficient, and there is no need to completely scan the user's data files. It only needs to analyze the recent WAL log files. After comparing a small amount of data, the state of the database can be determined, and the control file can be quickly restored when the data is inconsistent, thereby greatly improving the user experience.
[0037] In specific implementation, the embodiment of the present invention obtains the log sequence number LSN in the redo point redo of the wal log, reads the log sequence number LSN of the corresponding modification of the database, compares LSN and LSN, until a redo point redo with LSN less than LSN is found as the preset redo point.
[0038] That is, the embodiment of the present invention searches for a preset redo point of the wal log so that the data recorded in the wal log is consistent with the data in the database, and then replays the wal log from the preset redo point to generate the control file.
[0039] That is to say, in the embodiment of the present invention, when the control file of the database is lost or damaged, the control file is restored directly based on the information recorded in the wal log, or by searching the redo point redo in the wal log, finding that the log sequence number LSN in the redo point redo is smaller than the LSN of the corresponding modified database, and then starting from the redo point redo, the wal log is replayed to generate a new control file.
[0040] The following will be combined Figures 2 to 4 The method described in the embodiment of the present invention is explained and illustrated in detail:
[0041] like Figure 2As shown, when the database is started, the control file is checked first. If the control file can be read normally, the normal database startup process is followed. If the control file cannot be read, the latest wal log file is opened, the most recent wal log record is read from it, and the information in this record is compared with the information in the data file to verify whether the data file is consistent with the wal log. For example, if the latest wal record is a record of a table file change, the changed file name (m) + block number (n) + LSN (k1) is read from it. It should be noted that LSN is a unique number for each wal record and increases over time. Then, according to the information read above, file m is opened and the nth block is read. The latest LSN (k2) of this block stored in the header information of the block is read, and k1 and k2 are compared. If k1 and k2 are equal, it means that the data file is consistent with the wal log. After the control file is generated, the database starts normally. If k1 and k2 are not equal, it means that the data is inconsistent, and go to the next step. The specific process of judging consistency is as follows Figure 3 As shown;
[0042] like Figure 4 As shown, the embodiment of the present invention scans the wal records in order from new to old until a wal record of type checkpoint is found. This record is a checkpoint of the database, which stores redo information. Redo is a wal record, which indicates the starting point of the database for wal log replay. Read the LSN (k3) of the redo point, and read the LSN (k4) of the corresponding modified data block, compare k3 and k4, if k3 <= k4, it means that data can be recovered from this redo point. If k3>k4, it is necessary to continue scanning forward to find an earlier redo point and check until a suitable redo point is found. If the redo point is found, start from the redo point and replay the wal log until all wal logs are replayed, and the data is consistent with the wal. After the new control file is generated, the database starts normally.
[0043] In brief, the method described in the embodiment of the present invention mainly includes the following steps:
[0044] Determine whether the database control file is correct. If the control file is incorrect, read the latest wal log record from the wal log and compare it with the corresponding data file to determine whether the current data is consistent with the wal log. When the data is found to be inconsistent with the wal log, read the checkpoints from the wal file cyclically until a suitable redo point is found, and replay the wal log from the redo point to eventually make the data consistent with the wal log. Finally, rebuild the control file based on the above information, and then the database can start normally.
[0045] In general, the method described in the embodiment of the present invention can quickly detect the data in the database after the database control file is lost. If the data is intact, the database can be started and used normally immediately. If the data needs to be restored, the present invention only needs to replay the log of the data that must be repaired to obtain the control file. Therefore, the present invention can efficiently operate and maintain the numerical control as a whole, while improving the reliability of the database.
[0046] The second embodiment of the present invention provides a computer-readable storage medium, which stores a computer program for signal mapping. When the computer program is executed by at least one processor, it implements the method for recovering the control file of the database described in any one of the first embodiments of the present invention.
[0047] The relevant contents of the embodiments of the present invention can be understood by referring to the first embodiment of the present invention, and will not be discussed in detail here.
[0048] Although the preferred embodiments of the present invention have been disclosed for illustrative purposes, those skilled in the art will appreciate that various modifications, additions and substitutions are possible, and thus, the scope of the present invention should not be limited to the above embodiments.
Claims
1. A method for recovering a control file of a database, characterized in that: include: When the database is started, determining whether a control file in the database is abnormal, wherein the control file stores the state of the database and relevant information of the data in the database; When it is determined that the control file is abnormal, regenerating the control file; The regenerating the control file comprises: Compare the latest wal log with the corresponding data file in the database to determine whether the wal log is consistent with the data file. If they are consistent, directly generate the control file based on the wal log. Otherwise, find the preset redo point of the wal log and generate the control file based on the wal log after the redo point. The determining whether the wal log is consistent with the data file includes: Based on the file name m, block number n and log sequence number LSN (k1) that have changed in the latest wal log record, open the data file m and read the nth block in the data file m, and read the latest LSN (k2) of the block stored in the header information of the nth block; compare k1 and k2, if k1 and k2 are equal, it is determined that the data file is consistent with the wal log; if k1 and k2 are not equal, it is determined that the data is inconsistent; The step of searching for a preset redo point of the wal log includes: The wal records are scanned in order from the newest to the oldest until a wal record of the checkpoint type is found. The checkpoint stores redo point information, which indicates the starting point of the database for wal log replay; the LSN (k3) of the redo point is read, and the LSN (k4) of the corresponding modified data block is read; k3 and k4 are compared. If k3 is less than or equal to k4, it is used as the preset redo point to recover data from the redo point; if k3 is greater than k4, scan for an earlier redo point and check it until the preset redo point is found.
2. The method according to claim 1, characterized in that Determining whether the control file in the database is abnormal includes: It is determined whether the control file in the database can be read, and if not, it is determined that the control file is abnormal.
3. The method according to claim 1, characterized in that The control file abnormality in the database includes: the control file is lost, the control file is damaged, or the control file is deleted by mistake.
4. The method according to claim 1, characterized in that: Find the preset redo point of the wal log, including: Find the preset redo point of the wal log to make the data recorded in the wal log consistent with the data in the database.
5. The method according to claim 1, characterized in that The control file is generated based on the wal log after the redo point, including: The wal log is replayed starting from the preset redo point to generate the control file.
6. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program for signal mapping, and when the computer program is executed by at least one processor, the method for recovering a control file of a database according to any one of claims 1 to 5 is implemented.
Citation Information
Patent Citations
Database control file multiplexing method and device, medium and electronic equipment
CN111984445A
Database recovery method and device
CN112988456A