How to support emojis in MySQL databases

November 8, 2020

If you are still using utf8 charset you may have noticed that it cannot store all unicode characters as it promises, and that's because this charset uses 3 bytes per character instead of 4 that is required.

For this reason, MySQL has another charset: utf8mb4. And now is the time to switch your database in this charset.

For that, you have to modify existing tables and columns:

Databases:

ALTER DATABASE db_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

Tables:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8mb4 COLLATE = utf8mb4_unicode_ci;

Columns:

ALTER TABLE tbl_name CHANGE col1 col1 VARCHAR(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;