MySQL change character set utf8 to utf8mb4

Debian 7; with MySQL version 5.5.3 or higher

utf8mb4, a UTF-8 encoding of the Unicode character set using one to four bytes per character.
utf8, a UTF-8 encoding of the Unicode character set using one to three bytes per character.

NOTE; Be sure to make backup from all databases on the server, before you make any changes.
utf8mb4 is fully compatible with utf8, but the backup is necessery!

If you have multiple servers with differend versions of MySql but
you are using the same script and the database, DO NOT change character set to utf8mb4.
Want if you need to convert back to utf8 you could lose some characters.


You can change character set from your database and table with ALTER command.

In your older database you are using character set utf84 now you need to change the character set to utf8mb4 with ALTER command


But before...
InnoDB has a maximum index length of 767 bytes, 255x3=765bytes for utf8
If you use very long VARCHAR(255), you will need to change the size to VARCHAR(191) if is that not enough, you will need change VARCHAR type to a TEXT type. The same you need to do with type TINYTEXT.
utf8mb4 use 4 bytes per character this can looks as; 767 / 4 = 191

Check all definitions on columns and indexes to make sure they will not exceed the maximum length.

# Use this for each database:

ALTER DATABASE your_database CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;


# Use this for each database table

ALTER TABLE database_tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;


# Use this for each each column

ALTER TABLE database_tablename MODIFY column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

 



Links:

Converting Between 3-Byte and 4-Byte Unicode Character Sets - mysql.com
Data Type Storage Requirements - mysql.com
The CHAR and VARCHAR Types - mysql.com
The BLOB and TEXT Types - mysql.com
Integer Types - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT - mysql.com
Unicode Support - mysql.com