標籤:使用 檔案 io 資料 art 問題 cti ar
啟動mysql後,以root登入mysql
[email protected]:~# mysql -uroot -proot
mysql> show variables like ‘character%‘; #執行編碼顯示
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
在linux下修改/etc/mysql/my.cnf檔案
找到用戶端配置[client] 在下面添加
default-character-set=utf8 預設字元集為utf8
在找到[mysqld] 添加
default-character-set=utf8 預設字元集為utf8
init_connect=‘SET NAMES utf8‘ (設定串連mysql資料庫時使用utf8編碼,以讓mysql資料庫為utf8運行)
修改好後,重新啟動mysql 即可,查詢一下show variables like ‘character%‘;
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
此方法用於標準mysql版本同樣有效,對於/etc/my.cnf檔案,需要從mysql/support-files的檔案夾cp my-large.cnf一份到/etc/my.cnf
PS:
修改資料庫的字元集
mysql>use mydb
mysql>alter database mydb character set utf-8;
建立資料庫指定資料庫的字元集
mysql>create database mydb character set utf-8;
通過設定檔修改:
修改/var/lib/mysql/mydb/db.opt
default-character-set=latin1
default-collation=latin1_swedish_ci
為
default-character-set=utf8
default-collation=utf8_general_ci
重起MySQL:
[[email protected] ~]# /etc/rc.d/init.d/mysql restart
通過MySQL命令列修改:
mysql> set character_set_client=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_connection=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_database=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_results=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_server=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_system=utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> set collation_connection=utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> set collation_database=utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> set collation_server=utf8;
Query OK, 0 rows affected (0.01 sec)
查看:
mysql> show variables like ‘character_set_%‘;
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.03 sec)
mysql> show variables like ‘collation_%‘;
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.04 sec)
-------------------------------------------------------------------------
【知識性文章轉載】
MYSQL 字元集問題
MySQL的字元集支援(Character Set Support)有兩個方面:
字元集(Character set)和排序方式(Collation)。
對於字元集的支援細化到四個層次:
伺服器(server),資料庫(database),資料表(table)和串連(connection)。
1.MySQL預設字元集
MySQL對於字元集的指定可以細化到一個資料庫,一張表,一列,應該用什麼字元集。
但是,傳統的程式在建立資料庫和資料表時並沒有使用那麼複雜的配置,它們用的是預設的配置,那麼,預設的配置從何而來呢? (1)編譯MySQL 時,指定了一個預設的字元集,這個字元集是 latin1;
(2)安裝MySQL 時,可以在設定檔 (my.ini) 中指定一個預設的的字元集,如果沒指定,這個值繼承自編譯時間指定的;
(3)啟動mysqld 時,可以在命令列參數中指定一個預設的的字元集,如果沒指定,這個值繼承自設定檔中的配置,此時 character_set_server 被設定為這個預設的字元集;
(4)當建立一個新的資料庫時,除非明確指定,這個資料庫的字元集被預設設定為character_set_server;
(5)當選定了一個資料庫時,character_set_database 被設定為這個資料庫預設的字元集;
(6)在這個資料庫裡建立一張表時,表預設的字元集被設定為 character_set_database,也就是這個資料庫預設的字元集;
(7)當在表內設定一欄時,除非明確指定,否則此欄預設的字元集就是表預設的字元集;
簡單的總結一下,如果什麼地方都不修改,那麼所有的資料庫的所有表的所有欄位的都用
latin1 儲存,不過我們如果安裝 MySQL,一般都會選擇多語言支援,也就是說,安裝程式會自動在設定檔中把
default_character_set 設定為 UTF-8,這保證了預設情況下,所有的資料庫的所有表的所有欄位的都用 UTF-8 儲存。
2.查看預設字元集(預設情況下,mysql的字元集是latin1(ISO_8859_1)
通常,查看系統的字元集和排序方式的設定可以通過下面的兩條命令:
mysql> SHOW VARIABLES LIKE ‘character%‘;
+--------------------------+---------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | D:"mysql-5.0.37"share"charsets" |
+--------------------------+---------------------------------+
mysql> SHOW VARIABLES LIKE ‘collation_%‘;
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3.修改預設字元集
(1) 最簡單的修改方法,就是修改mysql的my.ini檔案中的字元集索引值,
如 default-character-set = utf8
character_set_server = utf8
修改完後,重啟mysql的服務,service mysql restart
使用 mysql> SHOW VARIABLES LIKE ‘character%‘;查看,探索資料庫編碼均已改成utf8
+--------------------------+---------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | D:"mysql-5.0.37"share"charsets" |
+--------------------------+---------------------------------+
(2) 還有一種修改字元集的方法,就是使用mysql的命令
mysql> SET character_set_client = utf8 ;