Yerel geri dönüş, SQL deyimlerini kullanarak verileri belirli bir zaman noktasında sorgulamanıza veya geri yüklemenize olanak tanır; bu sayede yanlışlıkla yapılan işlemlerden hızlıca kurtulursunuz.
Genel bakış
Veritabanı bakımı sırasında yanlışlıkla yapılan işlemler işletmenizi ciddi şekilde etkileyebilir. Binlog Flashback gibi geleneksel kurtarma yöntemleri karmaşıktır, hata yapmaya açıktır ve zaman alır. Yedekleme setinden geri yükleme ek sistem kaynakları gerektirir; büyük veri hacimlerinde kurtarma süresi öngörülemez hale gelir.
AliSQL, InnoDB motoru için yerel geri dönüş özelliğini sunarak karmaşık kurtarma işlemlerini ortadan kaldırır. Basit SQL deyimleriyle yanlış bir işlemden önceki tarihsel verileri sorgulayabilir veya geri yükleyebilirsiniz. Böylece kurtarma süresini azaltır ve işletme üzerindeki etkiyi en aza indirirsiniz.
Ön koşullar
Örneğiniz aşağıdaki veritabanı sürümlerinden birini çalıştırıyor olmalıdır. Küçük motor sürümü gereksinimleri karşılamıyorsa sürümü güncelleyin.
MySQL 8.4
Küçük motor sürümü 20210930 veya daha yeni olan MySQL 8.0.
Sınırlamalar
Yalnızca InnoDB motorlu tablolar desteklenir.
Bu özellik ek undo tablespace alanı tüketir. Maksimum boyutu innodb_undo_space_supremum_size parametresi ile sınırlayabilirsiniz.
Yerel geri dönüş sorgusu, belirtilen zaman damgasına en yakın zaman noktasındaki verileri döndürür. Tam eşleşme garantisi verilmez.
DDL işlemleri arasında tarihsel verilerin sorgulanması ve geri yüklenmesi desteklenmez. Örneğin, silinmiş bir tablonun içeriğini sorgulamak için yerel geri dönüşü kullanamazsınız.
Söz dizimi
Yerel geri dönüş, sorgulanacak veya geri yüklenecek zaman noktasını belirtmek için AS OF yan tümcesini sunar. Söz dizimi aşağıdaki gibidir:
SELECT ... FROM <table_name>
AS OF TIMESTAMP <expression>;
İfade, hedef zaman noktasını belirtir ve birden çok biçimi destekler. Örnekler:
SELECT ... FROM tablename
AS OF TIMESTAMP '2020-11-11 00:00:00';
SELECT ... FROM tablename
AS OF TIMESTAMP now();
SELECT ... FROM tablename
AS OF TIMESTAMP (SELECT now());
SELECT ... FROM tablename
AS OF TIMESTAMP DATE_SUB(now(), INTERVAL 1 minute);
Parametreler
Aşağıdaki parametreler yerel geri dönüş davranışını kontrol eder:
|
Parametre |
Açıklama |
|
innodb_rds_flashback_task_enabled |
Not
Yerel geri dönüş özelliğini devre dışı bıraktığınızda innodb_undo_retention parametresini de 0 olarak ayarlayın. |
|
innodb_undo_retention |
Not
|
|
innodb_undo_space_supremum_size |
|
|
innodb_undo_space_reserved_size |
Not
Büyük bir değer, aşırı undo kaydı birikimine neden olarak örnek performansını etkileyebilir. Gerekmedikçe bu parametreyi 0 olarak bırakın. |
Örnekler
# Get the current timestamp.
MySQL [mytest]> select now();
+---------------------+
| now() |
+---------------------+
| 2020-10-14 15:44:09 |
+---------------------+
1 row in set (0.00 sec)
# View the data.
MySQL [mytest]> select * from mt1;
+----+------+
| id | c1 |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
+----+------+
5 rows in set (0.00 sec)
# Run an update operation without a WHERE clause.
MySQL [mytest]> update mt1 set c1 = 100;
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5 Changed: 5 Warnings: 0
MySQL [mytest]> select * from mt1;
+----+------+
| id | c1 |
+----+------+
| 1 | 100 |
| 2 | 100 |
| 3 | 100 |
| 4 | 100 |
| 5 | 100 |
+----+------+
5 rows in set (0.00 sec)
# Query the historical data at the specified point in time. The query returns the expected result.
MySQL [mytest]> select * from mt1 AS OF timestamp '2020-10-14 15:44:09';
+----+------+
| id | c1 |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
+----+------+
5 rows in set (0.00 sec)
# If the specified time is outside the retention window for historical data, the query fails.
MySQL [mytest]> select * from mt1 AS OF timestamp '2020-10-13 14:44:09';
ERROR 7545 (HY000): The snapshot to find is out of range
# Start the data recovery.
MySQL [mytest]> create table mt1_tmp like mt1; # Create a temporary table with the same structure as the original table.
Query OK, 0 rows affected (0.03 sec)
MySQL [mytest]> insert into mt1_tmp
-> select * from mt1 AS OF
-> TIMESTAMP '2020-10-14 15:44:09'; # Insert the historical data from the original table into the temporary table.
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
MySQL [mytest]> select * from mt1_tmp; # Verify that the data in the temporary table is correct.
+----+------+
| id | c1 |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
+----+------+
5 rows in set (0.00 sec)
MySQL [mytest]> rename table mt1 to mt1_bak,
-> mt1_tmp to mt1; # (Before you perform this operation, stop all read and write operations on the table.) Rename the original table to mt1_bak and the temporary table to mt1 to complete the data recovery.
Query OK, 0 rows affected (0.02 sec)
MySQL [mytest]> select * from mt1; # Verify the restored data.
+----+------+
| id | c1 |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
+----+------+
5 rows in set (0.01 sec)