用以下语句:

mysqldump --complete-insert --lock-all-tables --no-create-db 
--no-create-info --extended-insert --password=XXX -u XXX 
--dump-date yyy > yyy_dataOnly.sql

我得到如下INSERT语句:

INSERT INTO `table` VALUES (1,'something'),(2,'anything'),(3,'everything');

在我的案例中,我需要的是这样的东西:

INSERT INTO `table` VALUES (1,'something');
INSERT INTO `table` VALUES (2,'anything');
INSERT INTO `table` VALUES (3,'everything');

有没有一种方法告诉“mysqldump”为每一行创建一个新的INSERT语句? 谢谢你的帮助!


当前回答

在更新的版本中,对标志进行了更改:

从文档中可以看到:

——extended-insert - e 使用包含多个VALUES列表的多行语法编写INSERT语句。这将导致转储文件更小,并在重新加载文件时加快插入速度。

--opt This option, enabled by default, is shorthand for the combination of --add-drop-table --add-locks --create-options --disable-keys --extended-insert --lock-tables --quick --set-charset. It gives a fast dump operation and produces a dump file that can be reloaded into a MySQL server quickly. Because the --opt option is enabled by default, you only specify its converse, the --skip-opt to turn off several default settings. See the discussion of mysqldump option groups for information about selectively enabling or disabling a subset of the options affected by --opt.

——skip-extended-insert 关闭扩展插入

其他回答

Use:

mysqldump --extended-insert=FALSE 

注意,多个插入比一个大插入慢。

在更新的版本中,对标志进行了更改:

从文档中可以看到:

——extended-insert - e 使用包含多个VALUES列表的多行语法编写INSERT语句。这将导致转储文件更小,并在重新加载文件时加快插入速度。

--opt This option, enabled by default, is shorthand for the combination of --add-drop-table --add-locks --create-options --disable-keys --extended-insert --lock-tables --quick --set-charset. It gives a fast dump operation and produces a dump file that can be reloaded into a MySQL server quickly. Because the --opt option is enabled by default, you only specify its converse, the --skip-opt to turn off several default settings. See the discussion of mysqldump option groups for information about selectively enabling or disabling a subset of the options affected by --opt.

——skip-extended-insert 关闭扩展插入

对于不想跳过-extended-insert的人来说 //导致(每个值一个插入句)性能损失,当以后执行/import sql。

但是想要保持SQL的可读性(避免值在一个长行中) 这可能是功 //没有找到正式的解决方案,只是使用sed格式化SQL

注意:这是不安全的,如果),(在原始数据。

使用sed


mysqldump ... | sed 's/),(/),\n  (/g'

//在第一个项目之前没有换行符。


mysqldump ... | sed -e 's/),(/),\n  (/g' -e 's/VALUES (/VALUES\n  (/g'

//在第一个项目前添加换行符。

输出

0. 原:

INSERT INTO `role_permission` VALUES (1328,106,1),(1329,171,1),(1330,144,1),(1331,147,1),(1333,157,1),(1334,88,1);

1. 成为:

INSERT INTO `role_permission` VALUES (1328,106,1),
  (1329,171,1),
  (1330,144,1),
  (1331,147,1),
  (1333,157,1),
  (1334,88,1);
INSERT INTO `role_permission` VALUES
  (1328,106,1),
  (1329,171,1),
  (1330,144,1),
  (1331,147,1),
  (1333,157,1),
  (1334,88,1);


http://blog.lavoie.sl/2014/06/split-mysqldump-extended-inserts.html

https://stackoverflow.com/q/15750535#answer-19961480 答案来自这里,我没有做完整的测试

对于MariaDB,您将使用

mysqldump -u root -p'secret' --skip-extended-insert dbnamehere > y.sql

几年前