##MySQL UTF-8 encodingMySQL has supported UTF-8 since version 4.1, that is, in 2003. However, the currently popular UTF-8 standard (RFC 3629) was stipulated after that. Because of this, UTF-8 in MySQL is inconsistent with UTF-8 in our daily development, which has caused some problems. MySQL's UTF-8 only supports up to three bytes per character, while true UTF-8 is up to four bytes per character. Reproduction of the problemThere are database tables as follows: utf8 encoding method
##Go to a record in the database:
@Test public void testInsert() { User user = new User(); user.setUsername("\uD83D\uDE00 "); user.setPassword("123456"); userRepo.save(user); }
This is just part of the code. It doesn’t matter if you don’t understand it. This is to insert a record into the user table. Where username is \uD83D\uDE00.
Actually \uD83D\uDE00 is an emoji expression.
Because the utf8 character set in MySQL only supports the Unicode range of three-byte UTF-8 encoding, and emoji characters belong to the four-byte encoding part, so the program is expected to run Reported an error. Run this code:
# As expected, an error is reported.
Solve the problem
Summary
MySQL Tutorial column to learn!
The above is the detailed content of Why not use UTF-8 encoding in MySQL. For more information, please follow other related articles on the PHP Chinese website!