Garbled Analysis of MySQL imported data and Solutions

Source: Internet
Author: User

There are many possible causes of garbled characters, but they are usually caused by inconsistent encoding. The Garbled text problem today is caused by inconsistent file encoding and database connection encoding, to add initialization data for batch execution, use the source command,

Root @ (none) 03:14:49> use test
Database changed
Root @ test 03:14:53> show create table rule \ G;
* *************************** 1. row ***************************
Table: Rule
Create Table: Create Table 'rule '(
'Id' bigint (20) not null auto_increment comment 'auto-incrementing primary key ',
'Name' varchar (64) not null comment' rule name, for example, sharding ',
'Status' tinyint (4) not null comment' status ',
Primary Key ('id ')
) Engine = InnoDB auto_increment = 2 default charset = GBK;
Root @ test 03:15:06> show variables like "% character % ";
+ -------------------------- + ------------------------------------ +
| Variable_name | value |
+ -------------------------- + ------------------------------------ +
| Character_set_client | GBK |
| Character_set_connection | GBK |
| Character_set_database | GBK |
| Character_set_filesystem | binary |
| Character_set_results | GBK |
| Character_set_server | GBK |
| Character_set_system | utf8 |
+ -------------------------- + ------------------------------------ +
7Rows in SET (0.00 Sec)

Root @ test 03:17:14>

We can see that the encoding method connecting the client is GBK. When I directly use

root@(none) 03:19:02>source a.sql;

Root @ test 03:20:45> select * from rule;
+ ---- + -------------------- + ---------------------------------- +

| ID | Name | status |

+ ---- + -------------------- + ---------------------------------- +
| 1 | | 0 |

| 2 | insects in the upper left corner | 0 |

+ ---- + -------------------- + ---------------------------------- +
2 rows in SET (0.00 Sec)

Root @ test 03:20:46>

Garbled, why? My client connection, the table encoding format is the same !!

Let's look at the encoding method of A. SQL:

$file a.sql
a.sql: UTF-8 Unicode text, with CRLF line terminators

The file is UTF-8 encoded. Run the following code:

root@test 02:35:36>set names utf8;
Query OK, 0 rows affected (0.00 sec)
Root @ test 02:35:57> truncate table rule;
Query OK, 0 rows affected (0.00 Sec)
Root @ test 02:36:04> source A. SQL;
Query OK, 1 row affected (0.00 Sec)

Query OK, 1 row affected (0.00 Sec)
Root @ test 02:36:08> select * from rule;
+ ---- + -------------------- + ---------------------------------- +
| ID | Name | status |
+ ---- + -------------------- + ---------------------------------- +
| 1 | | 0 |
| 2 | insects in the upper left corner | 0 |
+ ---- + -------------------- + ---------------------------------- +
2 rows in SET (0.00 Sec)

Root @ test 02:36:10> set names GBK;
Query OK, 0 rows affected (0.00 Sec)

Root @ test 02:36:23> select * from rule;
+ ---- + -------------- + -------------------------------- +
| ID | Name | status |
+ ---- + -------------- + -------------------------------- +
| 1 | irrigation | 0 |
| 2 | soy sauce | 0 |
+ ---- + -------------- + -------------------------------- +
2 rows in SET (0.00 Sec)

Root @ test 02:36:25>

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.