Home > Database > Mysql Tutorial > How to handle the failure of inserting rare characters into MySQL (picture)

How to handle the failure of inserting rare characters into MySQL (picture)

黄舟
Release: 2017-05-14 09:57:34
Original
2613 people have browsed it

Recently, the business side reported that the insertion of individual user information failed, and the error message was similar to Incorrect string value: "\xF0\xA5.... This prompt should be Character setIt is caused by a rare word not being supported. Friends who need it can refer to it

Recently, the business side reported that the insertion of individual user information failed, and the error message was similar to "Incorrect string value:"\xF0\xA5... .. "Looking at this prompt, it should be caused by the character set not supporting a certain rare character.

The following is a scenario reproduced in a virtual machine:

step1. 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 '姓名',
 `nick` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '昵称',
 PRIMARY KEY (`id`)
) ENGINE=InnoDBAUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='会员卡';
Copy after login

step2. Insert rare characters (use sqlyog simulation) :

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

How to handle the failure of inserting rare characters into MySQL (picture)

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

How to handle the failure of inserting rare characters into MySQL (picture)

##We try to insert the king (in w

ordHold down alt and enter 152964). You can see that the insertion 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 '姓名' ;
Copy after login

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
Copy after login


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
Copy after login
step4. Insert the experiment again:

1. Set the character set first:

How to handle the failure of inserting rare characters into MySQL (picture)

2. Insert it again and you can see The insertion was successful.

How to handle the failure of inserting rare characters into MySQL (picture)

In the command line

QueryThere is no garbled code:

How to handle the failure of inserting rare characters into MySQL (picture)

That’s it. Yes, you can refer to it.

The above is the detailed content of How to handle the failure of inserting rare characters into MySQL (picture). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template