修改MySQL字符集

wolfreturn發表於2015-12-24
二進位制方式安裝的MySQL當時沒注意字符集問題,後來在建表的時候發現預設字符集是latin1
建個ting表
mysql> create table ting(id int);
Query OK, 0 rows affected (0.03 sec)
檢視錶的字符集
mysql> show create table ting \G
*************************** 1. row ***************************
       Table: ting
Create Table: CREATE TABLE `ting` (
  `id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
檢視系統字符集
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       | /home/mysql/mysql/share/charsets/ |
+--------------------------+-----------------------------------+
8 rows in set (0.00 sec)
個性資料庫字符集
mysql> alter database test character set utf8;
Query OK, 1 row affected (0.00 sec)
再建ting1表
mysql> CREATE TABLE ting1 (   `id` int(11));
Query OK, 0 rows affected (0.01 sec)
檢視字符集
mysql> show create table ting1 \G
*************************** 1. row ***************************
       Table: ting1
Create Table: CREATE TABLE `ting1` (
  `id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

在my.cnf檔案中新增如下內容
[mysqld]
character_set_client= utf8
character_set_server= utf8
下次再建資料庫的時候字符集就會預設為utf8

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24742969/viewspace-1959177/,如需轉載,請註明出處,否則將追究法律責任。

相關文章