The MySQL Installer is an easy way to install and maintain your MySQL installations on Microsoft Windows.
Start -> All Programs -> MySQL -> MySQL Installer -> MySQL Installer
This launches the Maintenance window of the Installer GUI, and gives you the options:
- Add/Modify Products and Features
- Check for Updates
- Remove MySQL Products.
I had a question from someone who wanted to automate a process in an environment where they tear down and reinstall MySQL to test deployments. The GUI-based way to change or remove MySQL is manual and requires human intervention, and they wanted to automate as much as possible, and thereby install and uninstall MySQL repeatedly and automatically, from a script run at the command prompt.
This is quite easy to script if you've got the MSI file you used to install MySQL, as documented here. I thought I'd go a bit further into it and explore the mechanism a little more.
Control Panel and the Registry
Before we get into the how of unattended uninstallations, the Windows Control Panel also lets you uninstall from the Programs and Features applet (which used to be called Add/Remove Programs). For MySQL, this option offers similar options to the Installer.
This mechanism (which isn't specific to MySQL) relies on a centralised repository of change/uninstall commands.
So, after a bit of investigation, I found the location in the registry:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{26FFE68D-7BD4-472A-9AB8-3517B6BF9E51}
In that location, the UninstallString key contains the following text:
MsiExec.exe /I{26FFE68D-7BD4-472A-9AB8-3517B6BF9E51}
You might recognise part of that statement from the article Automating MySQL Installation linked to above. Running that MsiExec.exe command launches the uninstall GUI, just as if you'd clicked it from Control Panel.
The GUID (the value between brackets) is correct on my machine, but differs from version to version. You can find the correct value by searching HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ in the Windows Registry for “MySQL Server” and finding the GUID that the Windows Installer uses for the program.
(Note: The Registry is a dangerous place to play around in, even for experienced people, so you should be exceptionally careful in the registry and using MsiExec.)
Tweaking MsiExec.exe
Now, to automate it, there are options to MsiExec.exe that help:
- /X bypasses the front screen and goes straight to the uninstall process
- /quiet disables the GUI entirely and automates the uninstall (if possible, which it is in MySQL's case)
- /norestart disables the post-uninstall reboot that is needed in some cases
- /L enables logging, with various options (the following example uses /L*V to log in verbose fashion)
Performing an Unattended Uninstall
Bringing all of this together, you can do the following. In a command-line started with the “Run as Administrator” option, type the following (all on one line):
MsiExec.exe /x {26FFE68D-7BD4-472A-9AB8-3517B6BF9E51} /quiet /norestart
This performs a quiet (no-gui, i.e. unattended) uninstall, without a reboot, logging in verbose format to the file C:\MySQL-uninstall.log.
Follow a similar technique to automate the uninstallation of other components that the MySQL Installer installs, or indeed of any well-behaved Windows application.