mysql 插入中文数据乱码解决方案

 

MySQL 4.1的字符集支持(Character Set Support)有两个方面:字符集(Character set)和排序方式(Collation)。对于字符集的支持细化到四个层次: 服务器(server),数据库(database),数据表(table)和连接(connection)。下面将分两部分,分别设置服务器编码和数据库、数据表与连接部分的编码,从而杜绝中文乱码的出现。

 

  一、服务器编码设置

 

服务器编码设置方法有二:

mysql时,其中会有一个步骤选择编码方式,此时选择utf8即可。如果不选择,默认的编码是latin1;

mysql之后,手动修改其配置文件,如下:

(1)修改 MySql安装目录下面的my.ini(MySQL Server Instance Configuration 文件), (需要关闭mysql服务),(如果本身没有该文件,自己添加一个亦可)my.ini文件进行如下修改: 

=================================================================================

修改my.ini前显示结果
===================================================================================

mysql> show variables like 'character%';
+----------------------------------------+------------------------------------------------+
| Variable_name | Value |
+----------------------------------------+------------------------------------------------+
| character_set_client  | gbk |
| character_set_connection | gbk |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results  | gbk  |
| character_set_server | latin1  |
| character_set_system  | utf8  |
| character_sets_dir  | D:\mysql-5.5.10\share\charsets\  |
+----------------------------------------+------------------------------------------------+
8 rows in set (0.02 sec)mysql> show variables like 'collation%';
+-------------------------------+--------------------------------+
| Variable_name | Value  |
+-------------------------------+--------------------------------+
| collation_connection | gbk_chinese_ci  |
| collation_database  | latin1_swedish_ci  |
| collation_server  | latin1_swedish_ci  |
+-------------------------------+--------------------------------+
3 rows in set (0.00 sec)


==============================================================================

关闭mysql服务,在mysql安装目录下,my.ini文件进行如下修改,没有则直接进行添加
==============================================================================

[mysqld]
        #修改服务器端默认字符编码格式为utf8
        character-set-server=utf8
        [client]
        #修改客户端默认字符编码格式为utf8
        default-character-set=utf8

==============================================================================

修改后,再次输入命令查看,显示结果如下

==============================================================================
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  | D:\mysql-5.5.10\share\charsets\  |
+----------------------------------------+------------------------------------------------+
8 rows in set (0.02 sec)mysql> show variables like 'collation%';
+-------------------------------+--------------------------------+
| Variable_name | Value  |
+-------------------------------+--------------------------------+
| collation_connection | utf8_general_ci  |
| collation_database  | utf8_general_ci   |
| collation_server  | utf8_general_ci   |
+-------------------------------+--------------------------------+
3 rows in set (0.00 sec)


================================================================================

(2) (对于已经创建了的数据库)修改data目录中相应数据库目录下的db.opt配置文件

    

default-character-set=utf8
        default-collation=utf8_general_ci

重启数据库,关闭控制台窗口重新登录数据库即可。

通过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 character_set_system=utf8;
set collation_connection=utf8_general_ci;
set collation_database=utf8_general_ci;
set collation_server=utf8_general_ci;

 

  二、数据库、数据表和连接部分的编码设置

 

2.1 设置数据库和数据表编码(默认的latin1)

 

修改用户密码

D:\mysql-advanced-5.6.11-win32\bin>mysqladmin -u root -p password(回车)
Enter password:(回车,用户root的默认密码为空)
New password: ******(回车,新密码111111)
Confirm new password: ******(回车)

 

连接与断开服务器

D:\mysql-advanced-5.6.11-win32\bin>mysqld(启动mysql服务程序,用来执行客户程序提交的任务)
D:\mysql-advanced-5.6.11-win32\bin>mysql -uroot -p(回车)
Enter password:(输入密码,用户root的默认密码为空)
……
mysql> quit;(回车)
Bye

 

创建数据库

mysql> create database mydatabase CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
Query OK, 1 row affected (0.00 sec)

(注:后面一段是显式的设置数据库的字符集,如果已经设置了全局的字符集,则可以省略)

 

显示数据库列表

mysql> show databases;
+-------------------------+
| Database       |
+-------------------------+
| information_schema |
| mydatabase  |
| mysql             |
| performance_schema |
| test                 |
+-------------------------+
5 rows in set (0.05 sec)

 

修改数据库字符集:

mysql> show create database test;
+-----------+------------------------------------------------------------------------+
| Database | Create Database                                                 |
+-----------+------------------------------------------------------------------------+
| test     | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+-----------+------------------------------------------------------------------------+
1 row in set (0.00 sec)
 
mysql> alter database test default character set utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.04 sec)
 
mysql> show create database test;
+-----------+----------------------------------------------------------------------+
| Database | Create Database                                               |
+-----------+----------------------------------------------------------------------+
| test     | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+-----------+------------------------------------------------------------------------+
1 row in set (0.00 sec)

使用某个数据库

mysql> use eosdb;
Database changed

 

显示数据表列表

mysql> show tables;
Empty set (0.00 sec)

 

创建数据表

mysql> CREATE TABLE `mysqlcode` (
-> `id` TINYINT( 255 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
-> `content` VARCHAR( 255 ) NOT NULL
-> ) TYPE = MYISAM CHARACTER SET gbk COLLATE gbk_chinese_ci;
Query OK, 0 rows affected, 1 warning (0.03 sec)

 

插入数据

mysql> insert into爱好者');

按回车,结果如下:

ERROR 1406 (22001): Data too long for column 'content' at row 1

解决方案:设置连接编码,设置如下:

mysql> SET character_set_client='gbk';
mysql> SET character_set_connection='gbk'
mysql> SET character_set_results='gbk'

设置好连接编码,下面便可以成功插入中文了:

mysql> insert into mysqlcode values(null,'java爱好者');
Query OK, 0 rows affected (0.02 sec)

其实,上面设置连接编码的三条命令可以简化为一条:

mysql> set names 'gbk';

 

查询数据表

mysql> select * from mysqlcode;
+------+-------------+
| id   | content     |
+------+-------------+
| 1    | java爱好着 |
+------+-------------+
1 row in set (0.00 sec)

insert或update或select连接操作时,前面加上:set names 'gbk'; 解决的中文问题。