How to change all the tables in my database to UTF8 character set?

51,852

Solution 1

mysqldump --user=username --password=password --default-character-set=latin1 --skip-set-charset dbname > dump.sql
sed -r 's/latin1/utf8/g' dump.sql > dump_utf.sql
mysql --user=username --password=password --execute="DROP DATABASE dbname; CREATE DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;"
mysql --user=username --password=password --default-character-set=utf8 dbname < dump_utf.sql

Solution 2

For single table you can do something like this:

ALTER TABLE tab CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

For the whole database I don't know other method than similar to this:

http://www.commandlinefu.com/commands/view/1575/convert-all-mysql-tables-and-fields-to-utf8

Solution 3

replace my_database_name with your database name

SELECT CONCAT('ALTER TABLE ', TABLE_NAME, ' CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;')
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'my_database_name' AND TABLE_TYPE != 'VIEW';

this will build lots of queries which you can run

Share:
51,852
nubela
Author by

nubela

https://giki.wiki/@nubela

Updated on February 01, 2022

Comments

  • nubela
    nubela about 2 years

    My database is not in UTF8, and I'd like to convert all the tables to UTF8, how can I do this?

  • jocull
    jocull over 10 years
    For those without command line access, you can quickly run SHOW TABLES; in your database, paste them into NimbleText at nimbletext.com/live and then use this pattern to generate the change script: ALTER TABLE `$0` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
  • Charlie Gorichanaz
    Charlie Gorichanaz almost 10 years
    And hopefully your database doesn’t contain “latin1” inside any of the content, such as blog posts about character encoding… else your users may have grounds for a lawsuit if your post conversion website is suddenly offering erroneous database commands!
  • scribe
    scribe almost 9 years
    after the sed: uconv --from-code latin1 --to-code utf8 dump_utf.sql > dump_utf_fixed.sql
  • Nick T
    Nick T over 6 years
    @marcolopes The stream editor. man sed or look it up on Wikipedia or the like.
  • Smith
    Smith about 6 years
    You should also add ` AND Table_Type != 'VIEW'`
  • ValRob
    ValRob almost 6 years
    It apply for a 125MB data base?'
  • Michael Scott Asato Cuthbert
    Michael Scott Asato Cuthbert over 5 years
    I would suggest at the same time going not to utf8 but utf8mb4 so that astral plane characters such as emoji can also be stored.
  • Eli
    Eli about 5 years
    @marcolopes something she sed =)