Mysql converts Chinese latin1 encoding to gbk (or utf8) encoding.

Source: Internet
Author: User
Tags import database

Many projects use latin1 encoding to store Chinese data because engineers are not rigorous or have insufficient levels to choose wrong database encoding during initial construction. As the complexity of the project increases, various coding problems may occur, it can be said that the problem is endless. After repeated tests, I have come up with a way to convert the latin1 encoding database to gbk or u8 encoding.

1. latin1 to gbk

1. Export the database
Mysqldump -- default-character-set = latin1-h xxx. xxx-u xxx-P 3306-pxxxxxxxx db_name table_name>/usr/home/wanghui3/table_name. SQL

2. Modify table_name. SQL

/*! 40101 set names latin1 */; changed /*! 40101 set names gbk */;

Table creation statement
Default charset = latin1; change to default charset = gbk;

Note: different versions of mysqldump may be slightly different.

3. Import Database
Mysql-uroot-pxxxxxxxx db_name <table_name. SQL


2. latin1 to UTF-8

1. The first step remains unchanged.

2. Conversion Encoding
Iconv-t UTF-8-f gbk-c table_name. SQL> table_name_u8. SQL
Note: The text stored in latin1 is incorrect. The table stores the wrong latin1 encoding, but it is actually the correct gbk encoding. Therefore, the input encoding here should be gbk.

3. Modify table_name_u8. SQL and change latin1 to utf8.

4. Import the database
Mysql-uroot-pxxxxxxxx db_name <table_name_u8. SQL

========================================================== ====================

You can run the following two commands to view the character set and sorting method settings of the system:

Mysql> show variables like 'character % ';

Mysql> show variables like 'collation _ % ';

This article is from the "God learning programming" blog, please be sure to keep this source http://higgs.blog.51cto.com/6015929/995100

Related Article

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.