How to handle the failure of inserting rare characters in MySQL (Incorrect string value)

How to handle the failure of inserting rare characters in MySQL (Incorrect string value)

Recently, the business side reported that some user information failed to be inserted, and the error message was similar to "Incorrect string value:"\xF0\xA5.....". Judging from this message, it should be caused by the character set not supporting a certain uncommon character.

Here is the scenario reproduced in the virtual machine:

Step 1, simulate the original table structure character set environment:

use test;
CREATE TABLE `t1` (
 `id` int(10) NOT NULL AUTO_INCREMENT,
 `real_name` varchar(255) CHARACTER SET utf8 DEFAULT '' COMMENT 'Name',
 `nick` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT 'Nickname',
 PRIMARY KEY (`id`)
) ENGINE=InnoDBAUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Membership Card';

Step 2, insert rare words (using sqlyog simulation):

1. First simulate the online environment and set the character set:

wKiom1kUGECCXdKDAAAV9L1hpEY173.png

2. Insert rare characters (for rare characters, please refer to: http://www.qqxiuzi.cn/zh/hanzi-unicode-bianma.php?zfj=kzb&ks=24E20&js=257E3)

wKioL1kUGEHCtaAvAABxJXp_7Ow741.png

We try to insert Wang (hold down ALT in Word and enter 152964). You can see that the insert failed.

step3. Modify the character set of real_name:

use test;

alter table t1 change real_name real_name varchar(255) CHARACTER SET utf8mb4 DEFAULT '' COMMENT 'Name';

If it is a large online table, you can use pt-osc to process it. The command is as follows:

pt-online-schema-change -uroot -h localhost --alter=" change real_name real_name varchar(255) CHARACTER SET utf8mb4 DEFAULT '' COMMENT '姓名' " D=test, t=t1 --no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --quiet --charset=utf8mb4 --dry-run
pt-online-schema-change -uroot -h localhost --alter=" change real_name real_name varchar(255) CHARACTER SET utf8mb4 DEFAULT '' COMMENT '姓名' " D=test, t=t1 --no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --quiet --charset=utf8mb4 --execute

Step 4, insert the experiment again:

1. Set the character set first:

wKiom1kUGECCXdKDAAAV9L1hpEY173.png

2. Insert again and you can see that the insertion is successful.

wKioL1kUGILTLvpxAAAdWKxKR5A899.png

There is no garbled code in the command line query:

wKiom1kUGIPyeykFAAAiRzDqNxs956.png

That’s it, you can refer to it.

You may also be interested in:
  • Solution to the error message "java.sql.SQLException: Incorrect string value:'\xF0\x9F\x92\xA9\x0D\x0A...'" when storing emoticons in MySQL
  • MySQL throws Incorrect string value exception analysis
  • MySql error Incorrect string value for column
  • Solution to Incorrect string value in MySQL

<<:  win2008 server security settings deployment document (recommended)

>>:  Detailed explanation of routing parameter passing and cross-component parameter passing in Vue

Recommend

MySQL 8.0.25 installation and configuration tutorial under Linux

The latest tutorial for installing MySQL 8.0.25 o...

Detailed explanation of key uniqueness of v-for in Vue

Table of contents 1. DOM Diff 2. Add key attribut...

Implementation example of uploading multiple attachments in Vue

Table of contents Preface Core code File shows pa...

How to generate a unique server-id in MySQL

Preface We all know that MySQL uses server-id to ...

jQuery implements all shopping cart functions

Table of contents 1. Select All 2. Increase or de...

mysql command line script execution example

This article uses an example to illustrate the ex...

Mac installation mysqlclient process analysis

Try installing via pip in a virtual environment: ...

CSS achieves highly adaptive full screen

When writing my own demo, I want to use display:f...

Zookeeper stand-alone environment and cluster environment construction

1. Single machine environment construction# 1.1 D...

12 Javascript table controls (DataGrid) are sorted out

When the DataSource property of a DataGrid control...

Basic knowledge: What does http mean before a website address?

What is HTTP? When we want to browse a website, w...

Analysis of several reasons why Iframe should be used less

The following graph shows how time-consuming it is...

How to solve the problem of clicking tomcat9.exe crashing

A reader contacted me and asked why there were pr...

Best Practices for Sharing React Code

When any project develops to a certain complexity...