Mydumper and myloader are excellent third-party mysql database logical backup and recovery tools that use multi-threaded export and import. It makes up for the shortcomings of mysqldump's single thread. This article describes how to speed up the export and import of mydumper and myloader for your reference.
For other matters about mydumper and myloader, please refer to:
Mydumper installation and installation failure summary
Mydumper backup mysql database example
myloader restore mysql database example
1. Export and import based on MyIsam engine
a. Export and import tables without chunking
[root@GZAPP tmp]# mydumper -u inno -p xxx -B bsom -T tb_access_log -o /backup/tmp/
[root@GZAPP tmp]# ls -hltr
The exported data file is a single The file size is 2.6GB
total 2.6G
-rw-r--r-- 1 root root 1.6K Jul 24 08:51 bsom.tb_access_log-schema.sql
-rw-r--r -- 1 root root 214 Jul 24 08:52 metadata-rw-r--r-- 1 root root 2.6G Jul 24 08:52 bsom.tb_access_log.sql
Import based on the default number of threads, and set the number of queries per transaction to 10,000. This parameter actually has little effect at this time, because the table is myisam engine
[root@GZAPP tmp]# myloader -u inno -p xxx -B tempdb -d /backup/tmp -v 3 -q 10000
[root@GZAPP tmp]# myloader -u inno -p xxx -B tempdb -d /backup/tmp -v 3 -q 10000
** Message: 4 threads created
** Message: Creating table `tempdb`.`tb_access_log`
** Message: Thread 4 shutting down
** Message: Thread 1 restoring `bsom`.`tb_access_log` part 0
** Message: Thread 3 shutting down
** Message: Thread 2 shutting down
root@localhost[tempdb
]> show processlist;
+--------- +---------+----------+--------+---------+--------+ ---------+---------------------------------------- --------+
| Id | User | Host | db | Command | Time | State | Info |
+---------+------- --+----------+--------+---------+--------+-------- -+------------------------------------------------ +
| 4452079 | root | localhost| tempdb | Query | 0 | init | show processlist |
| 4453793 | inno | localhost| tempdb | Sleep | 420 | | NULL | | 4453794 | inno | localhost | tempdb | Query | 4 | update | INSERT INTO `tb_access_log` VALUES (506873,"325|
+---------+----------+----- -----+--------+---------+--------+---------+------ ------------------------------------------+
From the number of threads above, we can see that only a single thread is performing the insert operation
b, table partitioning Export and import
In the following example, 500MB is used for partitioning
[root@GZAPP tmp]# mydumper -u inno -p xxx -B bsom -T tb_access_log -F 500 -o /backup/tmp/ [root@GZAPP tmp]# ls -hltr total 2.6G -rw-r--r-- 1 root root 1.6K Jul 24 08:21 bsom.tb_access_log-schema.sql -rw-r--r-- 1 root root 478M Jul 24 08:21 bsom.tb_access_log.00001.sql -rw-r--r-- 1 root root 478M Jul 24 08:21 bsom.tb_access_log.00002.sql -rw-r--r-- 1 root root 478M Jul 24 08:21 bsom.tb_access_log.00003.sql -rw-r--r-- 1 root root 478M Jul 24 08:21 bsom.tb_access_log.00004.sql -rw-r--r-- 1 root root 478M Jul 24 08:22 bsom.tb_access_log.00005.sql -rw-r--r-- 1 root root 214 Jul 24 08:22 metadata -rw-r--r-- 1 root root 241M Jul 24 08:22 bsom.tb_access_log.00006.sql
It can be seen from the above that the large table tb_access_log is divided into nearly 500M Multiple files
[root@GZAPP tmp]# myloader -u inno -p xxx -B tempdb -t 6 -d /backup/tmp -v 3 ** Message: 6 threads created ** Message: Creating database `tempdb` ** Message: Creating table `tempdb`.`tb_access_log` ** Message: Thread 1 restoring `bsom`.`tb_access_log` part 3 ** Message: Thread 2 restoring `bsom`.`tb_access_log` part 5 ** Message: Thread 5 restoring `bsom`.`tb_access_log` part 4 ** Message: Thread 3 restoring `bsom`.`tb_access_log` part 6 ** Message: Thread 4 restoring `bsom`.`tb_access_log` part 1 ** Message: Thread 6 restoring `bsom`.`tb_access_log` part 2###############
#在下面的processlist可以看到,存在表级锁等待
+---------+-------+-----------+---------+---------+--------+-----------------------------+------------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+---------+-------+-----------+---------+---------+--------+-----------------------------+------------------------------------------------+
| 4452079 | root | localhost | bsom | Query | 0 | init | show processlist |
| 4452167 | inno | localhost | tempdb | Sleep | 769 | | NULL |
| 4452168 | inno | localhost | tempdb | Query | 36 | update | INSERT INTO `tb_access_log` VALUES (6367402,"0,|
| 4452169 | inno | localhost | tempdb | Query | 21 | Waiting for table level lock| INSERT INTO `tb_access_log` VALUES (12593865," |
| 4452170 | inno | localhost | tempdb | Query | 26 | Waiting for table level lock| INSERT INTO `tb_access_log` VALUES (15643029,""|
| 4452171 | inno | localhost | tempdb | Query | 6 | Waiting for table level lock| INSERT INTO `tb_access_log` VALUES (173947,"70 |
| 4452172 | inno | localhost | tempdb | Query | 15 | Waiting for table level lock| INSERT INTO `tb_access_log` VALUES (9490507,"7 |
| 4452173 | inno | localhost | tempdb | Query | 30 | Waiting for table level lock| INSERT INTO `tb_access_log` VALUES (3271602,"4 |
+---------+---------+-----------+---------+---------+--------+-----------------------------+----------------------------------------------+
c、调整myisam有关参数后导入
[root@GZ-APP-BAK01 tmp]# time myloader -u innobk -p InnoBK -B tempdb -t 6 -d /backup/tmp -v 3 ** Message: 6 threads created ** Message: Creating table `tempdb`.`tb_mobile_access_log` ** Message: Thread 1 restoring `blossom`.`tb_mobile_access_log` part 3 ** Message: Thread 6 restoring `blossom`.`tb_mobile_access_log` part 6 ** Message: Thread 2 restoring `blossom`.`tb_mobile_access_log` part 5 ** Message: Thread 3 restoring `blossom`.`tb_mobile_access_log` part 4 ** Message: Thread 4 restoring `blossom`.`tb_mobile_access_log` part 1 ** Message: Thread 5 restoring `blossom`.`tb_mobile_access_log` part 2 ** Message: Thread 6 shutting down ** Message: Thread 5 shutting down ** Message: Thread 1 shutting down ** Message: Thread 2 shutting down ** Message: Thread 4 shutting down ** Message: Thread 3 shutting down
real 266m28.903s
user 0m6.008s
sys 0m1.681s
###调整以下相关参数,后尝试再次导入,
concurrent_insert AUTO 改成 ALWAYS
bulk_insert_buffer_size 8388608 改成 256M
myisam_sort_buffer_size 67108864 改成 128M
[root@GZ-APP-BAK01 tmp]# time myloader -u innobk -p InnoBK -B tempdb -t 6 -o -d /backup/tmp -v 3 ** Message: 6 threads created ** Message: Dropping table (if exists) `tempdb`.`tb_mobile_access_log` ** Message: Creating table `tempdb`.`tb_mobile_access_log` ** Message: Thread 1 restoring `blossom`.`tb_mobile_access_log` part 3 ** Message: Thread 2 restoring `blossom`.`tb_mobile_access_log` part 6 ** Message: Thread 3 restoring `blossom`.`tb_mobile_access_log` part 5 ** Message: Thread 4 restoring `blossom`.`tb_mobile_access_log` part 4 ** Message: Thread 6 restoring `blossom`.`tb_mobile_access_log` part 1 ** Message: Thread 5 restoring `blossom`.`tb_mobile_access_log` part 2 ** Message: Thread 2 shutting down ** Message: Thread 1 shutting down ** Message: Thread 6 shutting down ** Message: Thread 5 shutting down ** Message: Thread 3 shutting down ** Message: Thread 4 shutting down
real 253m42.460s ###此时导入时间并无明显减少
user 0m5.924s
sys 0m1.637s
2、基于innodb引擎的导出导入
a、表未分块导出,数据文件大小为3.9GB
[root@GZAPP tmp]# ls -hltr total 3.9G -rw-r--r-- 1 root root 1.8K Jul 24 00:09 bscom.tb_message-schema.sql -rw-r--r-- 1 root root 3.9G Jul 24 00:25 bscom.tb_message.sql -rw-r--r-- 1 root root 215 Jul 24 09:14 metadata
###下面使用6个线程导入,实际上可以看到,只有1个线程在工作,因为数据文件只有1个
[root@GZAPP tmp]# myloader -u inno -p xxx -B tempdb -t 6 -d /backup/tmp -v 3 ** Message: 6 threads created ** Message: Creating table `tempdb`.`tb_message` ** Message: Thread 1 restoring `bscom`.`tb_message` part 0 ** Message: Thread 5 shutting down ** Message: Thread 2 shutting down ** Message: Thread 6 shutting down ** Message: Thread 3 shutting down ** Message: Thread 4 shutting down
b、表分块导出
[root@GZAPP tmp]# mydumper -u inno -p xxx -B bscom -T tb_message -F 500 -o /backup/tmp/ [root@GZAPP tmp]# ls -hltr total 3.9G -rw-r--r-- 1 root root 1.8K Jul 24 09:55 bscom.tb_message-schema.sql -rw-r--r-- 1 root root 478M Jul 24 09:55 bscom.tb_message.00001.sql -rw-r--r-- 1 root root 478M Jul 24 09:55 bscom.tb_message.00002.sql -rw-r--r-- 1 root root 478M Jul 24 09:55 bscom.tb_message.00003.sql -rw-r--r-- 1 root root 478M Jul 24 09:55 bscom.tb_message.00004.sql -rw-r--r-- 1 root root 478M Jul 24 09:55 bscom.tb_message.00005.sql -rw-r--r-- 1 root root 478M Jul 24 09:55 bscom.tb_message.00006.sql -rw-r--r-- 1 root root 478M Jul 24 09:55 bscom.tb_message.00007.sql -rw-r--r-- 1 root root 481M Jul 24 09:55 bscom.tb_message.00008.sql -rw-r--r-- 1 root root 135 Jul 24 09:55 metadata -rw-r--r-- 1 root root 93M Jul 24 09:55 bscom.tb_message.00009.sql
###下面尝试使用6线程导入,可以看到有6个线程在并发导入
[root@GZAPP tmp]# myloader -u inno -p xxx -B tempdb -t 6 -d /backup/tmp/ -v 3 ** Message: 6 threads created ** Message: Creating database `tempdb` ** Message: Creating table `tempdb`.`tb_message` ** Message: Thread 2 restoring `bscom`.`tb_message` part 5 ** Message: Thread 1 restoring `bscom`.`tb_message` part 9 ** Message: Thread 3 restoring `bscom`.`tb_message` part 1 ** Message: Thread 4 restoring `bscom`.`tb_message` part 8 ** Message: Thread 5 restoring `bscom`.`tb_message` part 4 ** Message: Thread 6 restoring `bscom`.`tb_message` part 6 ** Message: Thread 1 restoring `bscom`.`tb_message` part 7 ** Message: Thread 6 restoring `bscom`.`tb_message` part 3 ** Message: Thread 2 restoring `bscom`.`tb_message` part 2 ** Message: Thread 3 shutting down ** Message: Thread 5 shutting down ** Message: Thread 4 shutting down ** Message: Thread 1 shutting down ** Message: Thread 2 shutting down ** Message: Thread 6 shutting down
3、小结
a、mydumper在导出的时候可以根据服务器可用资源来合理地设置线程数。
b、mydumper在导出的时候尽可能地指定chunk-filesize或者rows参数以分块导出。
c、myloader在针对myisam引擎时建议调整相关参数至合理值以提高无法提高性能,主要是表级锁的问题。
d、myloader在针对innodb引擎时建议调整参数至合理值以提高性能,如以下参数等:
innodb_buffer_pool_size innodb_flush_log_at_trx_commit innodb_log_buffer_size
e、通过使用分块导出与导入可以显著利用并发来加快inndbo表导入。
f、注意mydumper导出时不会导出存储过程,函数,触发器等。
以上就是加快mydumper与myloader导出导入的内容,更多相关内容请关注PHP中文网(www.php.cn)!

MySQLoffersvariousstorageengines,eachsuitedfordifferentusecases:1)InnoDBisidealforapplicationsneedingACIDcomplianceandhighconcurrency,supportingtransactionsandforeignkeys.2)MyISAMisbestforread-heavyworkloads,lackingtransactionsupport.3)Memoryengineis

Common security vulnerabilities in MySQL include SQL injection, weak passwords, improper permission configuration, and unupdated software. 1. SQL injection can be prevented by using preprocessing statements. 2. Weak passwords can be avoided by forcibly using strong password strategies. 3. Improper permission configuration can be resolved through regular review and adjustment of user permissions. 4. Unupdated software can be patched by regularly checking and updating the MySQL version.

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.

To monitor the health and performance of MySQL servers, you should pay attention to system health, performance metrics and query execution. 1) Monitor system health: Use top, htop or SHOWGLOBALSTATUS commands to view CPU, memory, disk I/O and network activities. 2) Track performance indicators: monitor key indicators such as query number per second, average query time and cache hit rate. 3) Ensure query execution optimization: Enable slow query logs, record and optimize queries whose execution time exceeds the set threshold.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools
