今天系统升级,开发、测试说本地环境、测试环境都没有问题,都用ssh client升的,演示环境报错了Data too long for column。
仔细检查了下,表字符集都是utf-8,目测长度肯定没超过。仔细查了下,问题出在character_set_client上。如下所示:
mysql> show create table t_char;
+--------+-----------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+-----------------------------------------------------------------------------------------------------------------------------+
| t_char | CREATE TABLE `t_char` (
`id` int(11) DEFAULT NULL,
`name` varchar(32) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------+-----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show variables like '%char%';
+--------------------------+------------------------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/Percona-Server-5.6.30-rel76.3-Linux.x86_64.ssl101/share/charsets/ |
+--------------------------+------------------------------------------------------------------------------+
8 rows in set (0.00 sec)
mysql> insert into t_char values(1,'测试测试测试测试测试测试测试测试');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t_char values(1,'测试测试测试测试测试测试测试测试测试');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t_char values(1,'测试测试测试测试测试测试测试测试测试测试');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t_char values(1,'测试测试测试测试测试测试测试测试测试测试测试');
Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t_char values(1,'测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试');--32个字符,肯定没有问题。
Query OK, 1 row affected (0.01 sec)
mysql> insert into t_char values(1,'测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试');--34,必然的。
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> set character_set_client=gbk;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t_char values(1,'测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> insert into t_char values(1,'测试测试测试测试测试测试测试测试测试测试测试');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> insert into t_char values(1,'测试测试测试测试测试测试测试测试');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t_char values(1,'测试测试测试测试测试测试测试测试1');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t_char values(1,'测试测试测试测试测试测试测试测试测试');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t_char values(1,'测试测试测试测试测试测试测试测试测试测试');--20个字符,
Query OK, 1 row affected (0.00 sec)
mysql> insert into t_char values(1,'测试测试测试测试测试测试测试测试测试测试测试'); --22个字符
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> insert into t_char values(1,'测试测试测试测试测试测试测试测试测试测试测');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t_char values(1,'测试测试测试测试测试测试测试测试测试测试测1'); --20个中文,一个数字
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into t_char values(1,'测试测试测试测试测试测试测试测试测试测试测11'); --20个中文,两个数字
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> set character_set_client=latin1;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 3188579
Current database: test
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t_char values(1,'测试测试测试测试测试测试测试测试测试测试测1');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> insert into t_char values(1,'测试测试测试测试测试');--10个字符
Query OK, 1 row affected (0.01 sec)
mysql> insert into t_char values(1,'测试测试测试测试测试测'); --11个字符
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> insert into t_char values(1,'aaaaaaaaaa');
Query OK, 1 row affected (0.01 sec)
==========================
再看表时gbk字符的情况:
mysql> create table test_g(id int,name varchar(32)) charset=gbk;
mysql> set character_set_client=gbk;
Query OK, 0 rows affected (0.06 sec)
mysql> insert into test_g values(1,'测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试');--30个字符
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> insert into test_g values(1,'测试测试测试测试测试测试测试测试测试测试测试测试测试');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> insert into test_g values(1,'测试测试测试测试测试测试测试测试测');
ERROR 1366 (HY000): Incorrect string value: '\x8B' for column 'name' at row 1
mysql> insert into test_g values(1,'测试测试测试测试');
Query OK, 1 row affected (0.30 sec)
mysql> select * from test_g;
+------+--------------------------------------+
| id | name |
+------+--------------------------------------+
| 1 | 娴嬭瘯娴嬭瘯娴嬭瘯娴嬭瘯 |
+------+--------------------------------------+
1 row in set (0.51 sec)
mysql> insert into test_g values(1,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
Query OK, 1 row affected (0.01 sec)
mysql> insert into test_g values(1,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> set character_set_client=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test_g values(1,'测试测试测试测试');
Query OK, 1 row affected (0.06 sec)
mysql> select * from test_g;
+------+--------------------------------------+
| id | name |
+------+--------------------------------------+
| 1 | 娴嬭瘯娴嬭瘯娴嬭瘯娴嬭瘯 |
| 1 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
| 1 | 测试测试测试测试 |
+------+--------------------------------------+
3 rows in set (0.00 sec)
mysql> insert into test_g values(1,'测试测试测试测试测试测试测试测试测');
Query OK, 1 row affected (0.39 sec)
mysql> select * from g;
ERROR 1146 (42S02): Table 'test.g' doesn't exist
mysql> select * from test_g;
+------+-----------------------------------------------------+
| id | name |
+------+-----------------------------------------------------+
| 1 | 娴嬭瘯娴嬭瘯娴嬭瘯娴嬭瘯 |
| 1 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
| 1 | 测试测试测试测试 |
| 1 | 测试测试测试测试测试测试测试测试测 |
+------+-----------------------------------------------------+
4 rows in set (0.09 sec)
mysql> insert into test_g values(1,'测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试'); --32个字符
Query OK, 1 row affected (0.00 sec)
mysql> insert into test_g values(1,'测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测'); --33个字符
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> select * from test_g;
+------+--------------------------------------------------------------------------------------------------+
| id | name |
+------+--------------------------------------------------------------------------------------------------+
| 1 | 娴嬭瘯娴嬭瘯娴嬭瘯娴嬭瘯 |
| 1 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
| 1 | 测试测试测试测试 |
| 1 | 测试测试测试测试测试测试测试测试测 |
| 1 | 测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试 |
+------+--------------------------------------------------------------------------------------------------+
5 rows in set (0.00 sec)
实际结果看起来比较怪异,不管建表用的是gbk还是utf8,insert时,Mysql对于客户端连接的字符集好像只有utf-8是正常识别的,gbk好像总是有问题。数据库gbk/客户端gbk也进行了编码转化导致乱码。经仔细查询相关资料原因:
存数据时:
数据库在存放项目数据的时候会先用UTF-8格式将数据解码成字节码,然后再将解码后的字节码重新使用GBK编码存放到数据库中。
取数据时:
在从数据库中取数据的时候,数据库会先将数据库中的数据按GBK格式解码成字节码,然后再将解码后的字节码重新按UTF-8格式编码数据,最后再将数据返回给客户端。