MySQL Backup myloader
正文
之前的博文當中提到備份工具mydumper的使用,而軟體包中還包含了與之對應的恢復工具myloader,本文就總結下myloader的用法。關於mydumper的安裝與使用可以參考之前的博文:MySQL Backup mydumper。
檢視myloader的版本資訊:
# myloader -V
myloader 0.9.5, built against MySQL 5.7.21-21
主要選項
# myloader --help Usage: myloader [OPTION?] multi-threaded MySQL loader Help Options: -?, --help Show help options Application Options: -d, --directory Directory of the dump to import -q, --queries-per-transaction Number of queries per transaction, default 1000 -o, --overwrite-tables Drop tables if they already exist -B, --database An alternative database to restore into -s, --source-db Database to restore -e, --enable-binlog Enable binary logging of the restore data -h, --host The host to connect to -u, --user Username with the necessary privileges -p, --password User password -a, --ask-password Prompt For User password -P, --port TCP/IP port to connect to -S, --socket UNIX domain socket file to use for connection -t, --threads Number of threads to use, default 4 -C, --compress-protocol Use compression on the MySQL connection -V, --version Show the program version and exit -v, --verbose Verbosity of output, 0 = silent, 1 = errors, 2 = warnings, 3 = info, default 2 --defaults-file Use a specific defaults file
-d, --directory
要import的備份檔案所在目錄-q, --queries-per-transaction
每個事務的查詢數, 預設為1000-o, --overwrite-tables
如果表存在則刪除表-B, --database
指定需要還原成的資料庫名-s, --source-db
指定還原的源資料庫-e, --enable-binlog
還原資料時記錄binlog日誌-h, --host
指定連線host-u, --user
指定連線使用者,需有相應的許可權-p, --password
指定使用者密碼-a, --ask-password
指定使用者密碼提示輸入-P, --port
指定連線port-S, --socket
指定本地socket檔案連線-t, --threads
指定dump執行緒數, 預設是4-C, --compress-protocol
在mysql連線時使用壓縮協議-V, --version
顯示程式版本並退出-v, --verbose
顯示更詳細的輸出, 0 = silent, 1 = errors, 2 = warnings, 3 = info, 預設是2--defaults-file
指定預設引數檔案
還原流程
前期準備工作與mydumper一致,可參考mydumper備份流程
- 備份全庫
# mydumper -h 192.168.58.3 -u admin -a -P 3306 -t 8 -e -o /data/test/
- 檢視test2庫資訊
([email protected]) [(none)] > use test2;
Database changed
([email protected]) [test2] > show tables;
+-----------------+
| Tables_in_test2 |
+-----------------+
| t1 |
+-----------------+
1 row in set (0.00 sec)
([email protected]) [test2] > select * from t1;
+----+----------+
| id | title |
+----+----------+
| 1 | mydumper |
| 2 | myloader |
+----+----------+
2 rows in set (0.00 sec)
- 刪除test2庫
([email protected]) [(none)] > show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
| test2 |
+--------------------+
6 rows in set (0.01 sec)
([email protected]) [(none)] > drop database test2;
Query OK, 4 rows affected (0.16 sec)
([email protected]) [(none)] > show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
+--------------------+
5 rows in set (0.01 sec)
- 還原備份裡test2庫成test3庫
# myloader -h 192.168.58.3 -u admin -a -P 3306 -s test2 -B test3 -d /data/test/
- 檢視general log
-- 主執行緒連線資料庫,設定臨時session級別引數,預設匯入資料時不記錄binlog
264 Connect [email protected] on using TCP/IP
264 Query SET SESSION wait_timeout = 2147483
264 Query SET SQL_LOG_BIN=0
264 Query /*!40014 SET FOREIGN_KEY_CHECKS=0*/
-- 產生了4個子程序,設定會話級引數,預設不記錄binlog,關閉自動提交
265 Connect [email protected] on using TCP/IP
265 Query SET SESSION wait_timeout = 2147483
265 Query SET SQL_LOG_BIN=0
265 Query /*!40101 SET NAMES binary*/
265 Query /*!40101 SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */
265 Query /*!40014 SET UNIQUE_CHECKS=0 */
265 Query SET autocommit=0
266 Connect [email protected] on using TCP/IP
266 Query SET SESSION wait_timeout = 2147483
266 Query SET SQL_LOG_BIN=0
266 Query /*!40101 SET NAMES binary*/
266 Query /*!40101 SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */
266 Query /*!40014 SET UNIQUE_CHECKS=0 */
266 Query SET autocommit=0
267 Connect [email protected] on using TCP/IP
267 Query SET SESSION wait_timeout = 2147483
267 Query SET SQL_LOG_BIN=0
267 Query /*!40101 SET NAMES binary*/
267 Query /*!40101 SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */
267 Query /*!40014 SET UNIQUE_CHECKS=0 */
267 Query SET autocommit=0
268 Connect [email protected] on using TCP/IP
268 Query SET SESSION wait_timeout = 2147483
268 Query SET SQL_LOG_BIN=0
268 Query /*!40101 SET NAMES binary*/
268 Query /*!40101 SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */
268 Query /*!40014 SET UNIQUE_CHECKS=0 */
268 Query SET autocommit=0
-- 主執行緒建立還原資料庫和表
264 Query SHOW CREATE DATABASE `test3`
264 Query CREATE DATABASE `test3`
264 Query USE `test3`
264 Query /*!40101 SET NAMES binary*/
264 Query /*!40014 SET FOREIGN_KEY_CHECKS=0*/
264 Query /*!40103 SET TIME_ZONE='+00:00' */
264 Query CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
-- 子執行緒插入表資料
265 Query USE `test3`
266 Quit
265 Query START TRANSACTION
267 Quit
265 Query /*!40101 SET NAMES binary*/
265 Query /*!40014 SET FOREIGN_KEY_CHECKS=0*/
265 Query /*!40103 SET TIME_ZONE='+00:00' */
268 Quit
265 Query INSERT INTO `t1` VALUES
(1,"mydumper"),
(2,"myloader")
265 Query COMMIT
265 Quit
264 Quit
總結下myloader的工作流程:
- 主執行緒連線MySQL,根據指定選項設定會話級引數;
- 建立多個子執行緒(預設4個),設定會話級引數,關閉自動提交;
- 主執行緒建立庫和表;
- 子執行緒插入表資料;
- (如有)主執行緒建立函式、儲存過程、觸發器和檢視;
- import過程結束。
用法示例
- 還原單個庫
# myloader -h 192.168.58.3 -u admin -a -P 3306 -s test1 -o -v 3 -d /data/test/
- 還原庫並重命名
# myloader -h 192.168.58.3 -u admin -a -P 3306 -s test1 -B test1_bak -o -v 3 -d /data/test/
- 指定還原子執行緒數
# myloader -h 192.168.58.3 -u admin -a -P 3306 -s test1 -o -v 3 -t 8 -d /data/test/
很遺憾myloader沒有針對單表或都多表的還原操作命令,有一種方法是通過mydumper將個別大表進行指定chunk大小切割備份,並將每張表單獨目錄存放備份資料,必須是每張表的資料單獨放在一個目錄中,目錄中只包含一張表的資訊代表著還原時只須還原這一張表,避免影響相同庫下其他的表資料,因為myloader只能指定庫級別的引數選項,這樣就可以間接使用到myloader多執行緒還原功能,加快還原單錶速度。
以下做個測試:
-- test1.t3表有100萬行
([email protected]) [test1] > select count(*) from t3;
+----------+
| count(*) |
+----------+
| 1000000 |
+----------+
1 row in set (0.00 sec)
-- mydumper備份test1.t3,指定chunk大小為3M
# mydumper -h 192.168.58.3 -u admin -a -P 3306 -B test1 -T t3 -F 2 -e -o /data/test/
-- 刪除test1.t3表
([email protected]) [test1] > drop table t3;
Query OK, 0 rows affected (0.04 sec)
([email protected]) [test1] > select * from t3;
Empty set (0.00 sec)
-- myloader還原test1.t3
# myloader -h 192.168.58.3 -u admin -a -P 3306 -s test1 -o -v 3 -d /data/test/
** Message: 4 threads created
** Message: Dropping table or view (if exists) `test1`.`t3`
** Message: Creating table `test1`.`t3`
** Message: Thread 1 restoring `test1`.`t3` part 1
** Message: Thread 4 restoring `test1`.`t3` part 3
** Message: Thread 2 restoring `test1`.`t3` part 4
** Message: Thread 3 restoring `test1`.`t3` part 2
** Message: Thread 2 restoring `test1`.`t3` part 5
** Message: Thread 3 restoring `test1`.`t3` part 6
** Message: Thread 4 restoring `test1`.`t3` part 7
** Message: Thread 1 shutting down
** Message: Thread 4 shutting down
** Message: Thread 2 shutting down
** Message: Thread 3 shutting down
([email protected]) [test1] > select count(*) from t3;
+----------+
| count(*) |
+----------+
| 1000000 |
+----------+
1 row in set (0.00 sec)
由以上結果可見,還原使用到了多執行緒,提高了還原的效率。
參考
☆〖本人水平有限,文中如有錯誤還請留言批評指正!〗☆