57

I am using mysql 5.5.10, and its character_sets are

| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
| collation_connection     | utf8mb4_general_ci         |
| collation_database       | utf8mb4_general_ci         |
| collation_server         | utf8mb4_general_ci         |

I changed utf8mb4 from utf8 for iOS5's emoji. They are represented by 4byte code.

But when I inserted 3 smiley emojis, '???' is in mysql.

They are 3F 3F 3F (Hex).

I can store iOS4's emojis well, but not iOS5's.

How could I store iOS5's emojis?

Please help me.

Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
plenty
  • 583
  • 1
  • 5
  • 6
  • 1
    mysql phpadmin shows ???, but iOS app can show emoji. – Can Aksoy Mar 03 '13 at 14:27
  • See also http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored/ , especially discussion of "question marks". – Rick James Aug 19 '16 at 18:48
  • See also https://stackoverflow.com/questions/39463134/how-to-store-emoji-character-in-mysql-database/50264108 – ospider Nov 14 '20 at 09:26

2 Answers2

93

4 byte Unicode characters aren't yet widely used, so not every application out there fully supports them. MySQL 5.5 works fine with 4 byte characters when properly configured – check if your other components can work with them as well.

Here's a few other things to check out:

  • Make sure all your tables' default character sets and text fields are converted to utf8mb4, in addition to setting the client & server character sets, e.g. ALTER TABLE mytable charset=utf8mb4, MODIFY COLUMN textfield1 VARCHAR(255) CHARACTER SET utf8mb4,MODIFY COLUMN textfield2 VARCHAR(255) CHARACTER SET utf8mb4; and so on.

    If your data is already in the utf8 character set, it should convert to utf8mb4 in place without any problems. As always, back up your data before trying!

  • Also make sure your app layer sets its database connections' character set to utf8mb4. Double-check this is actually happening – if you're running an older version of your chosen framework's mysql client library, it may not have been compiled with utf8mb4 support and it won't set the charset properly. If not, you may have to update it or compile it yourself.

  • When viewing your data through the mysql client, make sure you're on a machine that can display emoji, and run a SET NAMES utf8mb4 before running any queries.

Once every level of your application can support the new characters, you should be able to use them without any corruption.

Nick Baicoianu
  • 2,275
  • 20
  • 15
66

I’ve recently written a detailed guide on how to switch from MySQL’s utf8 to utf8mb4. If you follow the steps there, everything should work correctly. Here are direct links to each individual step in the process:

Hope this helps.

Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
  • Hi, on step 3, do I have to do that for every single table, column in my db? or just to the table and column I want to store emojis in? because right now I am changing just the one column and as soon as I set `SET character_set_results = utf8mb4;` I can't execute any queries on any tables. I'm metting a `Server sent uknown charsetnr. Please report` error! (even for select queries) – iamserious Jan 21 '15 at 17:43
  • @iamserious - Don't set just `...results`, set all 3 things, as with `SET NAMES` or the connection parameters. – Rick James Aug 01 '16 at 15:47
  • Great tutorial! Do you know why after setting `character-set-server = utf8mb4` in my.cnf, after running the query on the database the result is that character-set-server is actually latin1? Do you know why is not updating? Thank you very much! – FrancescoMussi Sep 05 '16 at 12:05
  • If you want to take a look here is my similar problem: https://stackoverflow.com/questions/39326125/cannot-store-emoji-in-database – FrancescoMussi Sep 05 '16 at 12:05
  • Hi Mathias, can I ask, if there's actually just one column that I'd like to support emoji, can I just change the database, one table, and one column's character set? can I remain all other columns as normal UTF-8? – Chor Wai Chun Dec 20 '16 at 03:40
  • @Chor Wai Chun: Sure. But IMHO it’s better to follow best practices and enable full Unicode everywhere. That way, if you ever need it for any remaining columns, you can just start using it without having to mess around with the database/table/column settings. – Mathias Bynens Dec 21 '16 at 07:23
  • @MathiasBynens If I am planning to have few tables using utf8mb4, should I make all other tables and entire database to use utf8mb4 ? – sofs1 Jun 02 '17 at 08:13
  • @MathiasBynens Also how to set a table to use utf8mb4 while creating the table? – sofs1 Jun 02 '17 at 08:15