MySQL Chinese garbled solution

MySQL's character set support (Character Set Support) has two aspects: character set (Character set) and sorting method (Collation). The support for character sets is refined to four levels: server (server), database (database), data table (table) and connection (connection). The following will be divided into two parts, respectively setting the server code and the code of the database, data table and connection part, so as to prevent the appearance of Chinese garbled characters.

1. Server encoding settings

1. When installing MySQL, there will be a step to select the encoding method. At this time, you can choose utf-8 or gbk. If not selected, the default code is: latin1

2. After the installation is completed, manually modify the configuration file as follows:
(1) Modify my.ini (Windows) and my.cnf (Linux) under the MySQL installation directory, set :
[client]
   default-character-set=utf8
[mysqld]
   default-storage-engine=INNODB
   character-set-server=utf8
   collation-server=utf8_general_c

(2), add or modify the db under the directory of the corresponding database in the data directory .opt (in the directory where MySQL is installed) configuration file:
   default-character-set=utf8
   default-collation=utf8_general_ci

Two encoding settings for database, data table and connection parts

1. Set database and table encoding

To solve the garbled problem, you must first figure out what encoding the database and data tables use. If not specified, it will be the default latin1, and these three character sets, gb2312, gb, and utf8, should be used the most. How to specify the character set of the database and data table? The following uses utf8 as an example:

when creating a database:
such as:
mysql>CREATE DATABASE Message
mysql>CHARACTER SET 'utf8'
mysql>COLLATE 'utf8_general_ci';
when creating a data table:
such as:
mysql>USE Message;
mysql>CREATE TABLE person (
     personId int(11) not null primary key auto_increment,
     personName varchar(20) not null,
     personAccount varchar(20) not null,
     personPass varchar(20) not null,
     personDegree varchar(20) not null,
     personEmail varchar(30) not null,
     personAddr varchar(30) not null
   )ENGINE=MYISAM or ENGINE=InnoDB DEFAULT CHARSET=utf8;

2. Set the connection code

First
check whether the connection code is gb2312, gbk, utf8, check the default code format command: mysql> show variables like "%char%";
+- -------------------------+---------------+
| 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 | latin1|
+----------------------- ---+-------------+
Modify the default value of 'latinl' database: gbk, utf8, gb2312 Command:
mysql> SET character_set_client='utf8';
mysql> SET character_set_connection='utf8';
mysql> SET character_set_database='utf8';
mysql> SET character_set_results='utf8';
mysql> SET character_set_server='utf8';
mysql> SET character_set_system='utf8';
After modification, you can Check again whether the encoding format is successful:
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|
+----------------------------+-------------+
Run Command Prompt as Administrator , Restart MySQL and enter the command to close and open:
C:\Windows\systen32>net stop mysql
MySQL, the service is being stopped.
MySQL, the service has been stopped successfully.
C:\Windows\systen32>net start mysql
MySQL, the service is starting.
MySQL, the service has been started successfully.
After setting the connection code and restarting MySQL, you can now try to insert Chinese:
mysql>insert into person values(1003,'Zhanbo','zb','10005','University','[email protected]','Chaoyang Road No. 6');
Query whether the inserted data is successful:
mysql> select * from person ;

Three complete examples

1. Set the encoding when installing the MySQL server, modify the encoding in my.ini and dataTable.db to gbk, utf8, gb2312
2
  .Add CHARACTER SET 'utf8'
  COLLATE 'utf8_general_ci' after writing a statement to create a database ;


3. Check whether the connection code is: gbk, utf8, gb2312 format
4. Restart MySQL
5. Insert Chinese data, finished.      

4. You can use third-party software to automatically compile and transcode such as: Navicat for MySQL




Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327071369&siteId=291194637