Linux under the MySQL Chinese garbled (Chinese question mark) solution

Source: Internet
Author: User
Tags socket

Linux will need to modify/ETC/MY.CNF, under [mysqld] Add default-character-set = utf8,[client] under Add default-character-set = UTF8, in [MySQL] Add Default-character-set=utf8 in field

Realize:
1. View the original code

  code is as follows copy code
mysql>" show variables like ' character% '; To perform the encoding display, you can see that the default is Latin1
+--------------------------+----------------------------+
| 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 |/usr/share/mysql/charsets/|
+--------------------------+----------------------------+ 

2. Modify the/etc/my.cnf and add Default-character-set=utf8 in the [Client],[mysqld],[mysql] field respectively. Note that the [mysqld] field differs from the [MySQL] field. No one has been feedback on the Internet.

# VIM/ETC/MY.CNF//Add Default-character-set=utf8

The code is as follows Copy Code

[Client]
Port = 3306
Socket =/var/lib/mysql/mysql.sock
Default-character-set=utf8

[Mysqld]
Port = 3306
Socket =/var/lib/mysql/mysql.sock
Character-set-server=utf8

[MySQL]
No-auto-rehash

Default-character-set=utf8 after the modification, the service MySQL restart restart the MySQL server, using show VARIABLES like ' character% '; found that the database code has been changed to UTF8

The code is as follows Copy Code
Mysql> Show variables like ' character% ';
+--------------------------+----------------------------------------+
| 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 | /usr/local/mysql/share/mysql/charsets/|
+--------------------------+----------------------------------------+

3. If the above are all modified and garbled, then the remaining problems must be on the connection connection layer. The workaround is to execute the following sentence (written directly at the front of the SQL file) before sending the query:

The code is as follows Copy Code

SET NAMES ' utf8′;

It is equivalent to the following three instructions:

The code is as follows Copy Code
SET character_set_client = UTF8;
SET character_set_results = UTF8;
SET character_set_connection = UTF8;

Appendix:
A friend's site is transferred to my side. MySQL is still Latin encoded when you import MySQL, so although you add Default-character-set=utf8 to the [Client],[mysqld],[mysql] field in the/etc/my.cnf file, follow the steps above, But the site part of Chinese is garbled. After you change the MySQL edit to UTF8 and then re-import the database, it's OK.
It is estimated that MySQL is the Latin encoding, when importing data, because MySQL is not recognized, the data is saved directly to the??? This garbled form of the

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.