{
  "id": "8a370f73d1a3",
  "url": "http://alexking.org/blog/2008/03/06/mysql-latin1-utf8-conversion",
  "title": "Alex King: Fixing a MySQL Character Encoding Mismatch",
  "note": "We ran into an interesting MySQL character encoding issue at Crowd Favorite today while working to upgrade and launch a new client site.\n\nHere is what we were trying to do: copy the production database to the staging database so we could properly configure and test everything before pushing the new site live. Pretty simple right? It was, until we noticed a bunch of weird character encoding issues on the staging site.\n\n\n\nIt turned out that while the database tables were set to a Latin-1 (latin1), the content that populated those tables was encoded as UTF-8 (utf8). A variety of attempts to fix this failed, but what succeeded was as follows:\n\n\nExport the data as Latin-1. Because MySQL knows that the table is already using a Latin-1 encoding, it will do a straight export of the data without trying to convert the data to another character set. If you try to export as UTF-8, MySQL appears to attempt to convert the (supposedly) Latin-1 data to UTF-8 - resulting in double encoded characters (since the data was actually already UTF-8).\nChange the character set in the exported data file from ‘latin1′ to ‘utf8′. Since the dumped data was not converted during the export process, it’s actually UTF-8 encoded data.\nCreate your new table as UTF-8 If your CREATE TABLE command is in your SQL dump file, change the character set from ‘latin1′ to ‘utf8′.\nImport your data normally. Since you’ve got UTF-8 encoded data in your dump file, the declared character set in the dump file is now UTF-8, and the table you’re importing into is UTF-8, everything will go smoothly.\n\nI can confirm that a half-dozen or so variations on the above do not work. This includes INSERT INTO newdb.newtable SELECT * FROM olddb.oldtable;.\n\nAlso, if you’re doing this for a WordPress1 site (like we were), keep in mind that copying over the production database will generally mean that WP-Cache is enabled. You’ll want to remember to turn that off. Yeah.  \n\n\nThis is a fairly common issue in older WordPress installs because the MySQL database default is commonly Latin-1, and older versions of WordPress did not specify the character set when creating the database tables (so they would default to Latin-1) and the default encoding in the WordPress settings is UTF-8. [back]\n\nShareThis\n",
  "voice": "excerpt",
  "saved": "2008-03-06T07:13:55Z",
  "host": "alexking.org",
  "kind": "page",
  "tags": [],
  "record": "https://links.thingelstad.xyz/links/8a370f73d1a3/",
  "markdown": "https://links.thingelstad.xyz/links/8a370f73d1a3.md",
  "issues": [],
  "connections": {
    "same_host": {
      "count": 0,
      "page": "https://links.thingelstad.xyz/hosts/alexking.org/",
      "nearest": []
    },
    "same_week": {
      "count": 30,
      "nearest": [
        {
          "id": "3853823a4b0d",
          "title": "EU To Clear Google-DoubleClick on Tuesday, Google Into Display",
          "url": "http://www.businessinsider.com/2008/3/eu-to-clear-google-doubleclick-on-tuesday--about-time-",
          "record": "https://links.thingelstad.xyz/links/3853823a4b0d/"
        },
        {
          "id": "515ccc977a96",
          "title": "Bush's HHH",
          "url": "http://www.dailykos.com/story/2008/03/05/469841/-Bush-s-HHH",
          "record": "https://links.thingelstad.xyz/links/515ccc977a96/"
        },
        {
          "id": "01be2e9e949b",
          "title": "Google Analytics Data Sharing: Why Not Go All The Way?",
          "url": "http://techcrunch.com/2008/03/05/google-analytics-data-sharing-why-not-go-all-the-way/",
          "record": "https://links.thingelstad.xyz/links/01be2e9e949b/"
        },
        {
          "id": "db98dbfb5f3c",
          "title": "One more step",
          "url": "http://googlereader.blogspot.com/2008/03/one-more-step.html",
          "record": "https://links.thingelstad.xyz/links/db98dbfb5f3c/"
        },
        {
          "id": "d92c26ef525f",
          "title": "New iPhone applications to appear tonight",
          "url": "http://9to5mac.com/2008/03/06/new-iphone-applications-to-appear-tonight/",
          "record": "https://links.thingelstad.xyz/links/d92c26ef525f/"
        }
      ]
    },
    "mentions": [],
    "mentioned_by": [],
    "saved_twice": []
  },
  "elsewhere": {
    "wayback": "https://web.archive.org/web/*/http://alexking.org/blog/2008/03/06/mysql-latin1-utf8-conversion"
  }
}
