MYSQL character set question _ MySQL

Source: Internet
Author: User
Tags mysql commands
MYSQL character set problem bitsCN.com has been plagued by mysql character sets some time ago. Let's summarize this knowledge today.
MySQL Character Set Support has two aspects:
Character set and Collation ).
The support for character sets is refined to four layers:
Server, database, table, and connection ).
1. default MySQL character set
MySQL can refine the character set designation to a database, a table, and a column.
However, traditional programs do not use such complex configurations when creating databases and data tables. they use the default configuration. So where does the default configuration come from?
(1) when compiling MySQL, a default character set is specified, which is latin1;
(2) when installing MySQL, you can specify a default character set in the configuration file (my. ini). If this character set is not specified, this value inherits from the one specified during compilation;
(3) when mysqld is started, you can specify a default character set in the command line parameters. if not, this value inherits from the configuration in the configuration file, character_set_server is set to the default character set;
(4) when creating a new database, unless explicitly specified, the character set of this database is set to character_set_server by default;
(5) when a database is selected, character_set_database is set to the default character set of the database;
(6) when creating a table in the database, the default character set of the table is set to character_set_database, which is the default character set of the database;
(7) when setting a column in a table, unless explicitly specified, the default character set in this column is the default character set of the table;
Simply put, if you do not modify anything, all the columns of all tables in all databases will be stored in latin1. However, if you install MySQL, you will generally choose multi-language support, that is, the installer automatically sets default_character_set in the configuration file as a UTF-8, which ensures that all columns of all tables in all databases are stored in UTF-8 by default.
2. view the default character set. (by default, the mysql character set is latin1 (ISO_8859_1)
Generally, you can run the following two commands to view the character set and sorting method of the system:
Mysql> show variables like character %;
+ -------------------------- + ----------------------------------- +
| Variable_name | Value |
+ -------------------------- + ----------------------------------- +
| Character_set_client | latin1 |
| Character_set_connection | latin1 |
| Character_set_database | latin1 |
| Character_set_filesystem | binary |
| Character_set_results | latin1 |
| Character_set_server | latin1 |
| Character_set_system | utf8 |
| Character_sets_dir | D: "mysql-5.0.37" share "charsets" |
+ -------------------------- + ----------------------------------- +
Mysql> show variables like collation _ %;
+ ---------------------- + ----------------- +
| Variable_name | Value |
+ ---------------------- + ----------------- +
| Collation_connection | utf8_general_ci |
| Collation_database | utf8_general_ci |
| Collation_server | utf8_general_ci |
+ ---------------------- + ----------------- +
3. modify the default character set
(1) the simplest modification method is to modify the character set key value in mysql's my. ini file,
For example, default-character-set = utf8
Character_set_server = utf8
After modification, restart the mysql service, service mysql restart
Use mysql> show variables like character %; check that the database encoding has been changed to utf8
+ -------------------------- + ----------------------------------- +
| Variable_name | Value |
+ -------------------------- + ----------------------------------- +
| Character_set_client | utf8 |
| Character_set_connection | utf8 |
| Character_set_database | utf8 |
| Character_set_filesystem | binary |
| Character_set_results | utf8 |
| Character_set_server | utf8 |
| Character_set_system | utf8 |
| Character_sets_dir | D: "mysql-5.0.37" share "charsets" |
+ -------------------------- + ----------------------------------- +
(2) another way to modify the character set is to use mysql commands.
Mysql> SET character_set_client = utf8;
Mysql> SET character_set_connection = utf8;
Mysql> SET character_set_database = utf8;
Mysql> SET character_set_results = utf8;
Mysql> SET character_set_server = utf8;
Mysql> SET collation_connection = utf8;
Mysql> SET collation_database = utf8;
Mysql> SET collation_server = utf8;
Generally, even if the default character set of the table is set to utf8 and the query is sent through the UTF-8 encoding, you will find that the database is still garbled. The problem lies in the connection layer. The solution is to execute the following statement before sending the query:
Set names utf8;
It is equivalent to the following three commands:
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;
Summary:
Therefore, what database version is used, whether it is 3.x, 4.0.x or 4.1.x, is not important to us. There are two important aspects:
(1) correctly set the database encoding. The character set for versions earlier than MySQL4.0 is always the default ISO8859-1, MySQL4.1 will let you choose when installing. If you are going to use UTF-8, you need to specify the UTF-8 when creating the database (you can also change it after the creation, 4.1 or later versions can also separately specify the character set of the table)
2) correctly set the database connection encoding. after setting the database encoding, you should specify the connection encoding when connecting to the database. for example, when using a jdbc connection, specify the connection as utf8 .bitsCN.com.
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.