您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何使用mysqldump備份,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
在一次使用mysqldump備份單表部分?jǐn)?shù)據(jù)時,發(fā)現(xiàn)無備份數(shù)據(jù)。陣針對這一奇怪現(xiàn)象,進(jìn)行分析。
#表結(jié)構(gòu)信息 mysql> show create table test.t1; +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t1 | CREATE TABLE `t1` ( `id` int(11) NOT NULL AUTO_INCREMENT, `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `idx_t` (`time`) ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 | +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) #表數(shù)據(jù)信息 mysql> select * from test.t1; +----+---------------------+ | id | time | +----+---------------------+ | 1 | 2018-12-10 22:15:39 | | 2 | 2018-12-10 22:15:47 | | 3 | 2018-12-10 22:15:50 | | 4 | 2018-12-10 22:15:56 | | 5 | 2018-12-10 22:15:57 | | 6 | 2018-12-10 22:15:58 | | 7 | 2018-12-10 22:15:58 | | 8 | 2018-12-10 22:16:06 | | 9 | 2018-12-10 22:16:06 | | 10 | 2018-12-10 22:16:07 | | 11 | 2018-12-10 22:16:08 | | 12 | 2018-12-10 22:16:13 | | 13 | 2018-12-10 22:16:13 | | 14 | 2018-12-10 22:16:14 | | 15 | 2018-12-10 22:16:15 | +----+---------------------+ 15 rows in set (0.00 sec)
使用mysqldump --where選項備份t1表部分?jǐn)?shù)據(jù)。
#使用mysqldump根據(jù)time列條件備份 mysqldump -uroot -p123456 --default-character-set=utf8 -q --master-data=2 --single-transaction --databases test --tables t1 --where "time>'2018-12-10 22:16:08'">beifen.sql #通過備份文件可以看出備份結(jié)果中并無數(shù)據(jù)。 ... LOCK TABLES `t1` WRITE; /*!40000 ALTER TABLE `t1` DISABLE KEYS */; /*!40000 ALTER TABLE `t1` ENABLE KEYS */; UNLOCK TABLES; ...
在MySQL中使用相同條件查詢,并無異常,可以查到數(shù)據(jù)。
[root@master ~]# mysql -uroot -p123456 -e "select * from test.t1 where time>'2018-12-10 22:16:08'" mysql: [Warning] Using a password on the command line interface can be insecure. +----+---------------------+ | id | time | +----+---------------------+ | 12 | 2018-12-10 22:16:13 | | 13 | 2018-12-10 22:16:13 | | 14 | 2018-12-10 22:16:14 | | 15 | 2018-12-10 22:16:15 | +----+---------------------+
嘗試備份t1全表數(shù)據(jù)
mysqldump -uroot -p123456 --default-character-set=utf8 -q --master-data=2 --single-transaction --databases test --tables t1 >beifen.sql #有數(shù)據(jù),但是仔細(xì)對照,我們可以發(fā)現(xiàn)時間回退了八個小時。 LOCK TABLES `t1` WRITE; /*!40000 ALTER TABLE `t1` DISABLE KEYS */; INSERT INTO `t1` VALUES (1,'2018-12-10 14:15:39'),(2,'2018-12-10 14:15:47'),(3,'2018-12-10 14:15:50'),(4,'2018-12-10 14:15:56'),(5,'2018-12-10 14:15:57'),(6,'2018-12-10 14:15:58'),(7,'2018-12-10 14:15:58'),(8,'2018-12-10 14:16:06'),(9,'2018-12-10 14:16:06'),(10,'2018-12-10 14:16:07'),(11,'2018-12-10 14:16:08'),(12,'2018-12-10 14:16:13'),(13,'2018-12-10 14:16:13'),(14,'2018-12-10 14:16:14'),(15,'2018-12-10 14:16:15'); /*!40000 ALTER TABLE `t1` ENABLE KEYS */; UNLOCK TABLES; #查看mysqldump備份文件頭部信息,mysqldump使用的是中時區(qū)。 ... /*!40103 SET TIME_ZONE='+00:00' */; ... #查看MySQL和系統(tǒng)時區(qū)。 mysql> show variables like '%time%'; +---------------------------------+-------------------+ | Variable_name | Value | +---------------------------------+-------------------+ | binlog_max_flush_queue_time | 0 | | connect_timeout | 10 | | datetime_format | %Y-%m-%d %H:%i:%s | | default_password_lifetime | 0 | | delayed_insert_timeout | 300 | | explicit_defaults_for_timestamp | OFF | | flush_time | 0 | | have_statement_timeout | YES | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 50 | | innodb_old_blocks_time | 1000 | | innodb_rollback_on_timeout | OFF | | interactive_timeout | 28800 | | lc_time_names | en_US | | lock_wait_timeout | 31536000 | | log_timestamps | UTC | | long_query_time | 10.000000 | | max_execution_time | 0 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 60 | | slow_launch_time | 2 | | system_time_zone | CST | | time_format | %H:%i:%s | | time_zone | +08:00 | | timestamp | 1544775697.554299 | | wait_timeout | 28800 | +---------------------------------+-------------------+ 29 rows in set (0.01 sec) [root@master ~]# date -R Wed, 12 Dec 2018 16:00:34 +0800 #模擬數(shù)據(jù)恢復(fù) mysql> drop table test.t1; mysql -uroot -p123456<beifen mysql> select * from t1; +----+---------------------+ | id | time | +----+---------------------+ | 1 | 2018-12-10 22:15:39 | | 2 | 2018-12-10 22:15:47 | | 3 | 2018-12-10 22:15:50 | | 4 | 2018-12-10 22:15:56 | | 5 | 2018-12-10 22:15:57 | | 6 | 2018-12-10 22:15:58 | | 7 | 2018-12-10 22:15:58 | | 8 | 2018-12-10 22:16:06 | | 9 | 2018-12-10 22:16:06 | | 10 | 2018-12-10 22:16:07 | | 11 | 2018-12-10 22:16:08 | | 12 | 2018-12-10 22:16:13 | | 13 | 2018-12-10 22:16:13 | | 14 | 2018-12-10 22:16:14 | | 15 | 2018-12-10 22:16:15 | +----+---------------------+ 15 rows in set (0.00 sec) #數(shù)據(jù)恢復(fù)正常。但是存在一個問題,因為mysqldump備份時會把數(shù)據(jù)進(jìn)行時區(qū)轉(zhuǎn)換,導(dǎo)致mysqldump過濾條件與篩選的數(shù)據(jù)差8個小時,所以可能會使where條件過濾不準(zhǔn)確。比如文章開頭所碰到的問題。
解決辦法
#使用--skip-tz-utc,不使用mysqldump默認(rèn)的中時區(qū)。 mysqldump --default-character-set=utf8 -q --master-data=2 --single-transaction --databases test --tables t1 --where "time='2018-12-10 22:16:08'" -uroot -p123456 --skip-tz-utc>beifen.sql #數(shù)據(jù)完全正確,文件頭部無時區(qū)轉(zhuǎn)換,所以數(shù)據(jù)恢復(fù)時正常。 LOCK TABLES `t1` WRITE; /*!40000 ALTER TABLE `t1` DISABLE KEYS */; INSERT INTO `t1` VALUES (11,'2018-12-10 22:16:08'); /*!40000 ALTER TABLE `t1` ENABLE KEYS */; UNLOCK TABLES;
關(guān)于“如何使用mysqldump備份”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。