MySQL Incorrect string value error: correct solution with utf8mb4
Check link and column encoding together in MySQL character error; Plan utf8mb4 conversion with backup, collation and index checks.
Seen when adding text in MySQLIncorrect string valueerror may indicate that the characters sent cannot be represented by the character set of the target column or link. Especially in texts containing emojisutf8mb3columns are a common source.
The solution is to control together the path the data travels: the text generated by the application, the connection encoding of the driver, and the destination column. Just setting the database's default to utf8mb4 will not convert existing columns.
difference between utf8 and utf8mb4
MySQL historicalutf8the name is in many versionsutf8mb3is associated with. This character set uses up to three bytes per character.utf8mb4can also represent Unicode characters requiring four bytes. Not all emojis are the same length; The problem occurs especially with characters in additional Unicode planes.
Four bytes are not required for Turkish letters. If the Turkish text is also corrupted, investigate reasons such as incorrect connection coding, incorrect source decoding or resaving of incorrect data. The utf8mb4 conversion does not automatically repair previously corrupted text.
1. Examine the target column and link
The following table and database names are examples; replace it with your actual schema:
SELECT VERSION();
SHOW CREATE TABLE app_db.messages;
SHOW FULL COLUMNS FROM app_db.messages;
SHOW VARIABLES LIKE 'character_set_%';
SHOW VARIABLES LIKE 'collation_%';
In particular, depending on the character set of the text column andcharacter_set_client, character_set_connection, character_set_resultsLook at the values. If possible, query these variables over the connection used by the application. The connection to the management tool may be set differently than the application.
2. Prepare the transformation in the test database
Take a backup and test restore before the schema change. The conversion may rebuild the table, require locks, or additional disk space. Plan for maintenance time for large tables and your version's DDL behavior.
Example belowutf8mb4_unicode_ciIt uses collation. This isn't the only right choice for every project. Examine current behavior in terms of case and accent comparisons, sorting, UNIQUE indexes, and foreign key-linked text columns. Different comparison rules may introduce new conflicts.
ALTER DATABASE app_db
CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE app_db.messages
CONVERT TO CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
The first command changes the database default, the second command changes the text columns of the relevant table. It does not automatically change all tables in the database. If you are going to convert a single columnMODIFYwith the real column definition; Write while preserving length, null/default and other properties.
Table conversion can change some text types to wider type. Old version and index size restrictions may also have an impact. For this reasonSHOW CREATE TABLECompare the output before and after conversion. Putting incorrectly encoded bytes into normal charset conversion can convert the corrupted text to a different corrupted text.
3. Make the application connection utf8mb4
For example, in a Python application using PyMySQL, the relevant area of the connection configuration is:
connection = pymysql.connect(
host=db_host,
user=db_user,
password=db_password,
database="app_db",
charset="utf8mb4"
)
db_host, db_userAnddb_passwordmust come from the application's own configuration. If you are using another language or driver, use its charset option. Connection pools can retain old sessions; Verify that the new connection is created with the updated setting.
SET NAMES utf8mb4Changes character settings for a particular SQL session. This setting is not carried over when the application opens another connection. Proper identification at the driver level is a more consistent approach than manually issuing commands to a management session.
4. Check register and readback
Via parameterized query to a test table with the same application connectionİstanbul, şeker, çığ, 😀Write a value like and read it back. Accepting emoji alone is not enough; Check that Turkish letters are also preserved.
CHAR_LENGTHcharacter,LENGTHReturns the number of bytes. You can examine the test value when your SQL client also uses UTF-8/utf8mb4. If the result is still corrupt, investigate whether the data was previously decoded incorrectly in the application. Turning off SQL strict mode or deleting emojis does not resolve the error in the encoding path.
Short guide
Common mistake
Assuming that after changing the database default the existing columns are also transformed. The target column and application connection must be verified separately.