When I upgraded my WordPress installation recently, I ran into a chracter encoding problem. Long story short, it turns out that older WordPress installations like mine tend to have been created in latin1, but the data is actually being saved in UTF8. If you update your wp-config file to a newer version, it adds a DB_CHARSET option, which will cause your site to puke, because the database character set doesn’t match the data that’s actually stored in it.
Thankfully, the fix is relatively simple, if a bit of a hassle:
- From your command line, use the
mysqldumpcommand to export your database inlatin1format. Since MySQL sees your database is already inlatin1format, it won’t re-encode it (which would break theUTF8data in the database). The command should look something like this:mysqldump -u username -p --add-drop-table --default-character-set=latin1 databasename > databasename.sql
Note: You must do this from the command line, because PHPMyAdmin doesn’t allow you to specify the characterset of the export file, so you will end up with re-encoded data, that will get scrambled and kill your database.
- Copy that dump file somewhere safe for a backup, in case something goes wrong.
- Using a text editor, open the MySQL dump file and replace all instances of
latin1withutf8. There should be one reference in eachCREATE TABLEline. - Import the database over the top of your existing one. Since you did a complete dump, with the
add-drop-tableoption, this will drop all your existing tables, and recreate them. And since you changed the character sets, this will effectively update your database toUTF8. The command will look something like this:mysql -u username -p databasename < databasename.sql
Now, if you’re anything like me, those instructions are terrifying. But trust me that there is relatively little risk. The very first thing you’re going to do is make a backup. If anything goes wrong, the worst case scenario is that you restore your backup, and you’re back to square one. I can’t tell you that it’s totally safe, but I can assure you that I did this to two of my databases, and it went off without a hitch.