MySQL 中文insert報錯Incorrect string value:

來源:互聯網
上載者:User

MySQL 中文insert報錯Incorrect string value:

序言:中文錄入失敗,報錯:Incorrect string value: '\xCC\xEC\xB2\xC5',如下所示:

mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t select 2 as a ,'天才' as b;
ERROR 1366 (HY000): Incorrect string value: '\xCC\xEC\xB2\xC5' for column 'b' at row 1
mysql>
mysql>

1,去查看資料庫的字元集,是utf8,顯示正常的。
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                            |
| character_sets_dir      | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

mysql>

2,去查看串連用戶端的字元集,
vim /etc/my.cnf
#----------------- UTF-8 -----------------#
skip-character-set-client-handshake
init-connect='SET NAMES utf8'
character-set-server=utf8
#----------------- UTF-8 -----------------#
看到connect串連有utf8設定,也保證了,通過mysql命令列登入看到的是utf8編碼的。

3,通過sqlyog遠程工具操作,在Query視窗執行如下命令:
SET NAMES utf8;
INSERT INTO t SELECT 3  AS a ,'第二夢'  AS b;
是成功的,執行介面顯示如下:
2 queries executed, 2 success, 0 errors, 0 warnings

Query: set names utf8

0 row(s) affected

Execution Time : 0 sec
Transfer Time  : 0 sec
Total Time    : 0.001 sec
--------------------------------------------------

Query: insert into t select 3 as a ,'第二夢' as b

1 row(s) affected

Execution Time : 0.002 sec
Transfer Time  : 0 sec
Total Time    : 0.002 sec

在sqlyog視窗查詢t表,也正常顯示中文字元,如所示:

但是在linux後面使用mysql視窗命令登入,還是亂碼,如下所示:

mysql> SELECT * FROM t;
+---+-----------+
| a | b        |
+---+-----------+
| 1 | bb        |
| 1 | ??      |
| 2 | 騫村.    |
| 3 | 絎.?姊?|
+---+-----------+
4 rows in set (0.00 sec)

mysql>

那麼以此推斷,問題在哪裡呢,用戶端sqlyog能正常錄入中文顯示中文,而linux的mysql視窗不行,得去檢查os層的linux作業系統的字元集設定。

4,檢查mysql伺服器所在的linux os的字元集
[root@data01 ~]# cat /etc/sysconfig/i18n
LANG="zh_CN.GB18030"
LANGUAGE="zh_CN.GB18030:zh_CN.GB2312:zh_CN"
SUPPORTED="zh_CN.UTF-8:zh_CN:zh:en_US.UTF-8:en_US:en"
SYSFONT="lat0-sun16"
modified by timman on 2015/03/03

看到這裡面不是GB18030,不是utf8,所以需要重新設定下,從線上copy一下/etc/sysconfig/i18n 的內容,將此改成如下
[root@data01 ~]# cat /etc/sysconfig/i18n
# LANG="zh_CN.GB18030"
# LANGUAGE="zh_CN.GB18030:zh_CN.GB2312:zh_CN"
# SUPPORTED="zh_CN.UTF-8:zh_CN:zh:en_US.UTF-8:en_US:en"
# SYSFONT="lat0-sun16"
# modified by timman on 2015/03/03
LANG="zh_CN.UTF-8"

然後重啟linux,重啟mysql,或者export LANG="zh_CN.UTF-8";
[root@data01 ~]# service mysqld5612 restart
Shutting down MySQL......... SUCCESS!
Starting MySQL..................................................................... SUCCESS!
[root@data01 ~]#

更多詳情見請繼續閱讀下一頁的精彩內容:

  • 1
  • 2
  • 下一頁

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.