MySQL多線程匯入匯出工具Mydumper

來源:互聯網
上載者:User

今天從線上使用mysqldump將資料表從一個庫匯入到另外一個庫,結果匯出速度超級慢.於是網上搜了搜發現這個東東.測試之後發現還不錯.分享給大家.

簡單介紹一下
Mydumper是一個使用C語言編寫的多線程匯出匯入工具,並且能夠保證多個表之間的一致性.當然不是線程越多越好(這個跟伺服器的配置等諸多因素有關,只能作為一個經驗值而不是絕對值,機器好的時候,線程越多越好).

原理
前面提到保持資料一致性如何?呢?
下面是官方給出的解答
主要是使用flush tables with read lock和start transaction with consistent snapshot,在flush tables with read lock時開啟所有的線程,並且通過show master status和show slave status獲得當前的position(便於使用Mydumper重建slave以及確保多個表之間的資料一致性)
原版如下:
This is all done following best MySQL practices and traditions:
1. Global write lock is acquired ("FLUSH TABLES WITH READ LOCK")
2. Various metadata is read ("SHOW SLAVE STATUS","SHOW MASTER STATUS")
3. Other threads connect and establish snapshots ("START TRANSACTION WITH CONSISTENT SNAPSHOT")
3.1. On pre-4.1.8 it creates dummy InnoDB table, and reads from it.
4. Once all worker threads announce the snapshot establishment, master executes "UNLOCK TABLES" and starts queueing jobs.
安裝:
sudo yum install -y gcc gcc-c++ glib2-devel mysql-devel zlib-devel pcre-devel
cmake
make;make install
安裝完會生產兩個檔案
[mysql@localhost ~]$ ls /usr/local/bin/
mydumper  myloader
例子:
匯出
mysql> select count(*) from test;
+----------+
| count(*) |
+----------+
|  1048576 |
+----------+
1 row in set (0.41 sec)
[mysql@localhost bin]$ ./mydumper -u root -p 'xxxxxxxx' -t 4 -B test -T test -c --less-locking -o /home/mysql/
[mysql@localhost bin]$ ls /home/mysql/
test.test-schema.sql.gz  test.test.sql.gz
參數詳解
-u "使用者"
-p "密碼"
-t "指定並行數,預設是4"
-B "指定DB"
-T "指定表"
-c "壓縮"
--less-locking "盡量減少鎖表鎖定時間(針對InnoDB)"
-o "指定目錄"

例如:
設定長查詢的上限,如果存在比這個還長的查詢則退出mydumper,也可以設定殺掉這個長查詢
mydumper -u root -p 'xxxx' --long-query-guard 400 --kill-long-queries
通過regex設定正則表達,需要設定db名字
mydumper -u root -p 'xxxx' --regex=test.name

匯入
mysql> drop table test;
Query OK, 0 rows affected (0.26 sec)
mysql> exit
Bye
[mysql@localhost bin]$./myloader -u root -p 'xxxxx' -B test -d /home/mysql/
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test          |
+----------------+
1 row in set (0.00 sec)

mysql> select count(*) from test;
+----------+
| count(*) |
+----------+
|  1048576 |
+----------+
1 row in set (0.39 sec)

結論:
Mydumper在匯出匯入處理程序因為可以多線程進行,因此速度上優於mysqldump.(題外話:匯出上億表資料的時候速度提升明顯啊.^_^太爽了.大愛這個東東)
注意點:
--no-locks參數
這個參數官方給出英文注釋"Do not execute the temporary shared read lock.  WARNING: This will cause inconsistent backups"也就是會導致備份不一致.
在匯出myisam表時有表鎖.所以先處理myisam表,記錄myisam表個數,並在myisam表都處理完畢後,要立即解鎖.盡量減少鎖定的時間.

Mydumper:MySQL多線程邏輯備份與恢複

MySQL備份工具mysqldump和mydumper的備份效率比較

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.