mysqldump主要参数探究
在數據庫的日常維護中,對于數據量小的備份,我們常常采用的是邏輯備份,也就是使用mysqldump導出。數據量比較大的備份會使用percona的xtrabackup,關于xtrabackup工具的使用以及原理請參考我前面的文章xtrabackup詳解,當然還有其他的方法。現在我們主要來深入看看mysqldump幾個比較常用參數的原理。要需要了解mysqldump各種參數做了什么,我們需要打開查詢日志來分析,打開查詢日志很簡單,在[mysqld]段落添加如下參數:
general_log=1 general_log_file=/data/mysql/general.log重啟mysql服務器,然后我們所有的操作都會記錄日志了(線上繁忙的服務器不建議開啟)
兩張表結構如下,一個是MyISAM,一個是InnoDB的
mysql> show create table tb1\G *************************** 1. row ***************************Table: tb1 Create Table: CREATE TABLE `tb1` (`id` int(11) DEFAULT NULL,`name` varchar(20) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec)mysql> show create table tb2\G *************************** 1. row ***************************Table: tb2 Create Table: CREATE TABLE `tb2` (`id` int(11) DEFAULT NULL,`name` varchar(20) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 1 row in set (0.00 sec)mysql>1.不加參數備份tb2表,該表是MyISAM引擎
[root@MySQL-01 mysql]# mysqldump test tb2 > /tmp/a.sql 5 Init DB test5 Query SHOW TABLES LIKE 'tb2'5 Query LOCK TABLES `tb2` READ /*!32311 LOCAL */5 Query show table status like 'tb2'5 Query SET SQL_QUOTE_SHOW_CREATE=15 Query SET SESSION character_set_results = 'binary'5 Query show create table `tb2`5 Query SET SESSION character_set_results = 'utf8'5 Query show fields from `tb2`5 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `tb2`5 Query SET SESSION character_set_results = 'binary'5 Query use `test`5 Query select @@collation_database5 Query SHOW TRIGGERS LIKE 'tb2'5 Query SET SESSION character_set_results = 'utf8'5 Query UNLOCK TABLES5 Quit可以看見不加任何參數,自動加上了LOCK TABLES READ LOCAL鎖,,該鎖不會阻止讀,也不會阻止新的數據插入。所以不加參數的dump是非常danger。
2.--lock tables
[root@MySQL-01 mysql]# mysqldump --lock-tables test tb2 > /tmp/a.sql 6 Query SHOW TABLES LIKE 'tb2'6 Query LOCK TABLES `tb2` READ /*!32311 LOCAL */6 Query show table status like 'tb2'6 Query SET SQL_QUOTE_SHOW_CREATE=16 Query SET SESSION character_set_results = 'binary'6 Query show create table `tb2`6 Query SET SESSION character_set_results = 'utf8'6 Query show fields from `tb2`6 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `tb2`6 Query SET SESSION character_set_results = 'binary'6 Query use `test`6 Query select @@collation_database6 Query SHOW TRIGGERS LIKE 'tb2'6 Query SET SESSION character_set_results = 'utf8'6 Query UNLOCK TABLES6 Quit跟默認不加參數是一樣的,了LOCK TABLES READ LOCAL鎖,,該鎖不會阻止讀,也不會阻止新的數據插入。
3.--lock-all-tables
[root@MySQL-01 mysql]# mysqldump --lock-all-tables test tb2 > /tmp/a.sql 7 Query FLUSH TABLES7 Query FLUSH TABLES WITH READ LOCK7 Init DB test7 Query SHOW TABLES LIKE 'tb2'7 Query show table status like 'tb2'7 Query SET SQL_QUOTE_SHOW_CREATE=17 Query SET SESSION character_set_results = 'binary'7 Query show create table `tb2`7 Query SET SESSION character_set_results = 'utf8'7 Query show fields from `tb2`7 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `tb2`7 Query SET SESSION character_set_results = 'binary'7 Query use `test`7 Query select @@collation_database7 Query SHOW TRIGGERS LIKE 'tb2'7 Query SET SESSION character_set_results = 'utf8'7 Quit可以發現執行了flush tables(關閉所有已打開的表),它請求發起一個全局的讀鎖(FLUSH TABLES WITH READ LOCK)會阻止對所有表的寫入操作,以此來確保數據的一致性。備份完成后,該會話斷開,會自動解鎖。
4.--local-all-tables --master-data=2 這里master-data=2是比較常用的,當然也可以使用1,使用1后,備份記錄里面日志偏移相關的提示沒有注釋。
[root@MySQL-01 mysql]# mysqldump --lock-all-tables --master-data=2 test tb2 > /tmp/a.sql 10 Query /*!40100 SET @@SQL_MODE='' */10 Query /*!40103 SET TIME_ZONE='+00:00' */10 Query FLUSH /*!40101 LOCAL */ TABLES10 Query FLUSH TABLES WITH READ LOCK10 Query SHOW MASTER STATUS10 Init DB test10 Query SHOW TABLES LIKE 'tb2'10 Query show table status like 'tb2'10 Query SET SQL_QUOTE_SHOW_CREATE=110 Query SET SESSION character_set_results = 'binary'10 Query show create table `tb2`10 Query SET SESSION character_set_results = 'utf8'10 Query show fields from `tb2`10 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `tb2`10 Query SET SESSION character_set_results = 'binary'10 Query use `test`10 Query select @@collation_database10 Query SHOW TRIGGERS LIKE 'tb2'10 Query SET SESSION character_set_results = 'utf8'10 Quit可以發現沒什么變化,只是多執行了SHOW MASTER STATUS,我們看看備份出來的sql
[root@MySQL-01 mysql]# grep 'CHANGE MASTER TO' /tmp/a.sql -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=959; [root@MySQL-01 mysql]#可以看見有記錄二進制,以及日志的偏移量,這種用法在做主從的時候很實用。
5.--local-all-tables --master-data=2 --flush-logs
[root@MySQL-01 mysql]# mysqldump --lock-all-tables --master-data=2 --flush-logs test tb2 > /tmp/a.sql 17 Connect root@localhost on17 Query /*!40100 SET @@SQL_MODE='' */17 Query /*!40103 SET TIME_ZONE='+00:00' */17 Query FLUSH /*!40101 LOCAL */ TABLES17 Query FLUSH TABLES WITH READ LOCK17 RefreshId Command Argument17 Query SHOW MASTER STATUS17 Init DB test17 Query SHOW TABLES LIKE 'tb2'17 Query show table status like 'tb2'17 Query SET SQL_QUOTE_SHOW_CREATE=117 Query SET SESSION character_set_results = 'binary'17 Query show create table `tb2`17 Query SET SESSION character_set_results = 'utf8'17 Query show fields from `tb2`17 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `tb2`17 Query SET SESSION character_set_results = 'binary'17 Query use `test`17 Query select @@collation_database17 Query SHOW TRIGGERS LIKE 'tb2'可以看見添加多一個參數flush-logs日志里面并沒有明顯變化,但是該命令會刷新binlog,從新產生一個新的binlog。
下面我們看看備份表tb1,該表是innodb引擎的,眾所周知,innodb實現了mvcc,多版本并發控制,那么我們看看一個非常重要的參數
6.?--single-transaction?
mysqldump --single-transaction test tb1 > /tmp/a.sql 19 Connect root@localhost on19 Query /*!40100 SET @@SQL_MODE='' */19 Query /*!40103 SET TIME_ZONE='+00:00' */19 Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ19 Query START TRANSACTION /*!40100 WITH CONSISTENT SNAPSHOT */19 Query UNLOCK TABLES19 Init DB test19 Query SHOW TABLES LIKE 'tb1'19 Query show table status like 'tb1'19 Query SET SQL_QUOTE_SHOW_CREATE=119 Query SET SESSION character_set_results = 'binary'19 Query show create table `tb1`19 Query SET SESSION character_set_results = 'utf8'19 Query show fields from `tb1`19 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `tb1`19 Query SET SESSION character_set_results = 'binary'19 Query use `test`19 Query select @@collation_database19 Query SHOW TRIGGERS LIKE 'tb1'19 Query SET SESSION character_set_results = 'utf8'19 QuitInnoDB 表在備份時,通常啟用選項?--single-transaction?來保證備份的一致性,可以實際上它的工作原理是設定本次會話的隔離級別為:REPEATABLE READ。然后啟動了一個快照,實現一致性非鎖定讀。下面是官方給出的解釋:
START TRANSACTION WITH CONSISTENT SNAPSHOT;The?WITH CONSISTENT SNAPSHOT?option starts a consistent read for storage engines that are capable of it. This applies only to?InnoDB. The effect is the same as issuing a?START TRANSACTION?followed by a?SELECT?from anyInnoDB?table. See?Section?14.2.7.2, “Consistent Nonlocking Reads”. The?WITH CONSISTENT SNAPSHOT?option does not change the current transaction isolation level, so it provides a consistent snapshot only if the current isolation level is one that permits consistent read (REPEATABLE READ?or?SERIALIZABLE).
7.?--single-transaction and --master-data
[root@MySQL-01 mysql]# mysqldump --single-transaction --master-data=2 test tb1 > /tmp/a.sql 22 Connect root@localhost on22 Query /*!40100 SET @@SQL_MODE='' */22 Query /*!40103 SET TIME_ZONE='+00:00' */22 Query FLUSH /*!40101 LOCAL */ TABLES22 Query FLUSH TABLES WITH READ LOCK22 Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ22 Query START TRANSACTION /*!40100 WITH CONSISTENT SNAPSHOT */22 Query SHOW MASTER STATUS22 Query UNLOCK TABLES22 Init DB test22 Query SHOW TABLES LIKE 'tb1'22 Query show table status like 'tb1'22 Query SET SQL_QUOTE_SHOW_CREATE=122 Query SET SESSION character_set_results = 'binary'22 Query show create table `tb1`22 Query SET SESSION character_set_results = 'utf8'22 Query show fields from `tb1`22 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `tb1`22 Query SET SESSION character_set_results = 'binary'22 Query use `test`22 Query select @@collation_database22 Query SHOW TRIGGERS LIKE 'tb1'22 Query SET SESSION character_set_results = 'utf8由于增加了選項?--master-data,因此看見提交一個快速的全局讀鎖。難道這里是為了正確的記錄日志偏移量?(知道的童鞋請告知一聲)
?
總結一下:
備份MyISAM表的參數推薦如下:
mysqldump --lock-all-tables --master-data=2 --flush-logs db table > /data/backup/table.sql備份InnoDB表的參數推薦如下:
mysqldump --single-transaction --master-data=2 --flush-log db table > /data/backup/table.sql當然還有很多參數,比如設置字符集等,童鞋們自行help,你懂的!
?
參考資料:
https://dev.mysql.com/doc/refman/5.0/en/commit.html
http://dev.mysql.com/doc/refman/5.0/en/flush.html?
總結
以上是生活随笔為你收集整理的mysqldump主要参数探究的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iPhone 拍出来的照片发黄是什么原因
- 下一篇: 越发的近义词