Detailed explanation of mysql database Chinese garbled issues, detailed explanation of mysql
When a piece of data with Chinese characters is inserted into the MySQL databaseInsert into employee values (null, 'zhang san', 'female ', '2017-10-08', '2017-11-12 ', 'sales', 1995,' is a good employee! '); When garbled characters appear, you can use the statementshow variables like 'character%'To view the shard set of the current database.
We can see that MySQL uses character sets in six ways: client, connection, database, results, server, and system. It is related to the server: database, server, system (which can never be modified, or UTF-8); it is related to the client: connection, client, and results.
| Client |
The character set used by the client. |
| Connection |
Set the type of character set used to connect to the database. If the program does not specify the character set type used to connect to the database, it is set according to the default character set on the server. |
| Database |
Set the character set used by a database in the database server. If it is not specified during database creation, the character set specified during Server installation will be used. |
| Results |
Set the character set used by the database to return data to the client. If not specified, use the default Character Set of the server. |
| Server |
Set the default character set specified during Server installation. |
| System |
Set the character set used by the database system. |
Understand the above information we analyze the cause of garbled characters, the problem lies in the current CMD client window, because the current CMD client input using GBK encoding, and the database encoding format for UTF-8, inconsistent encoding causes garbled characters. However, the encoding format of the current CMD client cannot be modified. Therefore, you can only modify the encoding set of connection, client, and results to inform the server that the currently inserted data adopts GBK encoding, while the server database uses UTF-8 encoding, it can identify the GBK encoding data of the Notification Server and automatically convert it to the UTF-8 for storage. You can use the following statement to quickly set the consumer set related to the client:
After setting, you can solve the problem of data insertion or garbled data on the client. However, we will immediately find that this setting is only valid in the current window, when the window is closed and the CMD client is re-opened, garbled characters may occur. How can we make a permanent setting? There is a my. ini configuration file under the MySQL installation directory. By modifying this configuration file, you can solve the garbled problem once and for all. In this configuration file, [mysql] is related to client configuration, and [mysqld] is related to server configuration. The default configuration is as follows:
- Using [mysql]
- Optional default-character-set = utf8
- Commit [mysqld]
- Export character-set-server = utf8
In this case, you only need to change the default encoding default-character-set = utf8 to default-character-set = gbk and restart the MySQL service.
Summary
The above section describes the mysql database Chinese Garbled text. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!