MySql 中文亂碼

來源:互聯網
上載者:User

標籤: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 中文亂碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.