3

我在处理 Utf8 时遇到问题。我已经将数据库和表更改为如下所示:

       mysql>alter database database_name character set Utf8 collate utf8_unicode_ci;
       mysql>alter table table_name character set Utf8 collate utf8_unicode_ci;

当我检查其变量以显示字符配置和排序规则配置时,它显示如下:

       mysql> show variables like '%character%';
              +--------------------------+----------------------------+
              | Variable_name            | Value                      |
              +--------------------------+----------------------------+
              | character_set_client     | latin1                     |
              | character_set_connection | latin1                     |
              | character_set_database   | utf8                       |
              | character_set_filesystem | binary                     |
              | character_set_results    | latin1                     |
              | character_set_server     | latin1                     |
              | character_set_system     | utf8                       |
              | character_sets_dir       | /usr/share/mysql/charsets/ |
              +--------------------------+----------------------------+
              8 rows in set (0.00 sec)


       mysql> show variables like '%collation%';
              +----------------------+-------------------+
              | Variable_name        | Value             |
              +----------------------+-------------------+
              | collation_connection | latin1_swedish_ci |
              | collation_database   | utf8_unicode_ci   |
              | collation_server     | latin1_swedish_ci |
              +----------------------+-------------------+
              3 rows in set (0.00 sec)

当我将 /etc/my.cnf 设置为如下所示时,它运行良好:

        [mysqld]
        character-set-server=utf8

        [mysql]
        default-character-set=utf8

仅供参考,这是 JavaScript 上 Utf8 编码的代码:

        var Utf8 = {
            encode : function (string) {
                     string = string.replace(/\r\n/g,"\n");
                      var utftext = "";
                      deleteCookie('unicodeLength');
                      for (var n = 0; n < string.length; n++) {
                      var c = string.charCodeAt(n);
                      if (c < 128) {
                            utftext += String.fromCharCode(c);
                      }
                      else if((c > 127) && (c < 2048)) {
                            utftext += String.fromCharCode((c >> 6) | 192);
                            utftext += String.fromCharCode((c & 63) | 128);
                      }
                      else {
                            utftext += String.fromCharCode((c >> 12) | 224);
                            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                            utftext += String.fromCharCode((c & 63) | 128);
                      }
               }
            return utftext;
         }
    };

当这 2 行从 /etc/my.cnf 中删除时,问题就出现了。有什么建议么?

4

1 回答 1

-1

我认为您应该将 skip-character-set-client-handshake 添加到您的 /etc/my.cnf 中。

这是我的。

[mysqld]

default-character-set=utf8   
character-set-server=utf8  
skip-character-set-client-handshake  
collation-server=utf8_unicode_ci  
init-connect='SET NAMES utf8'  
init_connect='SET collation_connection = utf8_general_ci'  
port = 3306
于 2013-05-30T08:15:46.303 回答