Solutions to MySQL Chinese problems

Source: Internet
Author: User

The following articles mainly describe the actual solutions to MySQL Chinese problems. We all know that encoding and garbled problem of MySQL Chinese problems have always plagued computer workers, it can be said that the problem of encoding and garbled code in MySQL is imminent.

MySQL 4.1 Character Set Support has two aspects: Character set and Collation ). The support for character sets is refined to four levels: server, database, table, and connection ). Our final goal is to make these four levels of conversions support Chinese encoding. The following uses utf8 as an example.

1. First, view the character set and sorting method of the system. If you want to view the character set and sorting method of a specific database, you should first select the database, MySQL> use databasename;

 
 
  1. MySQL> SHOW VARIABLES LIKE ''character_set_%'';   
  2. +--------------------------+----------------------------+   
  3. | Variable_name | Value |   
  4. +--------------------------+----------------------------+   
  5. | character_set_client | latin1 |   
  6. | character_set_connection | latin1 |   
  7. | character_set_database | latin1 |   
  8. | character_set_results | latin1 |   
  9. | character_set_server | latin1 |   
  10. | character_set_system | utf8 |   
  11. | character_sets_dir | /usr/share/MySQL/charsets/ |   
  12. +--------------------------+----------------------------+  

2 rows in set (0.00 sec)

 
 
  1. MySQL> SHOW VARIABLES LIKE ''collation_%'';   
  2. +----------------------+-------------------+   
  3. | Variable_name | Value |   
  4. +----------------------+-------------------+   
  5. | collation_connection | latin1_swedish_ci |   
  6. | collation_database | latin1_swedish_ci |   
  7. | collation_server | latin1_swedish_ci |   
  8. +----------------------+-------------------+  

3 rows in set (0.00 sec) the values listed above are the default values of the system. lanti does not support Chinese, so we change it to urf8.

Next we will modify the encoding methods of these four layers:

1. In the [MySQL Chinese problem] section of the my. cf file, set:

 
 
  1. default-character-set=utf8  

This statement sets character_set_client, character_set_connection _, and character_set_results to utf8, including encoding of the corresponding arrangement. Character_set_server is the system code, which does not need to be changed.

Of course, another way to change the character_set_client, character_set_connection _, and character_set_results encoding methods is:

 
 
  1. SET NAMES “UTF8”;  

It serves the following purposes:

 
 
  1. SET character_set_client = utf8;   
  2. SET character_set_results = utf8;   
  3. SET character_set_connection = utf8;  

2. Change the encoding method of the database.

 
 
  1. alter database databasename character set utf8;  

Last night, we changed the encoding of the database. Of course, there are 10 thousand other methods besides this method, that is, we can see that it is not utf8, you only need to change the corresponding encoding to utf8. Format: SET

 
 
  1. character_set_client = utf8;  

3. Whether you are web programming or desktop programming, the suffix must be included in the url of your database connection address:

? UseUnicode = true & characterEncoding = UTF-8/hibernate? UseUnicode = true & characterEncoding = UTF-8

Of course, if you use a MySQL management tool for Chinese problems, such as MySQL yog and MySQL manager, you can set your database settings directly in a visual environment. Of course, this is only limited to the database settings.

Edit recommendations]

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.