Databases 7 min read

How to Fix MySQL Emoji Errors with utf8mb4: A Complete Guide

This article explains why MySQL throws "Incorrect string value" when storing emojis, how utf8mb4 solves the problem, and provides step‑by‑step instructions—including table conversion, my.cnf changes, connector upgrades, and runtime workarounds—for reliable emoji storage.

ITPUB
ITPUB
ITPUB
How to Fix MySQL Emoji Errors with utf8mb4: A Complete Guide

When inserting emoji characters into a MySQL database, the default utf8 charset (3‑byte) cannot represent 4‑byte Unicode symbols, causing the error Incorrect string value. The solution is to use the utf8mb4 charset, which is a superset of utf8 and supports all emoji.

1. Convert tables to utf8mb4

Export the affected tables, modify the CREATE statements to use utf8mb4, and re‑import them:

mysqldump -uusername -ppassword database_name table_name > table.sql
mysql -uusername -ppassword database_name < table.sql

2. Ensure MySQL version supports utf8mb4

utf8mb4 is supported from MySQL 5.5.3 onward. Versions such as 5.5.18 work fine, so upgrading the server is not always required.

3. Adjust the MySQL configuration file

Edit /etc/my.cnf (or the appropriate config file) and set the default character set for client, server, and connection layers to utf8mb4:

[client]
# client default charset
default-character-set = utf8mb4

[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

[mysql]
default-character-set = utf8mb4

Restart the MySQL service after saving the file.

4. Upgrade MySQL Connector/J

Use Connector/J 5.1.21 or newer. The connector can autodetect the server’s utf8mb4 setting, so the characterEncoding parameter can be omitted from the JDBC URL.

Runtime workarounds

If modifying the server configuration is impossible, execute SET NAMES utf8mb4 for each connection. In Java you can run: jdbcTemplate.execute("set names utf8mb4"); When using MyBatis, you can add an annotated method to issue the same command before inserts:

@Update("set names utf8mb4")
void setCharsetToUtf8mb4();

@Insert("insert into tb_message ...")
void insert(Message msg);

SqlSession sqlSession = sqlSessioFactory.openSession();
MessageDao messageDao = sqlSession.getMapper(MessageDao.class);
messageDao.setCharsetToUtf8mb4();
sqlSession.commit();
messageDao.insert(message);

Further considerations

Two possible approaches for projects managed by Spring:

Use AOP to execute SET NAMES utf8mb4 before any method that may insert 4‑byte characters. Verify that the AOP advice runs within the same transaction and connection.

Hook into Spring JDBC’s connection creation to run SET NAMES utf8mb4 for every new connection, guaranteeing the charset is set.

Both ideas require testing and validation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

EmojimysqlMyBatisJDBCCharacter Setutf8mb4
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.