Linux环境MySQL数据库insert记录中文乱码1366问题

Linux环境MySQL数据库insert记录中文乱码问题

  • 出错场景
  • 问题分析
  • 解决步骤
  • 参考资料

出错场景

环境: Linux CentOS 7.4.1708 (Core) 、MySQL 5.7.24, for linux-glibc2.12 (x86_64)
数据库pcsdb建库时指定gbk字符集,insert SQL语句中列的值若含有中文,执行insert语句时报1366错误。

ERROR 1366 (HY000): Incorrect string value: ‘\xB2\xD9\xD7\xF7\xC1\xF7…’ for column ‘seq_desc’ at row 1
insert失败

MYSQL 相关SQL语句

-- database
CREATE DATABASE `pcsdb` /*!40100 DEFAULT CHARACTER SET gbk */

-- table
CREATE TABLE `pcs_seq` (
  `seq_id` int(2) NOT NULL,
  `seq_type` char(2) NOT NULL,
  `seq_no` int(6) NOT NULL,
  `seq_desc` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`seq_id`,`seq_type`)
);

-- insert
insert into pcs_seq (`seq_id`, `seq_type`, `seq_no`, `seq_desc`)
values (1, '01', 1, '操作流水号');

问题分析

根据提示信息,断定为insert语句的中文字符集与数据库字符集设置不一致导致。
登录数据库,执行status\s查看数据库状态信息,注意字符集设置信息。

mysql> \s
--------------
mysql  Ver 14.14 Distrib 5.7.24, for linux-glibc2.12 (x86_64) using  EditLine wrapper

Connection id:          8
Current database:       pcsdb
Current user:           pcsdb@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.7.24-log MySQL Community Server (GPL)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8
Db     characterset:    gbk
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /tmp/mysql.sock
Uptime:                 36 min 48 sec

Threads: 3  Questions: 651  Slow queries: 1  Opens: 704  Flush tables: 1  Open tables: 265  Queries per second avg: 0.294
--------------

mysql> show variables like '%char%';
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8                             |
| character_set_connection | utf8                             |
| character_set_database   | gbk                              |
| character_set_filesystem | binary                           |
| character_set_results    | utf8                             |
| character_set_server     | utf8                             |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

show variables like ‘%char%’ 简要介绍

序号 字符集设置 简单描述
1 character_set_client 客户端使用的字符集
2 character_set_connection 连接数据库字符集,若不指定 则使用 服务器端默认字符集
3 character_set_database 数据库服务器中的某个库使用的字符集,若建库时不指定,则使用MYSQL服务器安装时指定的字符集
4 character_set_results 返给客服端返回记录时使用的字符集,若不指定 则使用 服务器端默认字符集
5 character_set_server MYSQL服务器安装时指定的字符集,建议由系统自定定义

以下3个系统设置不会影响乱码等问题,基本上不需关心。

序号 字符集设置 简单描述
1 character_set_filesystem 文件系统的字符集,binary 不做任何转换
2 character_set_system MYSQL数据库系统使用的字符集
3 character_sets_dir 数据库字符集安装目录

更改以上字符集直接 set character_set_xxxx = “gbk”; (xxxx写以上的变量名)

解决步骤

  1. 查看建库时指定的字符集
    show create database dbname1 命令
mysql> show create database pcsdb;
+----------+---------------------------------------------------------------+
| Database | Create Database                                               |
+----------+---------------------------------------------------------------+
| pcsdb    | CREATE DATABASE `pcsdb` /*!40100 DEFAULT CHARACTER SET gbk */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)
  1. 查看建表时指定的字符集
    show create table tblname1 命令
mysql> show create table pcs_seq;
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                                    |
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| pcs_seq | CREATE TABLE `pcs_seq` (
 `seq_id` int(2) NOT NULL,
 `seq_type` char(2) NOT NULL,
 `seq_no` int(6) NOT NULL,
 `seq_desc` varchar(100) DEFAULT NULL,
 PRIMARY KEY (`seq_id`,`seq_type`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show full columns from pcs_seq;
+----------+--------------+----------------+------+-----+---------+-------+----------------------+---------+
| Field    | Type         | Collation      | Null | Key | Default | Extra | Privileges           | Comment |
+----------+--------------+----------------+------+-----+---------+-------+----------------------+---------+
| seq_id   | int(2)       | NULL           | NO   | PRI | NULL    |       | select,insert,update |         |
| seq_type | char(2)      | gbk_chinese_ci | NO   | PRI | NULL    |       | select,insert,update |         |
| seq_no   | int(6)       | NULL           | NO   |     | NULL    |       | select,insert,update |         |
| seq_desc | varchar(100) | gbk_chinese_ci | YES  |     | NULL    |       | select,insert,update |         |
+----------+--------------+----------------+------+-----+---------+-------+----------------------+---------+
4 rows in set (0.00 sec)

  1. 修改客户端连接 的字符集
  • 临时解决方案 使用SET NAMES gbk
  • 永久解决方案 修改my.cnf2 文件,重新启动MySQL
    vi my.cnf
    [mysqld]下增加如下设置:
    init_connect=‘SET NAMES gbk’)
mysql> set names gbk;
Query OK, 0 rows affected (0.00 sec)
mysql>\s
--------------
mysql  Ver 14.14 Distrib 5.7.24, for linux-glibc2.12 (x86_64) using  EditLine wrapper

Connection id:          9
Current database:       pcsdb
Current user:           pcsdb@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.7.24-log MySQL Community Server (GPL)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8
Db     characterset:    gbk
Client characterset:    gbk
Conn.  characterset:    gbk
UNIX socket:            /tmp/mysql.sock
Uptime:                 1 hour 7 min 51 sec

  1. 执行 insert 语句进行验证
mysql> insert into pcs_seq (`seq_id`, `seq_type`, `seq_no`, `seq_desc`) values (1, '01', 1, '操作流水号');
Query OK, 1 row affected (0.01 sec)

mysql> select * from `pcs_seq`;
+--------+----------+--------+------------+
| seq_id | seq_type | seq_no | seq_desc   |
+--------+----------+--------+------------+
|      1 | 01       |      1 | 操作流水号  |
+--------+----------+--------+------------+
1 row in set (0.00 sec)

参考资料

MySQL错误代码:mysql/include/mysqld_error.h

  • 如有 错误之处 还请多多指正。希望能给您带来帮助。

  1. mysql ↩︎ ↩︎

  2. mysql ↩︎

你可能感兴趣的:(MYSQL)