Hướng dẫn charset mysql
MySQL includes character set support that enables you to store data using a variety of character sets and perform comparisons according to a variety of collations. The default MySQL server character set and collation are Show This chapter discusses the following topics:
Character set issues affect not only data
storage, but also communication between client programs and the MySQL server. If you want the client program to communicate with the server using a character set different from the default, you'll need to indicate which one. For example, to use the
For more information about configuring character sets for application use and character set-related issues in client/server communication, see Section 10.5, “Configuring Application Character Set and Collation”, and Section 10.4, “Connection Character Sets and Collations”. A “connection” is what a client program makes when it connects to the server, to begin a session within which it interacts with the server. The client sends SQL statements, such as queries, over the session connection. The server sends responses, such as result sets or error messages, over the connection back to the client.
Connection Character Set and Collation System VariablesSeveral character set and collation system variables relate to a client's interaction with the server. Some of these have been mentioned in earlier sections:
Additional character set and collation system variables are involved in handling traffic for the connection between a client and the server. Every client has session-specific connection-related character set and collation system variables. These session system variable values are initialized at connect time, but can be changed within the session. Several questions about character set and collation handling for client connections can be answered in terms of system variables:
To see the values of the character set and collation system variables that apply to the current session, use this statement:
The following simpler statements also display the connection variables, but include other related variables as well. They can be useful to see all character set and collation system variables:
Clients can fine-tune the settings for these variables, or depend on the defaults (in which case, you can skip the rest of this section). If you do not use the defaults, you must change the character settings for each connection to the server. Impermissible Client Character Sets The
Attempting to use any of those character sets as the client character set produces an error:
The same error occurs if any of those character sets are used in the following contexts, all of which result in an attempt to set
Client Program Connection Character Set Configuration When a client connects to the server, it indicates which character set it wants to use for communication with the server. (Actually, the client indicates the default collation for that character set, from which the server can determine the character set.) The
server uses this information to set the If the server does not support the requested character set or collation, it falls back to using the server character set and collation to configure the connection. For additional detail about this fallback behavior, see Connection Character Set Error Handling. The mysql, mysqladmin, mysqlcheck, mysqlimport, and mysqlshow client programs determine the default character set to use as follows:
With the mysql client, to use a character set different from the default, you could explicitly execute a
If you are using the mysql client with auto-reconnect enabled (which is not recommended), it is preferable to use the
The When configuration client programs, you must also consider the environment within which they execute. See Section 10.5, “Configuring Application Character Set and Collation”. SQL Statements for Connection Character Set Configuration After a connection has been established, clients can change the character set and collation system
variables for the current session. These variables can be changed individually using
Example: Suppose that Connection Character Set Error HandlingAttempts to use an inappropriate connection character set or collation can produce an error, or cause the server to fall back to its default character set and collation for a given connection. This section describes problems that can occur when configuring the connection character set. These problems can occur when establishing a connection or when changing the character set within an established connection.
Connect-Time Error HandlingSome character sets cannot be used as the client character set; see Impermissible Client Character Sets. If you specify a character set that is valid but not permitted as a client character set, the server returns an error:
If you specify a character set that the client does not recognize, it produces an error:
If you specify a character set that the client recognizes but the server does not, the server falls back to its default character set and collation. Suppose that the server is configured to use
You can see that the connection system variables have been set to reflect a character set and collation of In this case, the client cannot use the character set that it wants because the server does not support it. The client must either be willing to use a different character set, or connect to a different server that supports the desired character set. The same problem occurs in a more subtle context: When the client tells the server to use a character set that the server recognizes, but the default collation for that character set on the client side is not known on the server side. This
occurs, for example, when a MySQL 8.0 client wants to connect to a MySQL 5.7 server using
Why does this occur?
After all,
In this case, the client can still use Runtime Error Handling Within an established connection, the client can request a change of connection character set and collation with Some character sets cannot be used as the client character set; see Impermissible Client Character Sets. If you specify a character set that is valid but not permitted as a client character set, the server returns an error:
If the server does not recognize the character set (or the collation), it produces an error:
Tip A client that wants to verify whether its requested character set was honored by the server can execute the following statement after connecting and checking that the result is the expected character set: |