標籤:blank 微軟雅黑 justify target style
1.1.1 現象
在表中插入的字串包含中文字元,Select出來是亂碼。
1.1.2 原因
用mysql –uroot –p命令進入,status命令查看Server的資訊:
650) this.width=650;" title="1" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="1" src="http://s3.51cto.com/wyfs02/M01/80/6D/wKioL1dBcFzAyYXdAACI52ykTq8945.png" width="355" height="179" />
可以看到各個字元集都不是UTF8,因此會導致亂碼。
也可以用SHOW VARIABLES LIKE ‘character%‘;命令查詢,可看到如下結果:
650) this.width=650;" title="2" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="2" src="http://s3.51cto.com/wyfs02/M02/80/6D/wKioL1dBcHOTAGdVAACl_xKBQ6o956.png" width="356" height="199" />
不同的機器可能不一樣,但基本類似於,也就是各種字元集不全是UTF8。
1.1.3 解決
開啟MySQL安裝的根目錄下的my.ini,增加如下配置項:
| [client] default-character-set=utf8 [mysql] default-character-set=utf8 [mysqld] character-set-server=utf8 |
注意:mysqld下面的參數,網上有的文章說也是配置“default-character-set=utf8”,但是在我的機器上這樣配置會導致MySQL啟動失敗,因此一定要按上表中的方式配置。
修改完畢my.ini後,重啟MySQL,用status命令查看:
650) this.width=650;" title="3" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="3" src="http://s3.51cto.com/wyfs02/M00/80/6D/wKioL1dBcJHwCL5IAACopLupKGc265.png" width="352" height="197" />
已經全部都是UTF8字元集了。
這樣修改後,建立的表,預設就都是UTF8字元集了,而在此之前建的表,還是之前的字元集,需要另外處理。
MySql 中文亂碼