Solution to mysql import garbled data

Source: Internet
Author: User
Tags mysql import
Many of you may encounter Chinese garbled characters when using commands or phpmyadmin to import data. Next I will introduce you to the solution for importing garbled data. If you need it, refer to it.

Many of you may encounter Chinese garbled characters when using commands or phpmyadmin to import data. Next I will introduce you to the solution for importing garbled data. If you need it, refer to it.

To avoid garbled characters during import, you only need to ensure the encoding of the data file and even the encoding during import. Note: it is not the same as the encoding. For example, the data file data. SQL, encoded as a UTF-8, uses the following command:

The Code is as follows:

Mysql-uroot-p -- default-character-set = utf8 db <data. SQL

The above method may not be effective, because we do not know mysql encoding, We can first view mysql Encoding

The Code is as follows:

Mysql> show variables like "% char % ";

+ -------------------------- + ------------------------------------------------------------------------------- +

| Variable_name | Value |

+ -------------------------- + ------------------------------------------------------------------------------- +

| Character_set_client | latin1 |

| Character_set_connection | latin1 |

| Character_set_database | utf8 |

| Character_set_filesystem | binary |

| Character_set_results | latin1 |

| Character_set_server | utf8 |

| Character_set_system | utf8 |

| Character_sets_dir |/usr/local/Percona-Server-5.1.57-rel12.8-233 -- x86_64/share // charsets/|

+ -------------------------- + ------------------------------------------------------------------------------- +

8 rows in set (0.01 sec)

Mysql> show variables like "% coll % ";

+ ---------------------- + ------------------- +

| Variable_name | Value |

+ ---------------------- + ------------------- +

| Collation_connection | latin1_swedish_ci |

| Collation_database | utf8_bin |

| Collation_server | utf8_bin |

+ ---------------------- + ------------------- +

Modify an SQL File

The Code is as follows:

SET collation_connection = utf8_bin;

SET character_set_client = utf8;

SET character_set_connection = utf8;

Then execute Import

The Code is as follows:

Mysql -- socket =/opt/mydata/my3306/my3306.sock-A <xxx. SQL is fine...

We also found a solution to program import garbled code, that is, add a set names UTF8 after mysql_connect to eliminate garbled Code. For GBK databases, use set names gbk. The Code is as follows:

The Code is as follows:
$ Mysql_mylink = mysql_connect ($ mysql_host, $ mysql_user, $ mysql_pass );
Mysql_query ("set names 'gbk '");

The database character set is UTF-8.

The connection statement uses this

The Code is as follows:
Mysql_query ("set names 'utf8 '");
Mysql_query ("set character set UTF8 ");
Mysql_query ("SET CHARACTER_SET_RESULTS = utf8 '");

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.