Search code examples
unicodemariadbutf8mb4

Minimal setup for MariaDB to handle Emojis


I followed https://stackoverflow.com/a/35189650/4534 but still I get Error 1366: Incorrect string value, when posting an emoji into "name":

INSERT INTO `users` (`name`) VALUES ('Foo bar 😋');

SQL setup is:

SET NAMES utf8mb4;
CREATE DATABASE rest_api_example;
USE rest_api_example;
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    age INT NOT NULL
);

Database configuration

I use the mariadb image to test the above sql via https://github.com/kaihendry/GoApiTutorial/blob/master/docker-compose.yml


Solution

  • When I add command: ['--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci'] to the docker-compose.yml

    utf8 in MySQL

    Once I've done this, Emoji characters appear to work. However I've since discovered I can set charset/collation in the database creation, for example:

     CREATE DATABASE rest_api_example character set utf8mb4 collate utf8mb4_unicode_ci;
    

    And that works better and I don't have to fiddle with mysqld settings.