我有一个大型数据库,我正在复制到从属服务器。尝试通过常规mysqldump导入它(大约15GB)花了2天时间失败了。所以我正在尝试mysqldump --tab技巧。
我还想使用--use-threads导入 - 但它似乎不是一次执行多个表。有什么方法可以判断它是否正常工作?
mysqldump --single-transaction --quick --hex-blob --master-data=1 --tab=/tmp/backup/ apm
奴隶上的:
cat *.sql | mysql apm
mysqlimport --lock-tables --use-threads=4 apm /tmp/backup/*.txt
另外:知道如何禁用binlog而不编辑conf文件并重新启动服务器?看起来有点愚蠢&缓慢,mysql也将所有数据再次复制到binlog中。
答案 0 :(得分:2)
您使用的是MySQL 5.1.7 or later吗?
如果你想测试事情是否真的按预期进行,为什么不使用测试模式而只使用sample of data以便它运行得更快?
<强>更新强> 关于--use-threads是否正常工作,我不确定是否有办法明确检查。但是,我在一些测试中看不到任何真正的差异,我刚刚运行了~130M的数据:
mysqlimport --lock-tables --debug-info --use-threads=2 test /tmp/test/*.txt
Records: 2076063 Deleted: 0 Skipped: 0 Warnings: 0
User time 0.02, System time 0.08
Maximum resident set size 3884, Integral resident set size 0
Non-physical pagefaults 737, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 1340, Involuntary context switches 17
----
mysqlimport --lock-tables --debug-info --use-threads=4 test /tmp/test/*.txt
Records: 2076063 Deleted: 0 Skipped: 0 Warnings: 0
User time 0.03, System time 0.09
Maximum resident set size 3884, Integral resident set size 0
Non-physical pagefaults 738, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 1343, Involuntary context switches 41
----
mysqlimport --lock-tables --debug-info --use-threads=8 test /tmp/test/*.txt
Records: 2076063 Deleted: 0 Skipped: 0 Warnings: 0
User time 0.02, System time 0.09
Maximum resident set size 3884, Integral resident set size 0
Non-physical pagefaults 738, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 1341, Involuntary context switches 30
答案 1 :(得分:2)
事实上,当您指定要导入的多个文件(进入多个表)时,线程似乎只会启动 - 它对单个大文件没有帮助。
判断它是否实际线程的方法是查看SHOW PROCESSLIST输出。在这里你可以看到它实际上工作,虽然如上所述,在不同的表上。
mysql> show processlist;
+-------+--------+------------------+------+---------+------+-----------+------------------------------------------------------------------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-------+--------+------------------+------+---------+------+-----------+------------------------------------------------------------------------------------------------------+
| 4097 | root | 127.0.0.1:33372 | test | Query | 0 | executing | show processlist |
| 6145 | root | 10.2.13.44:44182 | test | Query | 3 | executing | LOAD DATA INFILE 'qpcrecpls2.tsv' INTO TABLE `qpcrecpls2` IGNORE 0 LINES |
| 7169 | root | 10.2.13.44:44186 | test | Query | 3 | executing | LOAD DATA INFILE 'qpcrecpls1.tsv' INTO TABLE `qpcrecpls1` IGNORE 0 LINES |
| 8193 | root | 10.2.13.44:44184 | test | Query | 3 | executing | LOAD DATA INFILE 'qpcrecpls3.tsv' INTO TABLE `qpcrecpls3` IGNORE 0 LINES |
| 9217 | root | 10.2.13.44:44188 | test | Query | 3 | executing | LOAD DATA INFILE 'qpcrecpls4.tsv' INTO TABLE `qpcrecpls4` IGNORE 0 LINES |
| 10241 | root | 10.2.13.44:44183 | test | Query | 3 | executing | LOAD DATA INFILE 'qpcrecpls5.tsv' INTO TABLE `qpcrecpls5` IGNORE 0 LINES |
| 11265 | root | 10.2.13.44:44185 | test | Query | 3 | executing | LOAD DATA INFILE 'qpcrecpls.tsv' INTO TABLE `qpcrecpls` IGNORE 0 LINES |
| 12289 | root | 10.2.13.44:44189 | test | Query | 3 | executing | LOAD DATA INFILE 'qpcrecpls6.tsv' INTO TABLE `qpcrecpls6` IGNORE 0 LINES |
| 13313 | root | 10.2.13.44:44190 | test | Query | 3 | executing | LOAD DATA INFILE 'qpcrecpls7.tsv' INTO TABLE `qpcrecpls7` IGNORE 0 LINES |
+-------+--------+------------------+------+---------+------+-----------+------------------------------------------------------------------------------------------------------+
- 详细输出也很有启发性。
答案 2 :(得分:2)
与@ bill-karwin一样,我也使用mydumper / myloader
在Ubuntu上:
sudo apt-get install mydumper
在其他任何方面,请按照以下说明操作: http://centminmod.com/mydumper.html
然后,备份:
mydumper -h [host] -u [user] -p [pass] -P [port] -B [database] -c -C --regex '^(?!excluded_table_1|excluded_table_2)' -v 3
然后,恢复:
myloader -d [dir_created_by_mydumper] -h [host] -u [user] -p [pass] -P [port] -o -C -v 3
注意:
-C
标志压缩MySQL协议,仅用于外部托管的数据库-c
使用gzip压缩,最好随时离开-v
3给出了详细的输出(这样你就可以看到发生了什么),如果你把它关掉,脚本会运行得更快。--regex
可以是任何有效的正则表达式。~/.my.cnf
文件,您不再需要主机,用户,通行证和端口。 mydumper --help
和myloader --hel
p将为您提供完整的选项列表答案 3 :(得分:1)
通过略读源代码,我认为你应该禁用--lock-tables
选项,见下文:
#ifdef HAVE_LIBPTHREAD
if (opt_use_threads && !lock_tables)
{
pthread_t mainthread; /* Thread descriptor */
pthread_attr_t attr; /* Thread attributes */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,
PTHREAD_CREATE_DETACHED);
VOID(pthread_mutex_init(&counter_mutex, NULL));
VOID(pthread_cond_init(&count_threshhold, NULL));
for (counter= 0; *argv != NULL; argv++) /* Loop through tables */
{
pthread_mutex_lock(&counter_mutex);
while (counter == opt_use_threads)
{
struct timespec abstime;
set_timespec(abstime, 3);
pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
}
/* Before exiting the lock we set ourselves up for the next thread */
counter++;
pthread_mutex_unlock(&counter_mutex);
/* now create the thread */
if (pthread_create(&mainthread, &attr, worker_thread,
(void *)*argv) != 0)
{
pthread_mutex_lock(&counter_mutex);
counter--;
pthread_mutex_unlock(&counter_mutex);
fprintf(stderr,"%s: Could not create thread\n",
my_progname);
}
}
/*
We loop until we know that all children have cleaned up.
*/
pthread_mutex_lock(&counter_mutex);
while (counter)
{
struct timespec abstime;
set_timespec(abstime, 3);
pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
}
pthread_mutex_unlock(&counter_mutex);
VOID(pthread_mutex_destroy(&counter_mutex));
VOID(pthread_cond_destroy(&count_threshhold));
pthread_attr_destroy(&attr);
}
答案 4 :(得分:0)
我使用 mydumper 作为mysqldump / mysqlimport的替代品。
Mydumper使用多个线程以.sql格式创建逻辑备份,myloader可以使用多个线程导入它们。
唯一的缺点是你必须自己从源代码构建它,因为似乎没有可用的二进制包。我发现在CentOS上构建非常简单。