Recent project use to MySQL, but suddenly appeared in Chinese garbled problem, tried a variety of solutions, finally solve garbled problem, summed up the solution, to meet the same problem of people a little reference.
Reasons for Chinese garbled characters
1. Problem with encoding set when installing MySQLServer
2. There is a problem with the encoding set when creating the database
3. There is a problem with the encoding set when creating the table
4. There is a problem with the encoding set set by the client
You can view the encoding set by command:
show variables like "%char%";
Common encoding Sets
The common coding UTF-8 in 1.java; GBK; gb2312;iso-8859-1;
2. Corresponding to the encoding utf8;gbk;gb2312;latin1 in the MySQL database
It is recommended to use the UTF8 format, UTF8 can be compatible with all characters in the world
MySQL Encoding set settings command
Note: If the content is not set through the My.ini configuration file, it is only valid in the current state and is invalidated when the database service is restarted. So if you want to not garbled only modify the My.ini file, the database encoding can be specified when the database is created UTF8
mysql设置编码命令SET character_set_client = utf8;SET character_set_connection = utf8;SET character_set_database = utf8;SET character_set_results = utf8;SET character_set_server = utf8;SET collation_connection = utf8_bin;SET collation_database = utf8_bin;SET collation_server = utf8_bin;
Set MySQLServer encoding
To set the MySQLServer encoding, you need to reinstall the MySQL service, as follows:
1. Open a command prompt (administrator), which may cause errors if you do not run as Administrator
2. Go to the Bin directory in the MySQL installation directory
3. Close the service
stop mysql
4. Uninstall
mysqld -remove
5. Modify the MySQL configuration file and locate the My.ini file in the MySQL installation directory
Modify the configuration file to include in the configuration file
character-set-server=utf8
Paste out my configuration file
[mysqld]##这里把路径改改为你的mysql安装目录basedir=D:\develeper\mysql5.6 datadir=D:\develeper\mysql5.6\data
My configuration file: Http://pan.baidu.com/s/1qW3hqri
6. Installation
mysqld -install
7. Start the service
start mysql
Specify character set when creating
1. Set the character set when creating a database
create database testdb default character set utf8;#建立数据库
2. Set the character set when creating a table
use testdb;create table student( id char(50) primary key, sname varchar(100) not null, age int )engine=innodb default charset=utf8;
3, the setting of the client character set, through the connection string to set.
jdbc:mysql://localhost:3306/testdb?characterEncoding=utf8
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
MySQL Chinese garbled solution