mysqldump中skip-tz-utc參數介紹

前言:

在前面文章中,有提到過 mysqldump 備份文件中記錄的時間戳數據都是以 UTC 時區爲基礎的,在篩選恢復單庫或單表時要注意時區差別。後來再次查看文檔,發現 tz-utc、skip-tz-utc 參數與此有關,本篇文章我們一起來看下此參數的作用吧。

1.tz-utc與skip-tz-utc參數介紹

這兩個參數可以作用於 mysqldump 備份過程中,互爲相反參數。顧名思義可以看出,一個參數是將時間戳改爲 UTC 時區,另一個是跳過時區變動。

在 mysql 服務器上執行 mysqldump --help 的命令,可以看到下面一段話。

[root@host ~]# mysqldump --help
mysqldump  Ver 10.13 Distrib 5.7.23, for Linux (x86_64)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
...省略很多內容
  --tz-utc            SET TIME_ZONE='+00:00' at top of dump to allow dumping of
                      TIMESTAMP data when a server has data in different time
                      zones or data is being moved between servers with
                      different time zones.
                      (Defaults to on; use --skip-tz-utc to disable.)

--tz-utc 參數是 mysqldump 的默認參數,會使得 mysqldump 的導出文件的頂部加上一個設置時區的語句 SET TIME_ZONE='+00:00' ,這個時區是格林威治時間,也就是0時區。這樣當導出 timestamp 時間戳字段時,會把在服務器設置的當前時區下顯示的 timestamp 時間值轉化爲在格林威治時間下顯示的時間。比如我們數據庫採用北京時間東八區,mysqldump 導出的文件當中顯示的 timestamp 時間值相對於通過數據庫查詢顯示的時間倒退了8個小時。

知道了 --tz-utc ,那麼 --skip-tz-utc 的含義就是當 mysqldump 導出數據時,不使用格林威治時間,而使用當前 mysql 服務器的時區進行導出,這樣導出的數據中顯示的 timestamp 時間值也和表中查詢出來的時間值相同。

2.實驗參數具體作用

爲了更清楚瞭解這對參數的作用,下面我們來具體測試下,我們知道 mysqldump 後可以跟 where 條件來備份部分數據,若根據 timestamp 字段來備份部分數據,這對參數是否有影響呢?我們一併來驗證下:

先來看下我的環境設置及測試數據:

mysql> select version();
+------------+
| version()  |
+------------+
| 5.7.23-log |
+------------+
1 row in set (0.00 sec)
# 時區採用北京時間東八區
mysql> show variables like 'time_zone'; 
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| time_zone     | +08:00 |
+---------------+--------+
1 row in set (0.00 sec)

# 測試表 有datetime字段和timestamp字段 共10條數據 兩個時間顯示是相同的
mysql> show create table test_tb\G
*************************** 1. row ***************************
       Table: test_tb
Create Table: CREATE TABLE `test_tb` (
  `increment_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主鍵',
  `stu_id` int(11) NOT NULL COMMENT '學號',
  `stu_name` varchar(20) DEFAULT NULL COMMENT '學生姓名',
  `dt_time` datetime NOT NULL,
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創建時間',
  PRIMARY KEY (`increment_id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COMMENT='測試表'
1 row in set (0.00 sec)

mysql> select * from test_tb;
+--------------+--------+----------+---------------------+---------------------+
| increment_id | stu_id | stu_name | dt_time             | create_time         |
+--------------+--------+----------+---------------------+---------------------+
|            1 |   1001 | fgds     | 2020-07-10 09:43:28 | 2020-07-10 09:43:28 |
|            2 |   1002 | fgsw     | 2020-10-10 09:43:28 | 2020-10-10 09:43:28 |
|            3 |   1003 | vffg     | 2020-10-10 02:00:00 | 2020-10-10 02:00:00 |
|            4 |   1004 | wdsd     | 2020-10-31 23:43:28 | 2020-10-31 23:43:28 |
|            5 |   1005 | grdb     | 2020-11-01 00:00:00 | 2020-11-01 00:00:00 |
|            6 |   1006 | sdfv     | 2020-11-01 02:00:00 | 2020-11-01 02:00:00 |
|            7 |   1007 | fgfg     | 2020-11-06 02:00:00 | 2020-11-06 02:00:00 |
|            8 |   1008 | tyth     | 2020-11-10 09:43:28 | 2020-11-10 09:43:28 |
|            9 |   1009 | ewer     | 2020-11-10 09:43:28 | 2020-11-10 09:43:28 |
|           10 |   1010 | erre     | 2020-11-11 15:17:03 | 2020-11-11 15:17:03 |
+--------------+--------+----------+---------------------+---------------------+

mysqldump 默認開啓 tz-utc ,先來看下默認情況下的備份結果:

# 爲更明顯看出結果 我們使用skip-extended-insert來一行行展現數據
# 全庫備份
[root@host ~]# mysqldump -uroot -pxxxx  --skip-extended-insert --databases testdb > utc_testdb.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@host ~]# more utc_testdb.sql 
-- MySQL dump 10.13  Distrib 5.7.23, for Linux (x86_64)
--
-- Host: localhost    Database: testdb
-- ------------------------------------------------------
-- Server version       5.7.23-log

...省略
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
# 先保存老時區 然後將此會話時區改爲0時區
...省略
--
-- Dumping data for table `test_tb`
--

LOCK TABLES `test_tb` WRITE;
/*!40000 ALTER TABLE `test_tb` DISABLE KEYS */;
INSERT INTO `test_tb` VALUES (1,1001,'fgds','2020-07-10 09:43:28','2020-07-10 01:43:28');
INSERT INTO `test_tb` VALUES (2,1002,'fgsw','2020-10-10 09:43:28','2020-10-10 01:43:28');
INSERT INTO `test_tb` VALUES (3,1003,'vffg','2020-10-10 02:00:00','2020-10-09 18:00:00');
INSERT INTO `test_tb` VALUES (4,1004,'wdsd','2020-10-31 23:43:28','2020-10-31 15:43:28');
INSERT INTO `test_tb` VALUES (5,1005,'grdb','2020-11-01 00:00:00','2020-10-31 16:00:00');
INSERT INTO `test_tb` VALUES (6,1006,'sdfv','2020-11-01 02:00:00','2020-10-31 18:00:00');
INSERT INTO `test_tb` VALUES (7,1007,'fgfg','2020-11-06 02:00:00','2020-11-05 18:00:00');
INSERT INTO `test_tb` VALUES (8,1008,'tyth','2020-11-10 09:43:28','2020-11-10 01:43:28');
INSERT INTO `test_tb` VALUES (9,1009,'ewer','2020-11-10 09:43:28','2020-11-10 01:43:28');
INSERT INTO `test_tb` VALUES (10,1010,'erre','2020-11-11 15:17:03','2020-11-11 07:17:03');
# 可以看出timestamp時間值減去了8小時 而datetime時間值不變
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
# 再將時區改爲原時區
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
-- Dump completed on 2020-11-11 15:34:21

# 使用where條件備份單表部分數據 備份11月份以來的數據
# 數據庫中查詢
mysql> select * from test_tb where create_time >= '2020-11-01 00:00:00';
+--------------+--------+----------+---------------------+---------------------+
| increment_id | stu_id | stu_name | dt_time             | create_time         |
+--------------+--------+----------+---------------------+---------------------+
|            5 |   1005 | grdb     | 2020-11-01 00:00:00 | 2020-11-01 00:00:00 |
|            6 |   1006 | sdfv     | 2020-11-01 02:00:00 | 2020-11-01 02:00:00 |
|            7 |   1007 | fgfg     | 2020-11-06 02:00:00 | 2020-11-06 02:00:00 |
|            8 |   1008 | tyth     | 2020-11-10 09:43:28 | 2020-11-10 09:43:28 |
|            9 |   1009 | ewer     | 2020-11-10 09:43:28 | 2020-11-10 09:43:28 |
|           10 |   1010 | erre     | 2020-11-11 15:17:03 | 2020-11-11 15:17:03 |
+--------------+--------+----------+---------------------+---------------------+
6 rows in set (0.00 sec)
# mysqldump導出
[root@host ~]# mysqldump -uroot -pxxxx  --skip-extended-insert testdb test_tb --where "create_time >= '2020-11-01 00:00:00' " > utc_testdb2.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@host ~]# more utc_testdb2.sql 
-- MySQL dump 10.13  Distrib 5.7.23, for Linux (x86_64)
--
-- Host: localhost    Database: testdb
-- ------------------------------------------------------
-- Server version       5.7.23-log
...
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
...省略
--
-- Dumping data for table `test_tb`
--
-- WHERE:  create_time >= '2020-11-01 00:00:00' 

LOCK TABLES `test_tb` WRITE;
/*!40000 ALTER TABLE `test_tb` DISABLE KEYS */;
INSERT INTO `test_tb` VALUES (7,1007,'fgfg','2020-11-06 02:00:00','2020-11-05 18:00:00');
INSERT INTO `test_tb` VALUES (8,1008,'tyth','2020-11-10 09:43:28','2020-11-10 01:43:28');
INSERT INTO `test_tb` VALUES (9,1009,'ewer','2020-11-10 09:43:28','2020-11-10 01:43:28');
INSERT INTO `test_tb` VALUES (10,1010,'erre','2020-11-11 15:17:03','2020-11-11 07:17:03');
# 發現只導出4條
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

-- Dump completed on 2020-11-11 15:58:56

建議各位仔細看下上面導出結果,說實話,筆者原來也沒做過詳細測試,現在看到結果也是稍微有點喫驚的。默認情況下,全備出來的數據是沒問題的,雖然將 timestamp 時間值轉爲0時區顯示,但當你導入數據庫時還會以你的數據庫時區來展示 timestamp 時間。但使用 where 條件導出部分數據時,卻出現了數據庫中查詢得出的結果與dump導出的結果不同的情況,這個時候 mysqldump 只導出了轉化成0時區後的時間值符合 where 條件的數據,與直接查詢出的結果有出入,這是我原來沒注意到的。

再來看下使用 --skip-tz-utc 參數,看下這個參數是否符合我們的預期:

# 使用skip-tz-utc全備
[root@host ~]# mysqldump -uroot -pxxxx  --skip-extended-insert --skip-tz-utc --databases testdb > skiputc_testdb.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@host ~]# more skiputc_testdb.sql 
-- MySQL dump 10.13  Distrib 5.7.23, for Linux (x86_64)
--
-- Host: localhost    Database: testdb
-- ------------------------------------------------------
-- Server version       5.7.23-log
..省略 未見時區更改語句
--
-- Dumping data for table `test_tb`
--

LOCK TABLES `test_tb` WRITE;
/*!40000 ALTER TABLE `test_tb` DISABLE KEYS */;
INSERT INTO `test_tb` VALUES (1,1001,'fgds','2020-07-10 09:43:28','2020-07-10 09:43:28');
INSERT INTO `test_tb` VALUES (2,1002,'fgsw','2020-10-10 09:43:28','2020-10-10 09:43:28');
INSERT INTO `test_tb` VALUES (3,1003,'vffg','2020-10-10 02:00:00','2020-10-10 02:00:00');
INSERT INTO `test_tb` VALUES (4,1004,'wdsd','2020-10-31 23:43:28','2020-10-31 23:43:28');
INSERT INTO `test_tb` VALUES (5,1005,'grdb','2020-11-01 00:00:00','2020-11-01 00:00:00');
INSERT INTO `test_tb` VALUES (6,1006,'sdfv','2020-11-01 02:00:00','2020-11-01 02:00:00');
INSERT INTO `test_tb` VALUES (7,1007,'fgfg','2020-11-06 02:00:00','2020-11-06 02:00:00');
INSERT INTO `test_tb` VALUES (8,1008,'tyth','2020-11-10 09:43:28','2020-11-10 09:43:28');
INSERT INTO `test_tb` VALUES (9,1009,'ewer','2020-11-10 09:43:28','2020-11-10 09:43:28');
INSERT INTO `test_tb` VALUES (10,1010,'erre','2020-11-11 15:17:03','2020-11-11 15:17:03');
# timestamp時間值顯示與datetime顯示一樣 未做轉換
UNLOCK TABLES;
-- Dump completed on 2020-11-11 16:23:32

# 使用skip-tz-utc備份部分數據
[root@host ~]# mysqldump -uroot -pxxxx  --skip-extended-insert --skip-tz-utc testdb test_tb --where "create_time >= '2020-11-01 00:00:00' " > skiputc_testdb2.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@host ~]# more skiputc_testdb2.sql 
-- MySQL dump 10.13  Distrib 5.7.23, for Linux (x86_64)
--
-- Host: localhost    Database: testdb
-- ------------------------------------------------------
-- Server version       5.7.23-log
.. 省略
--
-- Dumping data for table `test_tb`
--
-- WHERE:  create_time >= '2020-11-01 00:00:00' 

LOCK TABLES `test_tb` WRITE;
/*!40000 ALTER TABLE `test_tb` DISABLE KEYS */;
INSERT INTO `test_tb` VALUES (5,1005,'grdb','2020-11-01 00:00:00','2020-11-01 00:00:00');
INSERT INTO `test_tb` VALUES (6,1006,'sdfv','2020-11-01 02:00:00','2020-11-01 02:00:00');
INSERT INTO `test_tb` VALUES (7,1007,'fgfg','2020-11-06 02:00:00','2020-11-06 02:00:00');
INSERT INTO `test_tb` VALUES (8,1008,'tyth','2020-11-10 09:43:28','2020-11-10 09:43:28');
INSERT INTO `test_tb` VALUES (9,1009,'ewer','2020-11-10 09:43:28','2020-11-10 09:43:28');
INSERT INTO `test_tb` VALUES (10,1010,'erre','2020-11-11 15:17:03','2020-11-11 15:17:03');
# 6條數據 和數據庫中查詢一致
UNLOCK TABLES;
-- Dump completed on 2020-11-11 16:28:39

從上面結果可以看出,使用 --skip-tz-utc 參數後,timestamp 時間戳字段值不會轉換,導出部分數據也符合預期。

3.一些小建議

那麼這個參數的意義何在呢?當你的數據庫服務器處於不同時區時。假設一個服務器在北京(東八區),一個服務器在東京(東九區),現在需要將北京服務器裏的數據導入至東京服務器。當導入按照默認不加 --skip-tz-utc 參數的dump文件,查詢的 timestamp 時間數據相對於在之前的東八區服務器的時間值多了一個小時,但由於東八區服務器裏的13點和東九區服務器裏的14點代表的是同一時刻,所以,在東九區的服務器裏顯示的多出的一個小時,這樣顯示是正確的。而如果增加 --skip-tz-utc 參數,dump文件導入東九區服務器後,儘管顯示的時間值和之前東八區服務器顯示的時間值相同,但兩者代表的時刻卻已經不同。

關於這個參數應該如何使用,我們首先應該明白,是否加上 --skip-tz-utc 參數,只會影響 timestamp 字段的導入導出,對 datetime 時間字段不會影響。

這裏筆者建議首先對 timestamp 字段使用作出規範。比如 timestamp 字段只用於創建時間和更新時間需求,只代表該行數據的創建及更新時間,做到與業務弱相關,其他時間字段儘量使用 datetime 。這樣即使 mysqldump 採用不同參數,實際產生影響也不大。

如果你的服務器處於不同時區,那建議還是按照默認來,這樣導入導出的數據都是正確的。如果你的服務器都是處於同一時區,那麼是否使用 --skip-tz-utc 參數區別不大,我們只需知道默認情況 mysqldump 會將 timestamp 時間值轉爲0時區存儲即可。當備份部分數據且以 timestamp 字段來篩選時,這時候建議增加 --skip-tz-utc 參數。這裏再次提醒下,從全備中篩選單庫或單表的備份時,也要注意下 timestamp 字段數據。

參考:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章