MySQL导出的SQL语句在导入时有可能会非常非常慢,在处理百万级数据的时候,可能导入要花几小时。在导出时合理使用几个参数,可以大大加快导 入的速度。

-e 使用包括几个VALUES列表的多行INSERT语法;
--max_allowed_packet=XXX 客户端/服务器之间通信的缓存区的最大大小;
--net_buffer_length=XXX TCP/IP和套接字通信缓冲区大小,创建长度达net_buffer_length的行。

注意:max_allowed_packet 和 net_buffer_length 不能比目标数据库的设定数值 大,否则可能出错。

首先确定目标数据库的参数值

mysql> show variables like 'max_allowed_packet';
mysql> show variables like 'net_buffer_length';
根据参数值书写 mysqldump 命令,如:

# mysqldump -uroot -p dbhost -e --max_allowed_packet=16777216 --net_buffer_length=16384 > dbhost.sql
OK,现在速度就很快了,主要注意的是导入和导出端的 max_allowed_packet 和 net_buffer_length 这2个参数值设定,弄大点就OK了

其实,最快的方法,是直接COPY数据库目录,不过记得先停止 MySQL 服务。


测试导出
bblaishu数据库大小740M
mysql> show variables like 'net_buffer_length';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| net_buffer_length | 16384 |
+-------------------+-------+
1 row in set (0.00 sec)

mysql> show variables like 'max_allowed_packet';
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 1047552 |
+--------------------+---------+
1 row in set (0.00 sec)


time mysqldump -uroot -p`cat /data/save/mysql_root` -T/data/test/test bblaishu -e --max_allowed_packet=1047552 --net_buffer_length=16384

real    0m14.183s
user    0m0.005s
sys     0m0.003s


mysql> show variables like 'net_buffer_length';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| net_buffer_length | 65536 |
+-------------------+-------+
1 row in set (0.00 sec)

mysql> show variables like 'max_allowed_packet';
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 2096128 |
+--------------------+---------+
1 row in set (0.00 sec)

time mysqldump -uroot -p`cat /data/save/mysql_root` -T/data/test/test bblaishu -e --max_allowed_packet=2096128 --net_buffer_length=65536

real    1m8.397s
user    0m0.007s
sys     0m0.008s


mysql> show variables like 'max_allowed_packet';
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 2096128 |
+--------------------+---------+
1 row in set (0.01 sec)

mysql> show variables like 'net_buffer_length';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| net_buffer_length | 16384 |
+-------------------+-------+
1 row in set (0.00 sec)
time mysqldump -uroot -p`cat /data/save/mysql_root` -T/data/test/test bblaishu -e --max_allowed_packet=2096128 --net_buffer_length=16384

real    0m12.997s
user    0m0.004s
sys     0m0.007s

结论:在将数据库导出的时候net_buffer_length默认就好,max_allowed_packet建议设置大一点,测试设置2M比较合适


测试导入

mysql> show variables like 'net_buffer_length';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| net_buffer_length | 16384 |
+-------------------+-------+
1 row in set (0.00 sec)

mysql> show variables like 'max_allowed_packet';
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 1047552 |
+--------------------+---------+
1 row in set (0.00 sec)

time mysqlimport --local -l -uroot -p`cat /data/save/mysql_root` bblaishu `find ./ -name "*.txt"`

real    1m31.587s
user    0m0.115s
sys     0m0.409s

 

mysql> show variables like 'max_allowed_packet';
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 2096128 |
+--------------------+---------+
1 row in set (0.00 sec)

mysql> show variables like 'net_buffer_length';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| net_buffer_length | 65536 |
+-------------------+-------+
1 row in set (0.00 sec)
time mysqlimport --local -l -uroot -p`cat /data/save/mysql_root` bblaishu `find ./ -name "*.txt"`

real    1m31.524s
user    0m0.122s
sys     0m0.355s


mysql> show variables like 'max_allowed_packet';
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 2096128 |
+--------------------+---------+
1 row in set (0.00 sec)

mysql> show variables like 'net_buffer_length';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| net_buffer_length | 16384 |
+-------------------+-------+
1 row in set (0.00 sec)

time mysqlimport --local -l -uroot -p`cat /data/save/mysql_root` bblaishu `find ./ -name "*.txt"`
real    1m32.959s
user    0m0.123s
sys     0m0.429s

结论:在导入的时候同样net_buffer_length默认就好,max_allowed_packet建议设置为2M