To sum up the problem of MYSQL encoding and conversion, latin1 to utf8

Source: Internet
Author: User
Tags php website

Black noodle

Yesterday I met a PHP website where the database was MYSQL5.1. After the phpmyadmin management of the website, I found that all Chinese characters in the table were garbled. After export, the local import was garbled, you cannot view the required information. garbled characters are generally known as encoding problems. You can check that the target library is encoded as latin1. It is estimated that the website program is GBK... Direct backup download, download to local need to convert the encoding, but unfortunately did not do it before, search for a bunch of materials online to test... I have summarized these types of information to my friends who need it, so I don't need to search around.

I:

1. Back up the database

Mysqldump -- default-character-set = latin1 -- create-options = false -- set-charset = false-u root-p Database Name> E: ack. SQL

2. Create a new database

Create database name character set utf8 COLLATE utf8_general_ci;

3. Import Data

Mysql-u root-p -- default-character-set = gbk database name <E: ack. SQL

 

II:

Step 1 run the command: mysqldump -- opt-hlocalhost-uroot-p *** -- default-character-set = lantin1 dbname>/usr/local/dbname. SQL

Step 2 Change CHARSET = latin1 of the create table statement in the dbname. SQL file to CHARSET = utf8

Step 3 add a set names utf8 before the insert statement in the dbname. SQL file;

Step 4. Set dbname. SQL transcoding is in UTF-8 format, and UltraEditor is recommended, which can be used directly to convert-> ASCII to UTF-8 (Unicode edit), or to save the file as UTF-8 (without BOM) Format

Step 5 run the following command: mysql-hlocalhost-uroot-p *** -- default-character-set = utf8 new_dbname </usr/local/dbname. SQL

Conclusion: This method is fatal when there are a large number of Chinese characters and other special characters in the data, it is likely that in [Step 5], an error will be reported, resulting in the failure to import data normally, if the database is large, you can perform the preceding steps for each table.

 

III:

In order to solve the problem mentioned in the summary in the first method, I had to search for information on the Internet for a day to work out a safer solution.

Step 1: export the table structure of the data table to be exported (you can use Phpmyadmin, mysqldump, and so on. If you don't need to talk about it, change CHARSET = latin1 of the create table statement to CHARSET = utf8, execute the create table statement in the newdbname of the target database to create the table structure, and then start exporting-importing data

Step 2: Go to the mysql command line and choose mysql-hlocalhost-uroot-p *** dbname

Step 3: Execute SQL select * from tbname into outfile/usr/local/tbname. SQL;

Step 4. Set tbname. SQL transcoding is in UTF-8 format, and UltraEditor is recommended, which can be used directly to convert-> ASCII to UTF-8 (Unicode edit), or to save the file as UTF-8 (without BOM) Format

Step 5: Execute the statement set character_set_database = utf8 In the mysql command line. Note: set the mysql environment variable so that mysql will explain the file content in the form of utf8 when reading the SQL file in the next step.

Step 6 run the load data infile tbname. SQL into table newdbname. tbname statement in the mysql command line;

Note: Do not forget step 4.

In the second method, all data is imported normally, and the format conversion is successful without garbled characters.

 

IV:

Mysql Data conversion code
[Root @ piglet home] # mysqldump-u root-p -- default-character-set = latin1 -- set-charset = utf8 -- skip-opt aiyomama_cn>/home/sites/savemoney. SQL
Enter password:

 

V:

The following is a latin1 garbled file. Therefore, the local machine also sets up the Mysql5.0 environment.
2. Create an empty database named UTF8 and use the SQL file to import the database.
3. Use phpmyadmin to export the code to UTF8.
4. replace all default charset = latin1 In the exported SQL file with DEFAULT CHARSET = utf8
5. Delete the original latin1-encoded database and import the UTF8-encoded SQL file.
Completed :)

Important points:
Run PHPMYADMIN to open the database.
All the tables and fields in the table are still LATIN1. It doesn't matter. Continue to step 1.
After completing step 1, you can use a text editor to open the SQL file and view it in UTF8 format.
At this time, the table and field content is UTF8!

I have tried all the above five methods. It may be because the database is too large. Some methods are not good at all. Some methods can be converted to only half of them, and errors can be stopped, finally, I found a small PHP script file for encoding and conversion through unremitting efforts. After using the PHP script file, all the libraries were converted normally. It seems that it was still convenient to use the tool. It was not solved after I had been busy for half a day... However, no matter what the problem is, search engines can usually help you solve the problem, as long as you are willing to spend time solving the problem yourself.

 

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.