通常我們如要儲存中文字碼在一個欄位裡,只要在create scheme加上"CHARACTER SET utf8",如:

`name` varchar(255) CHARACTER SET utf8 NOT NULL

 

但是在client傳送資料給mysql時,走的編碼並不是看欄位的編碼,所以當hibernate執行insert、update時,還是會有亂碼產生,

所以我們要更改mysql client跟内部解碼的設定:

OS: windows 7

修改mysql.ini(在mysql安裝檔下),並部份修改如下:


[client]

init_connect='SET collation_connection = utf8_general_ci,NAMES utf8'
character-set-server = utf8
collation-server = utf8_general_ci
port=3306


[mysql]
default-character-set=latin1

 


[mysqld]

# The default character set that will be used when a new schema or table is
# created and no character set is defined
init_connect='SET collation_connection = utf8_general_ci,NAMES utf8'
character-set-server = utf8
collation-server = utf8_general_ci

OS: CentOS 5.5 64bit

Linux部份則修改/etc/mysql.cnf:

[client]
init_connect='SET collation_connection = utf8_general_ci,NAMES utf8'
character-set-server = utf8
collation-server = utf8_general_ci

port=3306


[mysql]
default-character-set=latin1

[mysqld]

# The default character set that will be used when a new schema or table is
# created and no character set is defined
init_connect='SET collation_connection = utf8_general_ci,NAMES utf8'
character-set-server = utf8
collation-server = utf8_general_ci

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

 

這樣就可以顯示與插入中文資料了

[mysql]
default-character-set=latin1

上面這個設成latin1還是可以顯示中文

 

Linux my.cnf 範例:

[client]
default-character-set=utf8

[mysql]
default-character-set=latin1

[mysqld]
user=mysql
basedir=/usr
datadir=/var/lib/mysql
tmpdir=/tmp
pid-file=/var/run/mysqld/mysqld.pid
socket=/var/lib/mysql/mysql.sock
port=3306
bind-address=210.65.46.87
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1

# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0

# The default character set that will be used when a new schema or table is
# created and no character set is defined
# language=/usr/share/mysql/English
default-character-set=utf8
character-set-server=utf8
init-connect='SET NAMES utf8'

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

en → zh
utf8_general_ci
 
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 caramels 的頭像
    caramels

    Samuel

    caramels 發表在 痞客邦 留言(0) 人氣()