Detailed explanation of the perfect solution to MySQL Chinese garbled problem!

Source: Internet
Author: User

Summary:

The first point: the six are all dried into utf8.

Mysql> show variables like "% char % ";

+ -------------------------- + --------------- +
| 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 |
+ -------------------------- + ------------- +

Second, the self-built table must be utf8, and the fields include utf8.

Third, the SSH remote logon tool must also be UTF-8 encoded. The Secure Shell Client client can only use the system default GBK, so you must change the tool.


The above three guarantees that the garbled problem can be almost completely solved ~~~


Bytes ----------------------------------------------------------------------------------------------------------------------

MySQL causes Chinese garbled characters for the following reasons:
1. Problems with server settings, such as staying at Latin1
2. Table language setup problems (including character and collation)
3. Connection language settings for client programs (such as PHP)
We strongly recommend that you use utf8 !!!!
Utf8 is compatible with all characters in the world !!!!
1. Avoid Chinese garbled characters and check encoding methods when creating databases and tables
1. When creating a database: Create Database 'test'
Character Set 'utf8'
Collate 'utf8 _ general_ci ';
2. Create Table 'database _ user '(
'Id' varchar (40) not null default '',
'Userid' varchar (40) not null default '',
) Engine = InnoDB default charset = utf8;

After these three settings are complete, there will basically be no problem, that is, the same encoding format is used for both database creation and table creation.
However, if you have created a database and a table, you can query the table by using the following methods.
1. view the default encoding format:
Mysql> show variables like "% char % ";
+ -------------------------- + --------------- +
| Variable_name | value |
+ -------------------------- + --------------- +
| Character_set_client | GBK |
| Character_set_connection | GBK |
| Character_set_database | utf8 |
| Character_set_filesystem | binary |
| Character_set_results | GBK |
| Character_set_server | utf8 |
| Character_set_system | utf8 |
+ -------------------------- + ------------- +
Note: You can use set names utf8 and set names GBK to set the default encoding format;

The execution of set names utf8 is equivalent to the following:
Set character_set_client = 'utf8 ';
Set character_set_connection = 'utf8 ';
Set character_set_results = 'utf8 ';

2. view the encoding format of the test database:
Mysql> show create database test;
+ ------------ + Response +
| Database | create database |
+ ------------ + Response +
| Test | create database 'test '/*! 40100 default Character Set GBK */|
+ ------------ + Response +

3. view the encoding format of the yjdb database:
Mysql> show create table yjdb;
| Yjdb | create table 'yjdb '(
'Sn 'int (5) not null auto_increment,
'Type' varchar (10) Not null,
'Brc' varchar (6) Not null,
'Teller 'int (6) Not null,
'Telname' varchar (10) Not null,
'Date' int (10) Not null,
'Count' int (6) Not null,
'Back' int (10) Not null,
Primary Key ('sn '),
Unique key 'sn '('sn '),
Unique key 'sn _ 2' ('sn ')
) Engine = MyISAM auto_increment = 1826 default charset = GBK row_format = dynamic |

Ii. Avoid Chinese garbled characters in the imported data
1: Save the data encoding format as UTF-8
Set the default encoding to utf8:
Set names utf8;
Set database db_name to utf8 by default:
Alter database 'db _ name' default Character Set utf8 collate utf8_general_ci;
Set the default table tb_name encoding to utf8:
Alter table 'tb _ name' default Character Set utf8 collate utf8_general_ci;
Import:
Load data local infile 'C: \ utf8.txt 'into Table yjdb;
2: Save the data encoding format as ANSI (GBK or gb2312)
Set the default encoding to GBK:
Set names GBK;
Set the default database db_name encoding to GBK:
Alter database 'db _ name' default Character Set GBK collate gbk_chinese_ci;
Set the default table tb_name encoding to GBK:
Alter table 'tb _ name' default Character Set GBK collate gbk_chinese_ci;
Import:
Load data local infile 'C: \ gbk.txt 'into Table yjdb;

Note: 1. Do not import GBK for utf8, or do not import utf8 for GBK;
2. utf8 display is not supported in DOS;
3. Solve the Problem of garbled code on the webpage
 
Set the website encoding to UTF-8 to be compatible with all characters in the world.
If the website has been running for a long time and has a lot of old data, you cannot change the settings of Simplified Chinese, we recommend that you set the page encoding to GBK. The difference between GBK and gb2312 is: GBK can display more characters than gb2312. To display simplified Chinese characters, you can only use GBK.
1. edit/etc/My. CNF and add default_character_set = utf8 in the [MySQL] segment;
2. When writing a connection URL, add? Useunicode = true & characterencoding = UTF-8 parameter;
3. Add a "set names utf8" or "set names GBK" command in the webpage code to tell MySQL to use the connection content
Utf8 or GBK;

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.