MySQL modifies the character set for existing data

Source: Internet
Author: User

MySQL modifies character set problems for existing data

Ran in the production environment for a long time, found that the Mysqlclient connection character set is the default latin1, we have always thought is utf8, resulting in this misunderstanding, because in the intranet environment, we are source compiled MySQL, and specify the compilation option character set bit UTF8, At this point, MySQL is the default character connection is UTF8.

While in the external network, we are binary package installation, the default is Latin1, although in my.conf specified [client] [MySQL] [mysqld] in the character set is UTF8, This does not guarantee that the character set is UTF8 when the mysqlclient is connected. So it is best to establish a connection is the specified character set, this is guaranteed. such as:

// c connections need to be set before connection is established " UTF8 ");

Since data is connected to MySQL storage via laint1, fetching data also requires a LAINT1 connection. However, this method does not match the expected, and the other client connections are connected through the UTF8, which causes garbled and must be repaired to the data stored through the UTF8 connection.

Repair principle

set up a connection by latin1 the data out, and then set up the connection by UTF8 to save the data back .

Repair case

The table structure is defined as follows:

CREATE TABLE IF  not EXISTS' tbl_friend ' (' Uin ' )bigint( -) unsigned not NULL, ' Name 'varchar(255)CHARACTER SETUTF8 COLLATE Utf8_binDEFAULT NULL,  PRIMARY KEY(' Uin '),UNIQUE KEY' name ' (' name ')) ENGINE=InnoDBDEFAULTCHARSET=UTF8;

 select  CONCAT ( " update tbl_friend set ' Name ' =\   " , Name,    " \"   where uin=  , Uin,"  ;  ) from  tbl_friend into  outfile  '  

After executing the above statement in MySQL and then importing the file in MySQL /tmp/UpdateFriendName.sql , the data is fixed. It should be noted that the character set in the My.cnf [MySQL] segment is your modified character set or through MySQL- Default-character-set=utf8 </tmp/updatefriendname.sql Specifies the fixed character set.

After the repair can be passed in MySQL set names utf8/latin1 , and then look at the select Name from tbl_friend limit 10; changes,

Obstacles that may be encountered

If you encounter the MySQL server is running and the--SECURE-FILE-PRIV option so it cannot execute this statement need to be in my Add the [mysqld] in the. conf secure_file_priv = and restart MySQL.

MySQL modifies the character set for existing data

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.