2012年3月27日 星期二

[MySQL] 資料庫、資料表、連線編碼 @ Ubuntu 10.04

最近接觸 MySQL,一直以來都不太熟,只記得幾年前曾透過 PhpMyAdmin 建過幾張表。最近則因為開發環境是 MySQL,加上資安設定,變成必須透過某台機器才能連線,於是我就"搞鋼"地透過 SSH Tunnel 和 MySQL command line 來使用(其實可以配合GUI來使用就好),隨後碰到資料庫、資料表及連線編碼問題。


首先是連線問題,要避開 mysql-client 的編碼問題,可採用以下指令強制使用 utf8 連線:


$ mysql -h 127.0.0.1 -u root -p --default-character-set=utf8


查詢當前環境的編碼資訊:


mysql> show variables like "char%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

其中 character_set_database、character_set_server 和 character_set_system 為 MySQL Server 端的設定,使用 mysql-client 連線時,設定 --default-character-set=utf8 影響的僅有 character_set_client 、character_set_connection 和 character_set_results,若 mysql-client 不設定使用 utf8 編碼時,在 Ubuntu 10.04 環境下,很有可能預設採用 latin1:


mysql> show variables like "char%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_results | latin1 |
+--------------------------+----------------------------+

接要要留意的還有 character_set_database 這個變數資訊,在 Ubuntu 10.04 server 環境預設也是 latin1,並且這個變數會跟你使用的資料庫有關:


mysql> CREATE DATABASE `MyDBDefault`;
mysql> use `MyDBDefault`;
mysql> show variables like "char%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_database | latin1 |
+--------------------------+----------------------------+

mysql> CREATE DATABASE `MyDB` DEFAULT CHARACTER SET utf8;
mysql> use `MyDB`;
mysql> show variables like "char%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_database | utf8 |
+--------------------------+----------------------------+

當資料庫 character_set_database 為 utf-8 時,這時在其裡頭建立的資料表預設就會是 utf8 了,如果建立資料庫沒指定並且系統環境也沒設定,那大概很有機會是 latin1 編碼。


總結一下,假使開發環境大家都默認是 utf8 編碼:


使用 mysql-client 操作時,多加 --default-character-set=utf8 使用,可避開 mysql-client 端環境變因(特別是不熟的機器):


$ mysql -h 127.0.0.1 -u root -p --default-character-set=utf8


建立資料庫時,可指定 DEFAULT CHARACTER SET utf8 來避開 server 端預設環境的問題:


mysql> CREATE DATABASE `MyDBUTF8` DEFAULT CHARACTER SET utf8;


建立資料表時,可指定 DEFAULT CHARSET=utf8 來避開資料庫預設非 utf8:


mysql> CREATE TABLE `MyTable` ( `MyID` INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY ) DEFAULT CHARSET=utf8;


其他部分:


如何透過指令查詢資料表、資料欄位的編碼 how-do-i-see-what-character-set-a-database-table-column-is-in-mysql


1 則留言:

  1. It's a superb blog. Thank you so much you covered all the needful skills scope for becoming an expert Full-stack web developer. I was thinking of starting freelancing for the last few weeks, and while I was searching for the perfect platform, I encountered Eilian.com. Since then, I am never running out of projects to suffer from delayed payments. They do everything to provide these on the top.

    回覆刪除