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

The latest popular script Autojs source code sharing

Today I will share with you a source code contain...

Learn Hyperlink A Tag

ask: I have styled the hyperlink using CSS, but i...

Detailed explanation of common methods of JavaScript arrays

Table of contents Common array methods pop() unsh...

CSS tips for controlling animation playback and pause (very practical)

Today I will introduce a very simple trick to con...

The principle and implementation of js drag effect

The drag function is mainly used to allow users t...

How to choose between MySQL CHAR and VARCHAR

Table of contents VARCHAR and CHAR Types Conclusi...

jQuery achieves the effect of advertisement scrolling up and down

This article shares the specific code of jQuery t...

MySQL 5.7 installation and configuration tutorial

This article shares the MySQL installation and co...

Docker implements container port binding local port

Today, I encountered a small problem that after s...

How to set up scheduled backup tasks in Linux centos

Implementation Preparation # Need to back up the ...

Vue implements the method example of tab routing switching component

Preface This article introduces the use of vue-ro...

The process of using vxe-table to make editable tables in vue

There is a table in the project that needs to be ...

JavaScript basics of this pointing

Table of contents this Method In the object Hidde...