Liunx下修改MySQL字符集
1.查找MySQL的cnf文件的位置
find / -iname '*.cnf' -print
/usr/share/MySQL/my-innodb-heavy-4G.cnf
/usr/share/MySQL/my-large.cnf
/usr/share/MySQL/my-small.cnf
/usr/share/MySQL/my-medium.cnf
/usr/share/MySQL/my-huge.cnf
/usr/share/texmf/web2c/texmf.cnf
/usr/share/texmf/web2c/mktex.cnf
/usr/share/texmf/web2c/fmtutil.cnf
/usr/share/texmf/tex/xmltex/XMLtexfmtutil.cnf
/usr/share/texmf/tex/jadetex/jadefmtutil.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-innodb-heavy-4G.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-large.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-small.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-medium.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-huge.cnf
2. 拷貝 small.cnf、my-medium.cnf、my-huge.cnf、my-innodb-heavy-4G.cnf其中的一個到/etc下,命名為my.cnf
cp /usr/share/MySQL/my-medium.cnf /etc/my.cnf
3. 修改my.cnf
vi /etc/my.cnf
在[clIEnt]下添加
default-character-set=utf8
在[MySQLd]下添加
default-character-set=utf8
4.重新啟動MySQL
[root@bogon ~]# /etc/rc.d/init.d/MySQL restart
Shutting down MySQL [ 確定 ]
Starting MySQL. [ 確定 ]
[root@bogon ~]# MySQL -u root -p
Enter passWord:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.22-rc-community-log MySQL Community Edition (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
5.查看字符集設置
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.02 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.02 sec)
MySQL>
其他的一些設置方法:
修改數據庫的字符集
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:
[root@bogon ~]# /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 ;