A thorough solution to mysql Chinese garbled characters

Source: Internet
Author: User
MySQL causes Chinese garbled characters for the following reasons: 1. server configuration problems, such as the problem of setting the language families (including character and collation) in latin12.table. we strongly recommend that you use utf8 !!!! Utf8 is compatible with all characters in the world !!!! Insert Mysql in Linux

MySQL causes Chinese garbled characters for the following reasons: 1. server settings, such as latin1 2. table language setup issues (including character and collation) 3. we strongly recommend that you use utf8 !!!! Utf8 is compatible with all characters in the world !!!! Insert Mysql in Linux

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

How to insert Chinese characters into Mysql in Linux

Mysql-uroot-p press enter to enter the password

Enter mysql to view the status as follows:

Mysql cannot insert Chinese Characters

By default, latin1 is used on the client and server, so garbled characters are displayed.

Solution:

Mysql> user mydb;

Mysql> alter database mydb character set utf8 ;!

Another method is to directly modify the default Mysql configuration file.

In the debian environment, mysql cannot insert Chinese and Chinese garbled characters.

As mentioned above, I used a temporary method to change the character set settings of the database to display Chinese characters. However, it was later found that some systems could not succeed, for example, debian 7.0 ,, after searching for a long time, I finally found a solution suitable for the debian system. The test was successful in debian 7.0, and other methods such as modifying the client and mysql to add default-character-set = utf8 were found, applicable only to systems earlier than 5.5!

The ultimate solution:

Modify the mysql string In debian to utf8 (refer to the "rainysia" column)

1. log on to mysql (mysql-uroot-p) and check the current database character set (status ;)

Ii. view the current system version (lsb_release-)

Mysql cannot insert Chinese Characters

Iii. # vim/etc/mysql/my. cnf. (System before 5.5) add default-character-set = utf8 under [client]

Add default-character-set = utf8 under mysqld

Notice: if the error cannot be reported after modification, change default-character-set = utf8 to character_set_server = utf8. You do not need to add the default-character-set = utf8 parameter to the. client under mysqld.

Iv. # vim/etc/mysql/my. cnf. (After December 5.5:

[Client]
Default-character-set = utf8

[Mysqld]
Default-storage-engine = INNODB
Character-set-server = utf8
Collation-server = utf8_general_ci

5. Restart mysql (/etc/init. d/mysql stop/etc/init. d/mysql start)

6. After successful modification, go to mysql to view the character set (mysql> show variables like 'character _ set _ % ';)

Mysql cannot insert Chinese Characters

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;

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.