Problem solving: When mysql load, if there are Chinese characters, there will be garbled characters after importing

Problem scenario

Use loadcommands to import data to MySQL. Among them, the file has Chinese characters, the file encoding is UTF8, and the encoding of the table field is also set to UTF8, but the problem of garbled characters still occurs in the final import. The commands used are as follows:

LOAD DATA LOCAL INFILE '/data01/exp.txt' INTO TABLE test FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '' LINES TERMINATED BY '\n' IGNORE 0 LINES (ID,NAME)

Problem environment

software version
MySQL 5.6.40

problem causes

The main reason is that the encoding of the database is not UTF8. If the loadcommand does not specify an encoding, it will be imported using the encoding of the database. There may be many problems here, including encoding settings for fields, tables, database tables, and database servers. If there is a problem, please troubleshoot in order.

solution

1. Modify the encoding of the database

Bloggers here is mainly a database table is encoded latin1, the encoded database is modified here UTF8, SQLas follows:

ALTER DATABASE `test` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';

2. The load command specifies the encoding

Add character set utf8and then execute, the modified command is as follows:

LOAD DATA LOCAL INFILE '/data01/exp.txt' INTO TABLE test character set utf8 FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '' LINES TERMINATED BY '\n' IGNORE 0 LINES (ID,NAME)

result

The problem was resolved smoothly.

to sum up

After a problem occurs, in addition to solving the problem, you must also know the cause of the problem.

Ask for praise

If my article is helpful to everyone, you can click like or favorite at the bottom of the article;
if there is a good discussion, you can leave a message;
if you want to continue to view my future articles, you can click Follow
You can scan the following QR code to follow me 'S public account: Fengye Zhixuege, check out my latest share!
Insert picture description here
Bye bye

Guess you like

Origin blog.csdn.net/u013084266/article/details/114029901