MySQL MariaDB Set utf8mb4 as default charset
2 minute read
By default charset is set to to latin1 and utf8. This can be checked by executing the following command
MariaDB [(none)]> SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';
+--------------------------+-------------------+
| Variable_name | Value |
+--------------------------+-------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+--------------------------+-------------------+
To set the charset as utf8mb4 do the following steps:
sudo vi /etc/mysql/conf.d/mariadb.cnf
Uncomment the following lines
[client]
#default-character-set = utf8
[mysqld]
#character-set-server = utf8
#collation-server = utf8_general_ci
#character_set_server = utf8
#collation_server = utf8_general_ci
replace the above section as follows:
[client]
default-character-set = utf8mb4
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
character_set_server = utf8mb4
collation_server = utf8mb4_unicode_ci
Note:
- changed all utf8 to utf8mb (More info on utf8mb4 check reference section below)
- changed utf8_general_ci to utf8mb4_unicode_ci
Check the variable again.
MariaDB [(none)]> SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';
+--------------------------+--------------------+
| Variable_name | Value |
+--------------------------+--------------------+
| 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 |
| collation_connection | utf8mb4_general_ci |
| collation_database | utf8mb4_unicode_ci |
| collation_server | utf8mb4_unicode_ci |
+--------------------------+--------------------+
Note:
Including
skip-character-set-client-handshake
or
character-set-client-handshake = FALSE
in my.cnf will change collation_connection variable display as utf8mb4_unicode_ci instead of utf8mb4_general_ci, however it force the connection to use utf8mb4_uncode_ci regardless of whatever requested by client.
References
- Why use utf8mb4? https://medium.com/@adamhooper/in-mysql-never-use-utf8-use-utf8mb4-11761243e434
- Difference between utf8_general_ci and utf8_unicode_ci https://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
- https://mathiasbynens.be/notes/mysql-utf8mb4
- https://dev.mysql.com/doc/refman/5.5/en/server-options.html#option_mysqld_character-set-client-handshake
- http://jbisbee.blogspot.in/2013/07/set-utf-8-as-default-mysql-encoding.html