Moving MySQL tables from MyISAM to InnoDB

One of the greatest things ever to happen to MySQL was the InnoDB engine. Before InnoDB, indexes would get corrupted, updates meant table locks, not just row locks, and we had no support for transactions. Since the advent of InnoDB however, we've come a long way. These days, most serious DBAs using MySQL build exclusively on the InnoDB engine.

[Related] Profiling MySQL Queries for Better Performance

Sadly though, many sites are still using the MyISAM engine. Some are hosted on shared hosting servers and some just don't have a proper DBA to look after their databases. For whatever the reason, these sites are missing out on the performance and stability gains that the rest of us take for granted. At Pantheon, we know there are a lot of these sites out there because we see them when they migrate their sites onto our platform. As part of our Launch Check, we check the engine type on every table. If we find a table using the MyISAM engine, we notify the user so they can fix it.

Fixing this problem is simple to any developer who understands a little SQL. However, for non-developers, this can be a daunting task. So I have put together a little PHP script to help you convert your MyISAM tables to InnoDB.

NOTE: This is an unofficial script. It is not supported by Pantheon. Customer Support will not help you run it, nor will they hold your hand while fixing things if this script screws up your database. Use THIS SCRIPT AT YOUR OWN RISK. I strongly suggest you make a backup of your database before running this script. 


The Easy Way

Still with me? Cool! Let's dive right in. 

There are 2 versions of this script. One is meant to be called from a web browser. It is meant for Pantheon customers. If you are a Pantheon customer, save the code in a file with the extension .php and sftp it up to your site. Place it in the code directory of your website.

Now, point a browser to your newly created script that should be in the root directory of your Dev environment.

That is all there is to it. The script will do all the work. You don't need to change anything.

Here is the browser version of the script:



The Developer Way

If you are not a Pantheon customer, or you want to run the script from the command line, use the one below. To use this one however, you need to know two things before you begin.

  1. You have to have PHP installed on the computer you want to run this on. I run this on my laptop. I have PHP installed on my laptop. I know that because I can type php -v at the command line and I get a proper response. If you do not understand any of that, this script is not for you, use the one above.

  2. If you are a Pantheon customer, you can get your database connection info from your site's dashboard. Make sure you get the information for your Dev environment. After you've run the script, and everything looks good, you can easily migrate it up the line to Test and then Live. If you are running this on your site and your site is not on Pantheon, you will have to find another way to get your MySQL connection info.

Here is the command line script:


See the line that says "* DO NOT CHANGE ANYTHING BELOW THIS LINE"? I'm serious about that. If it doesn't' work and you are not a programmer, don't mess with it. If you are a programmer, you realize exactly how dead-simple the script is.

Here are the parameters you will need to configure before running the script.

  • host = This is the name of the machine your db is running on. If you are a Pantheon customer localhost is WRONG. Get the correct host and paste it in there replacing localhost. 

  • port = This is the port that is running MySQL on your computer. Again, if you are a Pantheon customer, we give you this information. If you are not, 3306 is the standard port for MySQL. 

  • user = This is the user name you use to connect to MySQL with. 

  • password = This is your MySQL password for the user you specified in the line above.

  • database = This is the name of the database that contains the tables. If you are a pantheon customer, this is "pantheon". If you are not a Pantheon customer, you will need to get this from your host.

Now, save the file.

Then from a command window execute the program. (How you get a command window varies by OS. If you don't know, ask your computer friend, younger sibling, or niece.)

The script will tell you everything it is doing. It has safeties built in to keep it from changing anything but MyISAM tables. It will look at every table in your database and if the engine is MyISAM, it will change it to an InnoDB.

Once you have run it successfully, check everything! 

This is not a particularly dangerous script. The change is pretty simple. However, it's your data. So be careful. 

Topics Development