mysqldump all databases from XAMPP on Windows

A quick note to hopefully save others the time I’ve just wasted. If you want to dump out all your databases, perhaps in preparation for an upgrade to XAMPP as I’m about to try:

  1. Open up a command prompt (in Win 8+ type ‘Command’ and press <Enter> from the Start screen);
  2. Type:
    cd\xampp\mysql\bin

    (or wherever your myslq.exe is located)

  3. Type:
    mysqldump --user=root --password=myrootpwd --all-databases > backup.sql

Other options for this command can be found on the MySQL site.

Note that you are doing this from the Windows Command prompt – don’t get distracted, as I was, by running the MySQL command line tool. If you try and run mysqldump from the:

mysql>

prompt, you’ll get various syntax errors such as:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysqldump...' at line 1

However, if running the MySQL command line tool is what has brought you to this post, you launch this by following steps 1 & 2 above and then typing:

mysql --user=root --password=myrootpwd

When executing commands at the:

mysql>

prompt, don’t forget that it allows you to enter multiple lines at once and that clicking <Enter> will just give you a new line unless you finish your line with a semi-colon – this will tell MySQl that you want to execute the command.

 

Shock News (to me anyway): Windows Command Line is actually useful

Having used Windows since my earliest memories, I’ve only rarely used the command line. I never found it that useful and was always annoyed by the fact that I couldn’t click somewhere in my command to edit it, and instead had to use the arrow keys to move it forwards and backwards.

However, I’ve recently had to carry out quite a few batch tasks in Windows and have, to my surprise, found the command line to be pretty useful. So, as much to remind myself as anything, I thought I would note down the things that have helped me.

Changing File Extensions

This is easy using the REN command, e.g. REN *.svs *.tif

FOR Loops

As you would expect, this loops over a set of files/directories and carries out a command on each. The basic structure of a FOR command is as follows:

FOR %V IN (set) DO command [command-parameters]

The %V is a variable (it does not have to be ‘v’, but must be a single letter, and it is case sensitive, i.e. %v and %V are different) that can be used in the command parameters to refer to each of the files matched by set. As a spurious example, to echo all of the files in the current working directory, you would give the following command:

FOR %V IN (*) DO echo %V

Variable Modifiers

Variable modifiers can be used to output the variable in a variety of ways, e.g. for %V above:

  • %~fV outputs the full path name for the file
  • %~nV outputs the file name only
  • %~xV outputs the file extension

There are others apart from those mentioned above – see the FOR command help for more.

FOR /D

If the /D extension is used, the FOR command will match  directories rather than files.

For example, to separately zip up all of the subdirectories in the current working directory

FOR /D %D IN (*) DO C:\zip\zip.exe -r "%~fD.zip" "%~fD"

Using FOR in batch files

If you are using a FOR command in a batch file, it is necessary to use, for example, %%V, rather than %V for the variables.

START /WAIT

I was using the START command in a batch file to carry out a certain command on multiple files, but I needed it to wait until one command had finished executing before it called the next. Thankfully there is the /WAIT parameter, which means that the next command will not be called until the previous one has finished.