天天看點

MySQL字元集的問題總結

一般我們在配置檔案my.cnf中添加配置:

<code>[client]</code>

<code>default-character-</code><code>set</code> <code>= utf8 </code>

<code>[mysqld]</code>

<code>default-character-</code><code>set</code> <code>= utf8</code>

<code>character-</code><code>set</code><code>-server = utf8</code>

<code>init-connect = </code><code>'SET NAMES utf8'</code>

<code>collation-server = utf8_general_ci   </code><code>#可選的</code>

建立資料庫時指定字元集:

<code>create database zabbix character </code><code>set</code> <code>utf8 collate utf8_bin;</code>

<code></code>

在mysql連接配接狀态檢視資料庫的字元集:

<code>show create database zabbix_office;     </code><code>#檢視建立資料庫的字元集</code>

<code>show variables like </code><code>'character%'</code><code>;       </code><code>#檢視mysql預設的字元集</code>

修改字元集:

<code>SET character_set_client = utf8 ;  </code>

<code>SET character_set_connection = utf8 ; </code>

<code>SET character_set_results = utf8 ;  </code>

<code>SET character_set_database = utf8 ;       </code>

<code>SET character_set_server = utf8 ;   </code>

<code>SET collation_connection = utf8 ;  </code>

<code>SET collation_database = utf8 ;   </code>

<code>SET collation_server = utf8 ;</code>

用戶端連接配接仍然看到的是亂碼,這時要看用戶端的字元集是否和服務端的字元集一緻,如果不一緻的話要修改用戶端字元集:

<code>SET NAMES </code><code>'utf8'</code><code>;</code>

修改已建立資料庫的預設字元集,這個沒啥好辦法,隻能導出資料,删庫然後重建立庫指定預設的字元集。

本文轉自 王家東哥 51CTO部落格,原文連結:http://blog.51cto.com/xiaodongge/1923961