Detailed description of SETNAMESutf8 in MySql (garbled principle)

Source: Internet
Author: User

Detailed description of SETNAMESutf8 in MySql (garbled principle)
Zookeeper

Not to mention, first

Analysis:

Before set names utf8,

Character_set_client | gbk

Character_set_connection | gbk

Character_set_results | gbk

After set names utf8,

Character_set_client | utf8

Character_set_connection | utf8

Character_set_results | utf8

For more information about character sets, see mysql Chinese manual.

Address: http://dev.mysql.com/doc/refman/5.1/zh/charset.html#charset-connection

The following is an excerpt:

Character Set and checking rule variables are also involved in client-to-server connection. Each client has a connection-related character set and checking rule variable.

Consider what a "connection" means when connecting to the server. The client sends SQL statements, such as queries, to the server through connections. The server sends a response to the client through a connection, such as a result set. For client connections, this will cause some problems concerning the character set and verification rules of connections. These problems can be solved through system variables:

· When the query leaves the client, which character set is used in the query?

The server uses the character_set_client variable as the character set used in the query sent by the client.

· What character set should the server convert to after receiving the query?

During conversion, the server uses character_set_connection and collation_connection system variables. It converts the query sent by the client from the character_set_client system variable to character_set_connection (unless the string text has a prefix like _ latin1 or _ utf8 ). Collation_connection is important for comparing text strings. It is not important to compare the column value strings because the column has a higher priority for checking rules.

· Which character set should the server convert to before sending result sets or returning error messages to the client?

The character_set_results variable indicates that the server returns the query result to the character set used by the client. Includes result data, such as column values and result metadata (such as column names ).

You can adjust the settings of these variables or rely on the default values (in this way, you can skip this chapter ).

Two statements affect the character set:

SET names'Charset_name'

SET CHARACTERSETCharset_name

SET names'X'Statement is equivalent to the three statements:

Mysql>SETcharacter_set_client =X;

Mysql>SETcharacter_set_results =X;

Mysql>SETcharacter_set_connection =X;

So:

Set names utf8 specifies that the encoding rule for character passing between the client and the server is UTF8.

Modify the default MySql Encoding

MySql uses Latin1 by default and does not support Chinese characters. to modify the default encoding, modify the my. ini file.

[Client] added default-character-set = utf8

[Mysql] added default-character-set = utf8

[Mysqld] added

Character_set_server = utf8

Init_connect = 'set NAMES utf8'

Note the cmd encoding when operating mysql with garbled characters in cmd.

Check the current code page. If the current code page is GBK, set names GBK is required to ensure no garbled characters.

Chcp 65001/936 modify the code page. Solve the garbled problem perfectly.

Zookeeper

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.