Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

utf8mb4 in MySQL Workbench and JDBC

I've been working with a UTF-8 encoded MySQL DB that now needs to be able to store 4-byte emojis, so I decided to change from utf8 encoding to utf8mb4:

ALTER DATABASE bstdb CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
ALTER TABLE HISTORY CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE HISTORY CHANGE SOURCE_CONTEXT SOURCE_CONTEXT VARCHAR(2000) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

And changed mysql.conf.d "character-set-server = utf8" to "character-set-server = utf8mb4"

After these steps, I am able to store emojis (as 💢),but only when the SQL query is executed in the MySQL console: When I try to launch the query from MySQL Workbench or from a Wildfly webapp, I am getting this error:

Error Code: 1366. Incorrect string value: '\xF0\x9F\x92\xA2' for column 'SOURCE_CONTEXT' at row 1

I assume I need to change the way the clients are connecting to the DB, but I have no clue on how. I've read something on using "useUnicode=yes" in JDBC, but does not work.

${bdpath:3306/bstdb?useUnicode=yes}

Edit: As suggested in comments, I tried with:

${bdpath:3306/bstdb?characterEncoding=UTF-8}

but no luck, I am getting the same "Incorrect string value: '\xF0\x9F\x92\xA2'" error.

Also tried

${bdpath:3306/bstdb?useUnicode=true&characterEncoding=utf8mb4&}

but it refuses to stablish a connection.

Any idea on how to configure MySQL workbench and/or JDBC/Wildfly?

MySQL version is 5.7.18

MySQL WorkBench version is 6.0.8

JDBC driver version is 5.1.34

Thanks!

like image 602
motagirl2 Avatar asked Jun 16 '17 14:06

motagirl2


People also ask

Does MySQL support utf8mb4?

MySQL supports multiple Unicode character sets: utf8mb4 : A UTF-8 encoding of the Unicode character set using one to four bytes per character. utf8mb3 : A UTF-8 encoding of the Unicode character set using one to three bytes per character.

Should I use utf8mb4 or utf8?

The difference between utf8 and utf8mb4 is that the former can only store 3 byte characters, while the latter can store 4 byte characters. In Unicode terms, utf8 can only store characters in the Basic Multilingual Plane, while utf8mb4 can store any Unicode character.

What is the difference between utf8mb4 and utf8 charsets in MySQL?

utf-8 can store only 1, 2 or 3 bytes characters, while utf8mb4 can store 4 bytes characters as well. utf-8 is a subset of characters given by utf8mb4 .


2 Answers

Starting from MySQL Connector/J 5.1.47,

When UTF-8 is used for characterEncoding in the connection string, it maps to the MySQL character set name utf8mb4.

You can check docs here

like image 133
Alaa Nassef Avatar answered Sep 21 '22 05:09

Alaa Nassef


Use characterEncoding=utf8 for jdbc url

jdbc:mysql://x.x.x.x:3306/db?useUnicode=true&characterEncoding=utf8

Also check that you have configured MySQL to work with utf8mb4

    [client]
default-character-set = utf8mb4

[mysql]
default-character-set = utf8mb4

[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

See here

like image 27
Mike Adamenko Avatar answered Sep 21 '22 05:09

Mike Adamenko