Java Tools

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            83 隨筆 :: 0 文章 :: 16 評(píng)論 :: 0 Trackbacks
           

          MySQL客戶(hù)端顯示漢字亂碼的解決

          MySQL 服務(wù)端不加任何參數(shù)啟動(dòng)時(shí),客戶(hù)端可以默認(rèn)正常顯示漢字,如下面所示

          代碼:

          D
          :mysql5>.binmysqld-max-nt.exe --console
          061106 20
          :35:21  InnoDB: Started; log sequence number 0 43655
          061106 20
          :35:22 [Note] .binmysqld-max-nt.exe: ready for connections.
          Version: '5.0.18-nt-max'  socket: ''  port: 3306  MySQL Community Edition (GPL)

          Microsoft Windows XP [版本 5.1.2600]
          (
          C) 版權(quán)所有 1985-2001 Microsoft Corp.

          C:Documents and SettingsAdministrator>cd d:mysql5bin

          C
          :Documents and SettingsAdministrator>d:

          D:mysql5bin>mysql -uroot
          Welcome to the MySQL monitor
          .  Commands end with ; or g.
          Your MySQL connection id is 1 to server version: 5.0.18-nt-max

          Type
          'help;' or 'h' for help. Type 'c' to clear the buffer.

          mysql> use test
          Database changed
          mysql
          > SHOW VARIABLES LIKE 'character_set%';
          +--------------------------+---------------------------+
          |
          Variable_name            | Value                     |
          +--------------------------+---------------------------+
          |
          character_set_client     | latin1                    |
          |
          character_set_connection | latin1                    |
          |
          character_set_database   | latin1                    |
          |
          character_set_results    | latin1                    |
          |
          character_set_server     | latin1                    |
          |
          character_set_system     | utf8                      |
          |
          character_sets_dir       | D:mysql5sharecharsets |
          +--------------------------+---------------------------+
          7 rows in set (0.00 sec)

          mysql> create table ad(name varchar(32));
          Query OK, 0 rows affected (0.10 sec)

          mysql> insert into ad values('漢字');
          Query OK, 1 row affected (0.00 sec)

          mysql> select * from ad;
          +------+
          |
          name |
          +------+
          |
          漢字 |
          +------+
          1 row in set (0.00 sec)

          mysql>
          此時(shí),插入、顯示字段的漢字值都正確。

          但是如果服務(wù)器啟動(dòng)的時(shí)候指定了字符集參數(shù),或者配置文件里面包含字符集參數(shù)
          情況就不同了
          my
          .ini
          [mysqld]
          default-
          character-set=gbk

          D
          :mysql5>.binmysqld-max-nt.exe --defaults-file=my.ini --ansi --console
          InnoDB
          : Error: log file .ib_logfile0 is of different size 0 5242880 bytes
          InnoDB
          : than specified in the .cnf file 0 8388608 bytes!
          061106 21:31:06 [Note] .binmysqld-max-nt.exe: ready for connections.
          Version: '5.0.18-nt-max'  socket: ''  port: 3306  MySQL Community Edition (GPL)
          061106 21:37:46 [Note] .binmysqld-max-nt.exe: Normal shutdown

          061106 21
          :37:48 [Note] .binmysqld-max-nt.exe: Shutdown complete


          D
          :mysql5>binmysqld-max-nt.exe --character_set_server=gbk  --console
          InnoDB
          : Error: log file .ib_logfile0 is of different size 0 5242880 bytes
          InnoDB
          : than specified in the .cnf file 0 8388608 bytes!
          061106 21:39:55 [Note] binmysqld-max-nt.exe: ready for connections.
          Version: '5.0.18-nt-max'  socket: ''  port: 3306  MySQL Community Edition (GPL)

          此時(shí),顯示過(guò)去未指定字符集時(shí)建立的表并插入漢字的字段,顯示是正常的

          D
          :mysql5bin>mysql -uroot
          Welcome to the MySQL monitor
          .  Commands end with ; or g.
          Your MySQL connection id is 1 to server version: 5.0.18-nt-max

          Type
          'help;' or 'h' for help. Type 'c' to clear the buffer.

          mysql> use test
          Database changed
          mysql
          > select * from ad;
          +------+
          |
          name |
          +------+
          |
          漢字 |
          +------+
          1 row in set (0.00 sec)

          mysql> SHOW VARIABLES LIKE 'character_set%';
          +--------------------------+---------------------------+
          |
          Variable_name            | Value                     |
          +--------------------------+---------------------------+
          |
          character_set_client     | latin1                    |
          |
          character_set_connection | latin1                    |
          |
          character_set_database   | gbk                       |
          |
          character_set_results    | latin1                    |
          |
          character_set_server     | gbk                       |
          |
          character_set_system     | utf8                      |
          |
          character_sets_dir       | D:mysql5sharecharsets |
          +--------------------------+---------------------------+
          7 rows in set (0.00 sec)
          這個(gè)時(shí)候建立的表并插入漢字的字段,不能正確顯示
          mysql
          > create table ad2(name varchar(32));
          Query OK, 0 rows affected (0.06 sec)

          mysql> insert into ad2 values('漢字');
          Query OK, 1 row affected, 1 warning (0.01 sec)

          mysql> select * from ad2;
          +------+
          |
          name |
          +------+
          | ??? |
          +------+
          1 row in set (0.01 sec)
          如果只在客戶(hù)端設(shè)定character_set_results參數(shù),那么不管服務(wù)器端是什么字符集時(shí)建立、插入漢字的表都顯示不正確
          mysql
          > set character_set_results='gbk';
          Query OK, 0 rows affected (0.00 sec)

          mysql> SHOW VARIABLES LIKE 'character_set%';
          +--------------------------+---------------------------+
          |
          Variable_name            | Value                     |
          +--------------------------+---------------------------+
          |
          character_set_client     | latin1                    |
          |
          character_set_connection | latin1                    |
          |
          character_set_database   | gbk                       |
          |
          character_set_results    | gbk                       |
          |
          character_set_server     | gbk                       |
          |
          character_set_system     | utf8                      |
          |
          character_sets_dir       | D:mysql5sharecharsets |
          +--------------------------+---------------------------+
          7 rows in set (0.00 sec)

          mysql> select * from ad2;
          +-------+
          |
          name  |
          +-------+
          | ??
          ×? |
          +-------+
          1 row in set (0.00 sec)

          mysql> select * from ad;
          +-------+
          |
          name  |
          +-------+
          | ??
          ×? |
          +-------+
          1 row in set (0.00 sec)
          如果再設(shè)定character_set_client,效果和只設(shè)定character_set_results沒(méi)有什么差別
          mysql
          > set character_set_client='gbk';
          Query OK, 0 rows affected (0.00 sec)

          mysql> select * from ad;
          +-------+
          |
          name  |
          +-------+
          | ??
          ×? |
          +-------+
          1 row in set (0.00 sec)

          mysql> select * from ad2;
          +-------+
          |
          name  |
          +-------+
          | ??
          ×? |
          +-------+
          1 row in set (0.00 sec)

          mysql> create table ad3(name varchar(32));
          Query OK, 0 rows affected (0.05 sec)

          mysql> insert into ad3 values('漢字');
          Query OK, 1 row affected (0.00 sec)

          mysql> select * from ad3;
          +------+
          |
          name |
          +------+
          | ??   |
          +------+
          1 row in set (0.01 sec)

          mysql> SHOW VARIABLES LIKE 'character_set%';
          +--------------------------+---------------------------+
          |
          Variable_name            | Value                     |
          +--------------------------+---------------------------+
          |
          character_set_client     | gbk                       |
          |
          character_set_connection | latin1                    |
          |
          character_set_database   | gbk                       |
          |
          character_set_results    | gbk                       |
          |
          character_set_server     | gbk                       |
          |
          character_set_system     | utf8                      |
          |
          character_sets_dir       | D:mysql5sharecharsets |
          +--------------------------+---------------------------+
          7 rows in set (0.00 sec)
          如果再設(shè)定character_set_connection,不影響已經(jīng)建立和插入的表,但是新建立、插入漢字的表就能正確顯示
          mysql
          > set character_set_connection='gbk';
          Query OK, 0 rows affected (0.00 sec)

          mysql> select * from ad3;
          +------+
          |
          name |
          +------+
          | ??   |
          +------+
          1 row in set (0.00 sec)

          mysql> select * from ad2;
          +-------+
          |
          name  |
          +-------+
          | ??
          ×? |
          +-------+
          1 row in set (0.00 sec)

          mysql> select * from ad;
          +-------+
          |
          name  |
          +-------+
          | ??
          ×? |
          +-------+
          1 row in set (0.00 sec)

          我們可以查看各個(gè)表的字符集
          mysql
          > show create table ad;
          +-------+----------------------------------------------------------------------------------------------+
          |
          Table | Create Table                                                                                 |
          +-------+----------------------------------------------------------------------------------------------+
          |
          ad    | CREATE TABLE `ad` (
            `
          name` varchar(32) default NULL
          ) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
          +-------+----------------------------------------------------------------------------------------------+
          1 row in set (0.00 sec)

          mysql> show create table ad2;
          +-------+--------------------------------------------------------------------------------------------+
          |
          Table | Create Table                                                                               |
          +-------+--------------------------------------------------------------------------------------------+
          |
          ad2   | CREATE TABLE `ad2` (
            `
          name` varchar(32) default NULL
          ) ENGINE=MyISAM DEFAULT CHARSET=gbk |
          +-------+--------------------------------------------------------------------------------------------+
          1 row in set (0.00 sec)

          mysql> show create table ad3;
          +-------+--------------------------------------------------------------------------------------------+
          |
          Table | Create Table                                                                               |
          +-------+--------------------------------------------------------------------------------------------+
          |
          ad3   | CREATE TABLE `ad3` (
            `
          name` varchar(32) default NULL
          ) ENGINE=MyISAM DEFAULT CHARSET=gbk |
          +-------+--------------------------------------------------------------------------------------------+
          1 row in set (0.00 sec)
          此時(shí)新建的表、此時(shí)插入漢字,顯示就正確了
          mysql
          > set character_set_client=gbk;
          Query OK, 0 rows affected (0.00 sec)

          mysql> set character_set_connection=gbk;
          Query OK, 0 rows affected (0.00 sec)

          mysql> set character_set_results=gbk;
          Query OK, 0 rows affected (0.00 sec)

          mysql> create table ad4(name varchar(32));
          Query OK, 0 rows affected (0.06 sec)

          mysql> insert into ad4 values('漢字');
          Query OK, 1 row affected (0.00 sec)

          mysql> select * from ad;
          +-------+
          |
          name  |
          +-------+
          | ??
          ×? |
          +-------+
          1 row in set (0.00 sec)

          mysql> select * from ad4;
          +------+
          |
          name |
          +------+
          |
          漢字 |
          +------+
          1 row in set (0.00 sec)

          mysql> create table ad5(name varchar(32)) DEFAULT CHARSET=gbk;
          Query OK, 0 rows affected (0.10 sec)

          mysql> insert into ad5 values('漢字');
          Query OK, 1 row affected (0.00 sec)

          mysql> select * from ad5;
          +------+
          |
          name |
          +------+
          |
          漢字 |
          +------+
          1 row in set (0.00 sec)

          mysql> select * from ad4;
          +------+
          |
          name |
          +------+
          |
          漢字 |
          +------+
          1 row in set (0.00 sec)

          mysql> show create table ad4;
          +-------+--------------------------------------------------------------------------------------------+
          |
          Table | Create Table                                                                               |
          +-------+--------------------------------------------------------------------------------------------+
          |
          ad4   | CREATE TABLE `ad4` (
            `
          name` varchar(32) default NULL
          ) ENGINE=MyISAM DEFAULT CHARSET=gbk |
          +-------+--------------------------------------------------------------------------------------------+
          1 row in set (0.00 sec)

          mysql> show create table ad5;
          +-------+--------------------------------------------------------------------------------------------+
          |
          Table | Create Table                                                                               |
          +-------+--------------------------------------------------------------------------------------------+
          |
          ad5   | CREATE TABLE `ad5` (
            `
          name` varchar(32) default NULL
          ) ENGINE=MyISAM DEFAULT CHARSET=gbk |
          +-------+--------------------------------------------------------------------------------------------+
          1 row in set (0.00 sec)

          mysql> show create table ad3;
          +-------+--------------------------------------------------------------------------------------------+
          |
          Table | Create Table                                                                               |
          +-------+--------------------------------------------------------------------------------------------+
          |
          ad3   | CREATE TABLE `ad3` (
            `
          name` varchar(32) default NULL
          ) ENGINE=MyISAM DEFAULT CHARSET=gbk |
          +-------+--------------------------------------------------------------------------------------------+
          1 row in set (0.00 sec)

          mysql> exit
          Bye
          --------------


          由此,我們可以認(rèn)識(shí)到,當(dāng)服務(wù)器端指定字符集時(shí)。如果客戶(hù)端沒(méi)有指定character_set_results字符集,
          那么可以正確顯示服務(wù)器端未指定字符集時(shí)建立、插入漢字的表。
          如果要新建的表(默認(rèn)字符集已經(jīng)改變?yōu)榉?wù)器端指定字符集)正確顯示漢字,必須同時(shí)設(shè)定character_set_client和character_set_connection參數(shù)和
          服務(wù)器端指定字符集保持一致,才能正確插入、顯示。
          如果只是要顯示以前帶字符集正確插入漢字的表,可以只在客戶(hù)端指定character_set_results就夠了。
          實(shí)際上服務(wù)器端和客戶(hù)端都未指定字符集就是所有字符集都默認(rèn)latin1,也是保持一致的。
          所以,如果要指定字符集,要在數(shù)據(jù)庫(kù)還不包含表的時(shí)候進(jìn)行比較保險(xiǎn)。如果中途改變,可能引起數(shù)據(jù)丟失。
          posted on 2007-07-19 20:56 和田雨 閱讀(5023) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): MySQL

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 平山县| 张家界市| 兴文县| 梁河县| 昭通市| 丁青县| 青川县| 永和县| 河西区| 大宁县| 石林| 乐陵市| 桃园县| 玛纳斯县| 富锦市| 新巴尔虎右旗| 麻城市| 闸北区| 台北市| 迁西县| 论坛| 赫章县| 内黄县| 九江市| 健康| 咸丰县| 长治县| 中西区| 许昌市| 高尔夫| 琼海市| 宝坻区| 栾川县| 中西区| 孝昌县| 昆明市| 牡丹江市| 陕西省| 拜城县| 灵石县| 介休市|