6

In a lot of places, it is noted that if you want to use JDBC with MySQL and have support for UTF-8, you need to add useUnicode=true in the JDBC URL.

E.g.:

However, I tried this locally using MySQL 5.7.19 on Mac OS X with mysql-connector-java 5.1.41 (pulled in by Spring Boot 1.5.3). I added a file /etc/my.cnf with:

[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8

This makes sure MySQL is set up for UTF-8:

mysql> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+-----------------------------------------------------------+
| Variable_name            | Value                                                     |
+--------------------------+-----------------------------------------------------------+
| character_set_client     | utf8                                                      |
| character_set_connection | utf8                                                      |
| character_set_database   | utf8                                                      |
| character_set_filesystem | binary                                                    |
| character_set_results    | utf8                                                      |
| character_set_server     | utf8                                                      |
| character_set_system     | utf8                                                      |
| character_sets_dir       | /usr/local/mysql-5.7.19-macos10.12-x86_64/share/charsets/ |
+--------------------------+-----------------------------------------------------------+
8 rows in set (0.00 sec)

Now in my application.properties I tried with:

spring.datasource.url=jdbc:mysql://localhost:3306/testdb

and

spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useUnicode=true

But in both cases, it seems to be ok. So: is the advice to add useUnicode=true still valid?

Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
  • "But in both cases, it seems to be ok.". What's the "it" that seemed to be okay? – Kayaman Sep 11 '17 at 12:36
  • Storing and retrieving text in a `TEXT` MySQL column that has international characters like ř, á, ý, ž, ... – Wim Deblauwe Sep 11 '17 at 12:40
  • There indeed is a redundancy in MySQL. AFAIK the URL setting is for the JDBC driver at the client communicating the textual data in UTF-8. Whether SET NAMES is equivalent, or what exactly happens - _no idea._ – Joop Eggen Sep 11 '17 at 12:41
  • 2
    Moreover, according to https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html it is `true` by default anyway. – Wim Deblauwe Sep 11 '17 at 13:11
  • `useUnicode` is no longer present in configuration properties for [MySQL Connector/J 8.0(.18)](https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-configuration-properties.html#connector-j-reference-set-config) and the only place where it is mentioned is in the context of versions prior to 8.0.13 : [How can I use 3-byte UTF8 with Connector/J?](https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-troubleshooting.html) – MartinBG May 11 '20 at 19:24

0 Answers0