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