Mysql Chinese garbled Solutions

Source: Internet
Author: User
If you have encountered this problem, let's not talk about the cause. In the cmd of the PC (or the CommandLine client installed in mysql, or the SecureCRT used for work), try it out. Go to the mysql environment and start from scratch. Assume that your client code is gbk or utf8. (This is not rigorous. How can this problem be assumed?

If you have encountered this problem, let's not talk about the cause. In the cmd of the PC (or the Command Line client installed in mysql, or the SecureCRT used for work), try it out. Go to the mysql environment and start from scratch. Assume that your client code is gbk or utf8. (This is not rigorous. How can this problem be assumed?

If you have encountered this problem, let's not talk about the cause. In the cmd of the PC (or the Command Line client installed in mysql, or the SecureCRT used for work), try it out. Go to the mysql environment and start from scratch. Assume that your client encoding is gbk or utf8 (this is not rigorous, how can we assume it? But generally, if it is not updated after installation, cmd is gbk encoding, after mysql is installed, the Command Line client is not installed. Remember to use CRT to check the encoding settings in Session Options. It is usually set to utf8. Execute some statements:

1. Set the character set of the encoding client, connection, and returned results to latin1,

2. Run the following command to check whether the characters are like this,

If your character_set_client, character_set_connection, and character_set_results are not latin1, you can set them to latin1, for example, character_set_client. The other two are the same, make sure that all three are latin1 (the first SQL statement actually does this ),

3. Create a database db_latin1 separately. Of course it is very simple. For testing, set the database encoding to latin1 during creation,

4. Create a table tab_latin1 under it, and set the character set to latin1. Here, no character is set. The database level has been set. Here, only one name field is created,

5. Insert some Chinese characters to the table. First, the cmd code of the local machine is gbk. Right-click the attribute and choose> option. You can see the current code page,

6. view the result

Check it out. Chinese characters are displayed normally ~~~

Okay, you don't want to know why I can't set it like that. :

We know that mysql is a client-server software. Each operation is performed by the client to send a request to the server, and some results may be returned. The inserted characters are converted in series. The client for editing has an encoding. For example, the command line on the PC side is gbk by default, and the new text file on the notepad on the PC side is ANSI by default. common text editors such as notepad ++, we may set the default encoding to utf8, that is, editing in the editor. What you see is actually an encoding.

1. After editing on the client, it is first converted to the character set corresponding to the client, that is, the character set indicated by the character_set_client variable printed above;

2. send a request to the database service. During the sending process, the connection character set is converted to the character set corresponding to the character_set_connection variable;

3. the character set stored in the database may be character_set_server, character_set_database, or table-level or column-level );

4. When the database receives the request, it executes the query to obtain the result and converts it to the results character set again, which is indicated by the character_set_results variable. The result is returned to the client;

5. The result is encoded according to the results character set. It is also important for the client tool that shows the result to support the encoding, which determines how the client tool decodes the result. If the result is utf8 encoded, it is returned to a client, but this client only has ANSI encoding, it certainly cannot be displayed normally. For example, if it returns to SecureCRT, the result is not displayed normally, however, CRT supports multiple types of encoding. If we manually convert it to utf8 encoding, it will be displayed as normal again. Therefore, this step is not strictly related to the client conditions, after all, this step does not exist after we know that the client is changed to a normal encoding or the results encoding is supported.

In the preceding step 3rd, the encoding used for converting the connection character set encoding to database storage is divided into several situations. Generally, when we install mysql, especially the 32-bit installation version, there is a select encoding step in the middle, most of them will select utf8 encoding, then the system may set a series of character set variables to utf8, such as character_set_server, character_set_connection, character_set_database, and so on. That is to say, the character_set_server variable is set before you start the mysql service. We can call it server-level encoding. Before creating a table, we must first create a database. When creating a database, we know that the encoding can be explicitly specified. For example, when I create the initial part, the latin1 character set can be explicitly specified or not specified. If not specified, it uses a server-level character set, namely character_set_server. Similarly, when creating a table, no encoding is specified. If no encoding is specified, database-level encoding and character_set_database are used, similarly, you can specify the encoding when creating column fields in the table. If no encoding is specified, the table-level character set is used. Therefore, this inheritance relationship exists here:

Character_set_server => character_set_database => character set in table (no such variable) => character set column (no such variable)

Creating a mysql table can be refined to these four layers, not all of which must be specified. By default, the character set at the upper level is used (the character verification rule is also like this, collation, which will be described later ).

Is it possible that character_set_server is not specified? If it is not specified anywhere, especially in non-installed versions, if you forget, mysql uses latin1 by default during compilation. To cope with this situation, in non-installed versions, you often need to manually configure the mysql configuration file When configuring mysql. ini:

The default character set used in the configuration file. Therefore, if character_set_server is specified, it will be used by default. Otherwise, it will be inherited in turn.

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.