我在处理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;
}
};
当从/etc/my.cnf删除这两行时出现问题。有什么建议吗?
答案 0 :(得分:-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