Deep MySQL Character set settings

Source: Internet
Author: User
Tags mysql client

InMySQLClient andMySQLThere is a character set converter between the service side. Character_set_client =>GBK: The converter knows that the client is sending a GBK format encoding character_set_connection=> GBK: Convert the data sent from the client to the GBK format character_set_results =>GBK: Note:The above three character sets can be set uniformly using set names GBK example: Create TABLE Test (name varchar () not NULL) charset UTF8; #这里的utf8表示服务器端的字符编码 First, Insert a data into the data table test inert into test values (' test '); then the data "test" is stored in the database in the "UTF8" format:

First, through the MySQL client, the data sent to the MySQL server, after the character set converter, because the character_set_connection value is GBK, so the client sends the data to GBK format, followed by, When the character set converter sends the data to the server, it discovers that the server is storing the data in UTF8, so the data is automatically converted from GBK to UTF8 format within it.   when will there be garbled characters?
    1. The client's data format does not match the character_set_client of the Declaration
Through the header (' Content-type:text/html;charset=utf8 '), the client's data is transferred to the UTF8 format, when the data passes through the "character Set Converter", due to CHARACTER_SET_CLIENT=GBK, The character_set_connection is also equal to GBK, so the data transmitted from the client (in fact, the UTF8 format) is not converted into a format.   However, the character set converter when the data sent to the server, the server to find the format is UTF8, so the current data as a GBK format to deal with, and thus turn into UTF8 (but this step is actually wrong ...)    )。 2. When the result does not match the client page

The format of the returned result is set to UTF8, but the client accepts a format of GBK, which can cause garbled   With the show character set syntax, you can display all available character sets Latin character sets

Note: The MaxLen column displays the maximum number of bytes used to store one character. UTF8 Character Set

GBK Character Set

  when will data be lost? Compare the above three graphs to know that in each character set, the maximum number of bytes used to store one character is different, utf8 maximum, Latin minimum. Therefore, when the character set converter, if handled improperly, will result in data loss, and is irreversible. Like what: will beWhen the value of character_set_connection is changed to Lantin

The GBK data sent from the client will be converted to the LANTIN1 format because the data in the GBK format takes up more characters, resulting in data loss

Summary:
    1. Character_set_client and character_set_results are generally consistent because one represents the data format that the client sends, and the other represents the data format that the client accepts
    2. To avoid data loss, the character encoding of the character_set_connection is greater than the character_set_client character encoding

Deep MySQL Character set settings

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.