# Splitting massive MySQL dumps

As I posted yesterday, I have a massive MySQL dump to import. I tried BigDump, but one of the tables kept producing errors and so BigDump would exit. I don't need the whole db imported, so I wrote this to split it by table. It produces a new sql file for every table it finds, numbered sequentially so if you process them in alphabetical order it's the equivalent of the whole dump. USE statements get their own files in the same sequence.

#! /usr/bin/perl use strict;use warnings;use 5.010; my <span class="katex math inline">dump_file =</span>ARGV[0];&usage() if !<span class="katex math inline">dump_file; say "using ".</span>dump_file; my (<span class="katex math inline">line,</span>table,@query, <span class="katex math inline">file_number,</span>file_name);my <span class="katex math inline">line_number = 1;my</span>find_count = 0; open(DUMP_IN, "< $dump_file"); while(<DUMP_IN>){ my <span class="katex math inline">line =</span>_; if (/^USE\s.(\w+)./){ say "changing db: ".<span class="katex math inline">1;</span>file_name = &make_file_name("USE_<span class="katex math inline">1", "</span>find_count"); &write_USE(<span class="katex math inline">file_name,</span>line); <span class="katex math inline">find_count++; }elsif (/^-- Table structure for table .(.+)./){ ## If the current line is the beginning of a table definition ## and @query is defined, then @query must be full of the previous ## table, so we want to process it now: if (@query){</span>file_name = &make_file_name("<span class="katex math inline">table", "</span>find_count"); open(OUTPUT, "><span class="katex math inline">file_name"); foreach(@query){ print OUTPUT</span>_; } close OUTPUT; undef @query; } <span class="katex math inline">table =</span>1; <span class="katex math inline">find_count++; } next unless</span>table; push @query, <span class="katex math inline">line;</span>line_number++; }close DUMP_IN;say <span class="katex math inline">line_number; ## Subroutines!sub write_USE() { my(</span>filename, <span class="katex math inline">line) = @_[0,1]; open (OUTPUT, "></span>filename"); print OUTPUT <span class="katex math inline">line; close OUTPUT;} sub make_file_name() { my (</span>type, <span class="katex math inline">number) = @_[0,1];</span>number = sprintf("%05d", <span class="katex math inline">number);</span>file_name=<span class="katex math inline">number."_".</span>type.".sql"; return <span class="katex math inline">file_name;} sub usage() { say "Error: missing arguments."; say "Usage:"; say "</span>0 [MYSQL_DUMP]"; exit 1;}  A small downside is that this replaces my 2.5Gb file with about 1800 smaller ones. A scripted importer is to follow. # Massive dumps with MySQL hurr. *insert FLUSH TABLES joke here* I have a 2.5GB sql dump to import to my MySQL server. MySQL doesn't like me giving it work to do, and the box it's running on only has 3GB of memory. So, I stumbled across bigdump, which is brilliant. It's a PHP script that splits massive SQL dumps into smaller statements, and runs them one at a time against the server. Always the way: 10 lines into duct-taping together something to do the job for you, you find that someone else has done it rather elegantly.1 In short, we extract the directory to a publicly http-accessible location, stick the sql dump there and tell it to go. In long, installation is approximately as follows: [email protected]:~<span class="katex math inline">cd www[email protected]:~/www</span> mkdir bigdump[email protected]:~/www<span class="katex math inline">chmod 777 bigdump[email protected]:~/www</span> cd bigdump/[email protected]:~/www<span class="katex math inline">wget -q http://www.ozerov.de/bigdump.zip[email protected]:~/www</span> unzip bigdump.zip[email protected]:~/www/bigdump$ lsbigdump.php  bigdump.zip

Where ~/www is my apache UserDir (i.e. when I visit http://localhost/~avi, i see the contents of ~/www). We need permissions to execute PHP scripts in this dir, too (which I have already). We also need to give everyone permissions to do everything - don't do this on the internet!2

Configuration involves editing bigdump.php with the hostname of our MySQL server, the name of the DB we want to manipulate and our credentials. The following is lines 40-45 of mine:

// Database configuration <span class="katex math inline">db_server   = 'localhost';</span>db_name     = 'KBDB';<span class="katex math inline">db_username = 'kbox';</span>db_password = 'imnottellingyou';

Finally, we need to give it a dump to process. For dumps of less than 2Mb3, we can upload through the web browser, else we need to upload or link our sql dump to the same directory as bigdump:

[email protected]:~/www/bigdump\$ ln -s /home/avi/kbox/kbox_dbdata ./dump.sql

Now, we visit the php page through a web browser, and get a pretty interface:

BigDump lists all the files in its working directory, and for any that are SQL dumps provides a 'Start Import' link. To import one of them, click the link and wait.

1. Yes, you Perl people, it's in PHP. But it's not written by me. So on balance turns out more elegant. []
2. Those permissions aside - anyone can execute whatever SQL they like with your credentials through this page. Seriously, not on the internet! []
3. Or whatever's smaller out of upload_max_filesize and post_max_size in your php.ini []