Stories
Slash Boxes
Comments

News for nerds, stuff that matters

Migrate a MySQL Database Preserving Special Characters

Posted by kdawson on Mon May 07, 2007 12:30 AM
from the encoding-juju dept.
TomSlick writes "Michael Chu's blog provides a good solution for people migrating their MySQL databases and finding that special characters (like smart quotes) get mangled. He presents two practical solutions to migrating the database properly."
This discussion has been archived. No new comments can be posted.
Display Options Threshold:
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • Migration (Score:4, Informative)

    by dfetter (2035) on Monday May 07 2007, @12:38AM (#19017095)
    (http://fetter.org/~shackle/)
    Better still, install DBI-Link http://pgfoundry.org/projects/dbi-link/ [pgfoundry.org] inside PostgreSQL, migrate once and have done ;)
    • Re:Migration by alexandreracine (Score:2) Monday May 07 2007, @11:24AM
    • Re:Migration by try_anything (Score:1) Monday May 07 2007, @02:41AM
      • 1 reply beneath your current threshold.
    • 2 replies beneath your current threshold.
  • by Architect_sasyr (938685) on Monday May 07 2007, @12:40AM (#19017099)
    There's about 8,000 wordpress blog's out there that could use this. Pity I can't mod an article insightful
    • Re:Useful? This is damned awesome! (Score:5, Informative)

      by jamshid (140925) on Monday May 07 2007, @01:38AM (#19017415)
      Then send the wordpress developers this link:

      http://www.joelonsoftware.com/articles/Unicode.htm l [joelonsoftware.com]
      The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
      [ Parent ]
      • Re:Useful? This is damned awesome! by frisket (Score:2) Monday May 07 2007, @05:09AM
      • UCS-2 only covers plane zero (the Basic Multilingual Plane, or BMP). It doesn't cover code points outside that. Unicode actually supports the entire UCS, all 1.1 million (and growing) code points.

        In other words, Joel has made the same mistake as the people who wrote software that only works in 7-bit ASCII or 8-bit UTF-8 or the IBM or Apple or Adobe 8-bit extended ASCII sets or the 9-bit extended ASCII set that ITS used, or...

        And it's already too late to try and cram everything into 2 bytes. After the Han Unification mess (the attempt to force Chinese and Japanese and everything else that used some variant of Chinese ideograms (Kanji, etc...) into a common subset of similar characters that fit in the 65535 available codes in the BMP) the People Republic of China decided to require their computers to support their national encoding anyway. As of 2000.

        So you have to support the full UCS encoding anyway.

        There's three storage formats that it's practical to use: UCS-4 (4 bytes per character, with the same byte-ordering problems as UCS-2), UTF-16 (2-4 bytes per character, same as UCS-2 for the BMP) or UTF-8 (1-4 bytes per character). Internally: you can use UCS-4 as your wide character type, and translate on the fly; use UTF-8 and use care to avoid breaking strings in the middle of glyphs or use UTF-16 and translate on the fly and use care to avoid breaking strings in the middle of glyphs.

        If Joel is lucky the libraries he's using are actually operating on UTF-16 strings instead of UCS-2 strings. If he's *really* lucky they're designed to avoid breaking up codes outside the BMP. If he's *super* lucky he's managed to avoid creating any code that just operates on strings as a stream of wchar_t anywhere.

        Personally, I think that UTF-16 gets you the worst of both worlds: your data is almost certainly less compact than if you use UTF-8; you still have to deal with multi-wchar_t strings so your code is no easier to write than if you used UTF-8... you're just less likely to find bugs in testing; and you get byte order issues in files just like you would with UCS-4. Unless you think UCS-2 is "good enough" and you just ignore everything outside the BMP and discover that people in China are suddenly getting hash when they use your program.
        [ Parent ]
        • Re:Big Trouble in Little China. Don't use UCS-2. by wiredlogic (Score:2) Monday May 07 2007, @11:02AM
        • by epine (68316) on Monday May 07 2007, @07:26PM (#19029849)
          That was a good post, but I don't understand your premise whatsoever. There seems to be two tactics at work here: arbitrary line drawing, and the belief that if you can't make everyone happy the best compromise is to make everyone unhappy. I read that post by Joel long ago, and I just read it again. I don't think he could have done a better job in the space devoted to it.

          My one criticism of Joel was passing himself the "get out of jail free" card. Before I get started, I should warn you that if you are one of those rare people who knows about internationalization, you are going to find my entire discussion a little bit oversimplified. This is a fair disclaimer, but it makes it impossible to judge where Joel was simplifying deliberately and where he simplified because he didn't know any better. The correction would be for Joel to state "I'm going to simplify issues X, Y, and Zed". Then mistakes in the middle of the alphabet would be entirely his own. Just as there is no such thing as a string without a coding system, there is no such thing as a useful disclaimer that doesn't specify precisely what it disclaims. It amused me to see Joel invoke the ASCII standard of accountability.

          Concerning the claim that Joel has made the same mistake [over again], this same claim comes up all the time concerning address arithmetic. How much existing code is portable to a 128 bit address size? We're sure to need this by 2050. Or perhaps not. People tend to neglect the observation that we're talking about a doubly exponential progression in codespace: (2^2^3)^2^N, with the values N=0,1,2,3,4 plausible in photolithographic solid state. On the current progression, for N=5 transistors would need to be subatomic. As far as the present transition from 32 bits to 64 bits of address space, it makes sense that operating systems and file systems are 64-bit native, while 99% of user space applications continue to run in less time and space compiled for 32 bits. Among the growing sliver of applications that do run better in 64-bits are a few applications of especially high importance.

          I worked extensively with CJK languages in the early 1990s, and my opinion at the time was that UCS-4 primarily catered to the SETI crowd, and potentially, belligerent Mandarins in mainland China. I recall more argument at the time about Korean, which is a syllabic script masquerading as ideographic blocks.

          http://en.wikipedia.org/wiki/Hangul [wikipedia.org]

          I've always had a lot of trouble understanding the opposition to Han unification. Many distinctions in the origins of the English language were lost in the adoption of ASCII, such as the ae ligature and the old-English thorn (which causes many Hollywood sets to feature "Ye old saloon").

          http://en.wikipedia.org/wiki/Han_unification [wikipedia.org]
          http://en.wikipedia.org/wiki/Thorn_(letter) [wikipedia.org]

          ... Unicode now encodes far more [Han] characters than any other standard, and far more than were listed in any dictionary, with many more being processed for inclusion as fast as the scholars can agree on their identities.

          Some characters used only in names are not included in Unicode. This is not a form of cultural imperialism, as is sometimes feared. These characters are generally not included in their national character sets either.


          And all this fits quite nicely in UCS-2 as advocated by Joel.

          A slight difference in rendering characters might be considered a serious problem if it changes the meaning or reflects the wrong cultural tradition. Besides a simple nuisance like Japanese text looking like Chinese, names might be displayed with a different glyph -- the same character in the sense of encoding but a different character in the view of the users. This rendering problem is often employed to criticize Westerners for not being aware o
          [ Parent ]
        • Re:Big Trouble in Little China. Don't use UCS-2. by argent (Score:2) Monday May 07 2007, @11:33AM
        • 2 replies beneath your current threshold.
      • 1 reply beneath your current threshold.
    • Re:Useful? This is damned awesome! by Krischi (Score:2) Monday May 07 2007, @06:55AM
  • Pffft Easy... (Score:5, Funny)

    by Frogbert (589961) on Monday May 07 2007, @12:53AM (#19017163)
    First you get the names of every table in the old database

    Then you create these tables in the new one. Just so there are no problems with data types you should probably just make every field varchar(100) in the new one.

    Then you fire up MS Access, the older the better. I try to stick to Access 95.

    Then you create two ODBC links, one to your old one and one to the new one.

    Then you use the linked table manager to link each table to ms access.

    Then you open both the new table and the old table and select all, copy and paste the data into the new table.

    It's so simple even a child could do it!
  • How is this news? (Score:1)

    by cabinetsoft (923481) on Monday May 07 2007, @01:12AM (#19017273)
    How is this news??? Later on today: "Data from POST/GET and special characters"?
  • Unicode integration woes (Score:5, Interesting)

    by DJ Rubbie (621940) on Monday May 07 2007, @01:15AM (#19017301)
    (http://127.0.0.1/I_h4x0r_j00r_cpu | Last Journal: Friday August 24, @06:57PM)

    As I understand it, the problem arises from the fact that mysqldump uses utf8 for character encoding while, more often than not, mysql tables default to latin1 character encoding. (If you were smart enough to manually set the character encoding to utf8, then you'll have no problems - everyone running mysql 4.0 or early will be using latin1 since it didn't support any other encodings.) So lets say we have a database named example_db with tables that have varchar and text columns. If you have special characters that are really UTF-8 encoded characters stored in the db, it works just fine until you try to move the db to another server.

    That bit me one time when one of my live servers crashed and I had to load the data on the backup onto a different server. I remember wondering to myself, when was the good old days when a database was a dumb (smart, depending your POV) engine that only worries about a string of bytes (chars). Seriously, it only should become smarter and start talking in unicode only when I want it to.

    Issues with using unicode is not just limited to MySQL, as Python have similar issues. However they are almost always caused by poor programmers who mixes usage between the two, or not doing type checking on the proper type (basestring).
  • mysqldump is too think (Score:3, Interesting)

    by hpavc (129350) on Monday May 07 2007, @01:37AM (#19017411)
    This guys mysqldump statement could use some args, too much is packed in his my.cnf defaults to make this truely useful as a how to. He could easily cause more problems than he is solving.
  • by rylin (688457) on Monday May 07 2007, @02:12AM (#19017575)
    Not a single day seems to go by without a gnubie or two posting things that are really basic knowledge.
    If you do insert unicode data into a latin1 table, you will get unexpected results.

    What you do is make sure that your:
    a) database(s) are set to utf8 by default
    b) table(s) are set to utf8 by default
    c) column(s) are set to utf8 by default
    d) connection defaults to utf8
    (provided, of course, that it's utf8 you're after)

    That way, it'll "Just Work"(tm)

    We've gone through upgrades from 3.23 -> 4.0 -> 4.1 -> 5.0 and never had a problem; and yes, our tables were all latin1 from the beginning.

  • Tagged slashdigg (Score:2)

    by Cutie Pi (588366) on Monday May 07 2007, @05:01AM (#19018407)
    Anyone noticing a strong similarity between this article and about 95% of the stuff posted on digg every day? What's next, "The Top 10 Photoshop Filters of All Time?" :) Tagged slashdigg.
  • From TFA:

    If you were smart enough to manually set the character encoding to utf8, then you'll have no problems
    That's all that's needed here, the whole article & this page can be condensed in a 1 line Question & 1 line Answer - doh!
    Oh & you can change the encoding at anytime, so even if you initially forget you just change it later, then dump.
  • by v3xt0r (799856) on Wednesday May 09 2007, @01:01AM (#19048539)
    when doing (non-'real-time'-critical) migrations... I find it much less troublesome to just scp the files and then run mysqlcheck to repair the tables on the new server (if required).

    http://www.seanodonnell.com/code/?id=66 [seanodonnell.com]

    That process prevents the syntax conversion issues, storage and bandwidth requirements, and processing-time requirements caused by using mysqldump.
  • Re:how about ... (Score:3, Informative)

    by Majik Sheff (930627) on Monday May 07 2007, @01:11AM (#19017265)
    It usually works as long as you're staying on the same architecture. I successfully pulled this off when a client's DB server died horribly with no functional backups in sight. I salvaged the vast majority of the binary tables and dropped them into a fresh install of MySQL. After the migration of the binaries I renamed the tables to *_bak and told MySQL to dump the contents into freshly created data files. Then I set my client on the task of assessing damage to the data. I would only recommend this tactic if you're doing crash recovery on a borked system, as there are risks. P.S. Have you hugged your backups today?
    [ Parent ]
  • I share your attitude. Sometimes people where I work send snippets of code and whatnot over Outlook, and it gets corrupted with smiley faces.
    [ Parent ]
  • 4 replies beneath your current threshold.