MySQL Chinese garbled

Source: Internet
Author: User
Tags mysql client

[MySQL]

# Set the MySQL client default character set
#default-character-set=utf8
Default-character-set=gbk

[Mysqld]

#设置3306端口

Port = 3306

# set up the MySQL installation directory

Basedir=d:\install\mysql\mysql-5.6.27-winx64

# Set up a storage directory for MySQL database data

Datadir=d:\install\mysql\mysql-5.6.27-winx64\data

# Maximum number of connections allowed

max_connections=200

init_connect= ' SET collation_connection = Utf8_unicode_ci '
init_connect= ' SET NAMES UTF8 '

# The character set used by the service side defaults to the 8-bit encoded latin1 character set
Character-set-server=utf8
Collation-server=utf8_unicode_ci
Skip-character-set-client-handshake

# The default storage engine that will be used when creating a new table

Default-storage-engine=innodb

[Client]
Port = 3306
Default_character_set=utf8

Here are a few ideas to explain

First, MySQL's character set problem is mainly two concepts, one is character sets, one is collations, the former is character content
and coding, which are some of the rules for comparing operations on the former. These two sets of parameters can be four levels in a DB instance, a single database, a table, a column, and so on
do not specify.

For users, it is generally recommended to use UTF8 encoding to store data. To solve the garbled problem, is not only the MySQL data storage problem, but also
is related to how the user's program files are encoded, the user program, and how the MySQL database is connected.

First of all, MySQL has the default character set, which is determined when the installation, when compiling MySQL can be default_charset=
UTF8 and default_collation=utf8_general_ci These two parameters (MySQL5.5 version, version 5.1 with--with-charset=
UTF8--with-collation=utf8_general_ci) to specify the default character set as UTF8, which is also the most permanent way, after this designation,
The encoding of the client connecting to the database is also UTF8 by default, and the application does not require any processing.

Unfortunately, many people do not specify these two parameters when compiling and installing MySQL, most of them are installed by binary program, that
The default character set for MySQL is latin1. At this point, we can still specify MySQL's default character set, which is increased by my.cnf file
Two parameters:
1. Under [mysqld] Add
Default-character-set=utf8 (MySQL version 5.5 added Character-set-server=utf8)
2. Add under [client]
Default-character-set=utf8
So we don't have to specifically specify the UTF8 character set when we build the database. This writing in the configuration file solves the problem of data storage and comparison.
, but the connection to the client is not useful, the client usually need to specify UTF8 mode connection in order to avoid garbled. Which is the general set of Legends.
Names command. In fact, the set names UTF8 command corresponds to the following server-side commands:
SET character_set_client = UTF8;
SET character_set_results = UTF8;
SET character_set_connection = UTF8;
But thisthree parameters can not be written in the configuration file MY.CNF. Can only be modified dynamically by the set command.。 What we need is to write a job in the config file.
The way of Yong-yi. At this time, whether there is a solution to the problem on the service side, the idea is to set in the Init_connect. This command is in every
When a normal user is connected, the connection character set is triggered by adding the following line to the [mysqld] section:
Under [Mysqld], add:
Init_connect = ' SET NAMES UTF8 '
Summarize:
1, preferred to compile the installation of MySQL to specify two parameters using UTF8 encoding.
2, Zeshun in the configuration file my.cnf or My.ini set two parameters, and set the Init_connect parameters.
3, third in the configuration file my.cnf or My.ini set two parameters, while the client connection to specify the set names command.
4.adding the Default-character-set parameter to the client and server in the config file my.cnf is easy to manage.

MySQL 5.5 version to solve the problem of Chinese garbled in My.ini [MYSQLD] Item can not be written Default-character-set=utf8

To see how to solve the garbled problem:

In MySQL, the default character set is Latin1,

If you want to set the character set to Uft-8, you can add the following settings in the My.cnf file:

[client]

1 default-character-set=utf8

[mysqld]

1 default-character-set=utf8
2 skip-character-set-client-handshake #此处是忽略客户端的字符集,使用服务器的设置

(The Skip statement and the default in the client select one)

Of course you can set up a database in a defined character set:

1 createdatabase db_name default character setutf8 callate utf8-general-ci;

You can also set it before linking the database:

1 setnames ‘utf8‘

Focus on the Scarlet Letter, add the skip-character-set-client-handshake,client inside will not add the default

01 show variables like‘characte%‘;
02 +--------------------------+----------------------------+
03 | Variable_name | Value |
04 +--------------------------+----------------------------+
05 | character_set_client | utf8 |
06 | character_set_connection | utf8 |
07 | character_set_database | utf8 |
08 | character_set_filesystem | binary|
09 | character_set_results | utf8 |
10 | character_set_server | utf8 |
11 | character_set_system | utf8 |
12 | character_sets_dir | /usr/share/mysql/charsets/ |
13 +--------------------------+----------------------------+

However, according to the above configuration, MySQL will not be able to boot, this is why? Look down:

In the 5.1 version, in order to solve the Chinese garbled problem set the default character set is UTF8, in the My.ini in the [MySQL] and [mysqld] entries are written:

1 default-character-set=utf8

In the 5.5 version, [MySQL] entries can be written in this way, [mysqld] in this write in the start MySQL service will have 1067 errors, after the query found that this must be written:

1 character-set-server=utf8

5.1 and 5.5 are now known to have such a difference and are seen in the auto-generated My.ini file from the 5.5 installation version. Further experimentation is required to configure the 5.5 version that is not installed.

MySQL Chinese garbled

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.