MySQL garbled problem of the tinkering

Source: Internet
Author: User
Tags mysql in

MySQL in the Ubuntu terminal under the problem of Chinese garbled:

First, learn to modify the MY.CNF configuration file without changing the database.

The main is to set

default-character-set=utf8

But the database restarts after the setup problem:/etc/init.d/mysql/restart

Terminal display failed to load Socket:/var/run/mysqld/mysqld.socket

Learn to view Error.log find real problems for MySQL unrecognized:default-character-Set=utf8

Baidu view try to change default-character-Set=utf8 to Character-set-server=utf8 However, there is no egg

Continue to report unable to identify Character-set-server=utf8;

At this time the egg has a strange ache, again Baidu Search, one of the more credible is to "Character-set-server=utf8" instead

Character_set_server=utf8, or the newspaper is not recognized. I'm more irritable.

Anger will change the configuration to the original file, but MySQL does not boot, again to view the log discovery is because there are programs running in the background,

PS aux |grep mysq* see exactly which turtle is running, kill the restart MySQL, found can

It takes 3 hours to get back to the point of origin.

He's two big uncle,

Later I saw a blog post: http://www.2cto.com/database/201108/101151.html

The main contents are as follows:

Workaround:

1. Set the code when setting up the databases:

' UTF8 '  'utf8_general_ci';

2. When the table is under construction

CREATE TABLE ' database_user ' (' ID ' varchar (' +default', ' UserID ' varchar (  (+default",) ENGINE=innodb default Charset=utf8;

With these 3 settings, there is no problem at all, that is, the same encoding format is used when building the library and building the table.
But if you have built libraries and tables, you can query them in the following ways.

1. View the default encoding format:

" %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: The previous 2 determines that the default encoding format can be set using the set names Utf8,set names GBK, and the effect of performing set names UTF8 is equivalent to setting the following:

Mysql> Show create database test; /* */ |+------------+---------------------------------------------------------------------------- --------------------   

Want to sleep, the format will be like this

Second, to avoid the import of data has the problem of Chinese garbled

1: Save the Data encoding format as Utf-8
Set the default encoding to UTF8:
Set names UTF8;
Setting the database db_name default to UTF8:

[SQL]View Plaincopy
    1. ALTER DATABASE ' db_name ' DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;

Set table tb_name default encoding is UTF8:

[SQL]View Plaincopy
    1. ALTER TABLE ' tb_name ' DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;

Import:

[SQL]View Plaincopy
    1. LOAD DATA LOCAL INFILE ' c:\\utf8.txt ' into TABLE yjdb;

2: Save data Encoding format to ANSI (i.e. GBK or GB2312)
Set the default encoding to GBK:
Set names GBK;
Set the database db_name default encoding to GBK:

[SQL]View Plaincopy
    1. ALTER DATABASE ' db_name ' DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

Set table Tb_name default encoding is GBK:

[SQL]View Plaincopy
    1. ALTER TABLE ' tb_name ' DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

Import:

[SQL]View Plaincopy
    1. LOAD DATA LOCAL INFILE ' c:\\gbk.txt ' into TABLE yjdb;

Note: 1. UTF8 do not import GBK,GBK do not import UTF8;
The display of UTF8 is not supported under 2.dos;
Third, solve the problem of garbled pages

Set the site encoding to Utf-8, which is compatible with all characters in the world.
If the site has been working for a long time, there are many old data, can not change the setting of Simplified Chinese, then it is recommended to set the page encoding GBK, GBK and GB2312 is the difference between: GBK can show more characters than GB2312, to show the simplified code of traditional characters, you can only use GBK.
1. Edit/etc/my.cnf, add Default_character_set=utf8 in [MySQL] section;
2. When writing the connection URL, add the useunicode=true&characterencoding=utf-8 parameter;
3. Add a "Set names UTF8" or "Set names GBK" instruction in the Web page code to tell MySQL to use the contents of the connection
UTF8 or GBK;

MySQL garbled problem of the tinkering

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.