📜 ⬆️ ⬇️

Fix MySQL dump (in less than 30 lines)

A long time ago (it seems last Wednesday) I got into the hands of a dump of the MySQL database, which should be immediately deployed on my machine. Why this was needed and where the dump came from, I will not tell, it is hardly interesting to anyone. It is important that the dump was from MySQL 4.1.22 and was removed using one well-known tool (version 5.23).
He decisively refused to turn around with me ...


... Error occured at:2016-02-12 10:28:24 Line no.:65 Error Code: 2013 - Lost connection to MySQL server during query 

The error, as usual, was rather indistinct, but since it was preceded by a long (too long) SQL query, its reason was obvious. In order to reduce the number of inserts (undoubtedly, a good undertaking), SQLyog “sticks together” them, approximately as follows:

 insert into abc(a, b, c) values (1,2,3),(4,5,6),(7,8,9),... 

When there is a lot of data, the resulting giant request is broken into pieces, but somehow it turns out that the size of these pieces (~ 1 megabyte) still remains too large. I didn’t find a control handle for SQLyog to make the captured dump more sane (I might have looked badly), and in any case, the original dump (the one in question in this article) was not shot by me and I had something to do with it. do something In general, the fairy tale has a long effect, but the work itself is done much faster:
')
 while (<>) { chomp; if (length($_) < 4096) { print "$_\n"; } else { if (/^(insert.*values\s+)\((.*)\);$/) { my $hd = $1; my $tx = ''; my @bd = split(/\),\(/, $2); foreach $st (@bd) { if ($tx) { $tx .= ',(' . $st . ')'; } else { $tx = $hd . '(' . $st . ')'; } if (length($tx) > 4096) { print "$tx;\n"; $tx = ''; } } if ($tx) { print "$tx;\n"; } } } } 

Favorite Perl didn't let me down this time either. Perhaps the code could be written and somehow more elegant, but I'm quite happy with how it works. Long inserts are broken into pieces of about 4 kilobytes in size (maybe more, I have not experimented). Who needs it - take it and use it.

Source: https://habr.com/ru/post/277101/


All Articles