209

with the following statement:

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

I get INSERT statements like the following:

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

What I need in my case is something like this:

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

Is there a way to tell "mysqldump" to create a new INSERT statement for each row? Thanks for your help!

4 Answers 4

329

Use:

mysqldump --extended-insert=FALSE 

Be aware that multiple inserts will be slower than one big insert.

9
  • 81
    --skip-extended-insert appears the be the correct syntax for me (using mysqldump version 10.13) May 22, 2013 at 20:17
  • 22
    Slower, yes, but a lot of text editors have trouble with hugely long lines of text and if the tables have a huge amount of data, that is what will happen. Sep 17, 2013 at 12:16
  • 2
    mysqldump --opt --skip-extended-insert or mysqldump --opt --extended-insert=FALSE work Nov 3, 2016 at 0:27
  • 1
    OMG! I migrated to a new version which caused this issue and a lot of my automated scripts to fail. I thought I was screwed but this seems to be like an easy fix. Thanks for sharing! Sep 12, 2017 at 14:43
  • 20
    This is nice because now I can do a git diff on two different database dumps and have a clear picture of what was changed.
    – Rolf
    Jun 14, 2018 at 7:54
21

In newer versions change was made to the flags:

from the documentation:

--extended-insert, -e

Write INSERT statements using multiple-row syntax that includes several VALUES lists. This results in a smaller dump file and speeds up inserts when the file is reloaded.

--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

Turn off extended-insert

1

For MariaDB you would use

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

MaraiDB mysqldump Docs

1
-1

for people who don't want to --skip-extended-insert
// cause (each record has one insert sentence) performance loss when later execute/import sql.

but want keep the sql readability (avoid records in one long line) this may be work
// didn't find offical solution, just use sed to format the sql

NOTE: It's not safe if ),( in raw data.
TODO: handle ),( in row data

use sed


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

// no newline before first item.


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

// add newline before first item.

outputs

0. original:

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

1. become:

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
Solution came from here, I didn't do fully test

6
  • The problem with this is that it will break for strings that happen to contain ),(
    – Flimm
    Dec 24, 2022 at 14:42
  • Useful for diff'ing SQL dumps, but not good for dumps where you need to trust the data. (If you are just diffing, you might as well just remove each ),( and replace with a single linebreak.
    – mwfearnley
    Jan 10 at 10:45
  • yes, you were right, it's not safe for ),(, I wonder is there a easy way to escape or protect it
    – yurenchen
    Jan 15 at 16:34
  • This does not solve anything, as far as "performance loss" is involved.
    – Luuk
    Jan 15 at 16:47
  • @Luuk this solution avoid split into one value each insert (which generate too many inset sentence that slow down when later execute import them) by use multi-lines values split by sed. but not process special string safely
    – yurenchen
    Jan 15 at 16:54

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.