ºìÁªLinuxÃÅ»§
Linux°ïÖú

MySQL 4.1 ×Ö·û¼¯Ö§³ÖµÄÔ­Àí

·¢²¼Ê±¼ä:2006-02-24 00:25:47À´Ô´:ºìÁª×÷Õß:ÖйúÈË
ÏÂÃæҪдµÄÊÇһƪ·Ç³£ÎÞÁĵĶ«Î÷£¬³ä³âÁË´óÁ¿¸÷ʽ¸÷ÑùµÄ±àÂ롢ת»»¡¢¿Í»§¶Ë¡¢·þÎñÆ÷¶Ë¡¢Á¬½Ó¡­¡­ßÀ£¬ÎÒ×Ô¼º¶¼²»Ô¸ÒâÈ¥¿´Ëü£¬µ«ÏëÒ»Ï룬дÏÂÀ´»¹ÊÇÓеãÒâÒåµÄ£¬Ô­ÒòÓÐËÄ£º

MySQL 4.1 ¶Ô¶àÓïÑÔµÄÖ§³ÖÓÐÁ˺ܴó±ä»¯ (Õâµ¼ÖÂÁËÎÊÌâµÄ³öÏÖ)£»
¾¡¹Ü´ó²¿·ÖµÄµØ·½ (°üÀ¨¸öÈËʹÓúÍÖ÷»úÌṩÉÌ)£¬MySQL 3 ÈÔȻռÖ÷µ¼µØλ£»µ« MySQL 4.1 ÊÇ MySQL ¹Ù·½ÍƼöµÄÊý¾Ý¿â£¬ÒѾ­ÓÐÖ÷»úÌṩÉÌ¿ªÊ¼Ìṩ²¢½«»áÔ½À´Ô½¶à£»
Ðí¶à PHP ³ÌÐòÒÔ MySQL ×÷ΪĬÈϵÄÊý¾Ý¿â¹ÜÀíÈí¼þ£¬µ«ËüÃÇÒ»°ã²»Çø·Ö MySQL 4.1 Óë 4.1 ÒÔÏ°汾µÄÇø±ð£¬ÁýͳµØ³Æ¡°MySQL 3.xx.xx ÒÔÉÏ°æ±¾¡±¾ÍÂú×ã°²×°ÐèÇóÁË£»
ÒòΪ latin1 ÔÚÐí¶àµØ·½ (ϱ߻áÏêϸÃèÊö¾ßÌåÊÇÄÄЩµØ·½) ×÷ΪĬÈϵÄ×Ö·û¼¯£¬³É¹¦µÄÃɱÎÁËÐí¶à PHP ³ÌÐòµÄ¿ª·¢ÕߺÍÓû§£¬ÑÚ¸ÇÁËÔÚÖÐÎĵÈÓïÑÔ»·¾³Ï»á³öÏÖµÄÎÊÌ⣻
¼òµ¥µÄ˵£¬MySQL ×ÔÉíµÄ±ä»¯ºÍʹÓà MySQL µÄ PHP ³ÌÐò¶Ô´ËºöÂÔ£¬µ¼ÖÂÁËÎÊÌâµÄ³öÏֺ͸´ÔÓ»¯£¬¶øÓÉÓڴ󲿷ÖÓû§Ê¹ÓõÄÊÇÓ¢ÎÄ£¬Ê¹ÕâÖÖÎÊÌâ²»±»ÖØÊÓ¡£ÕâÀïÌáµ½µÄ PHP ³ÌÐò£¬Ö÷Òª¾Í WordPress ¶øÑÔ¡£

MySQL 4.1 ×Ö·û¼¯Ö§³ÖµÄÔ­Àí

MySQL 4.1 ¶ÔÓÚ×Ö·û¼¯µÄÖ¸¶¨¿ÉÒÔϸ»¯µ½Ò»Ì¨»úÆ÷ÉÏ°²×°µÄ MySQL£¬ÆäÖеÄÒ»¸öÊý¾Ý¿â£¬ÆäÖеÄÒ»ÕÅ±í£¬ÆäÖеÄÒ»À¸£¬Ó¦¸ÃÓÃʲô×Ö·û¼¯¡£µ«ÊÇ£¬´«Í³µÄ Web ³ÌÐòÔÚ´´½¨Êý¾Ý¿âºÍÊý¾Ý±íʱ²¢Ã»ÓÐʹÓÃÄÇô¸´ÔÓµÄÅäÖã¬ËüÃÇÓõÄÊÇĬÈϵÄÅäÖã¬ÄÇô£¬Ä¬ÈϵÄÅäÖôӺζøÀ´ÄØ£¿

±àÒë MySQL ʱ£¬Ö¸¶¨ÁËÒ»¸öĬÈϵÄ×Ö·û¼¯£¬Õâ¸ö×Ö·û¼¯ÊÇ latin1£»
°²×° MySQL ʱ£¬¿ÉÒÔÔÚÅäÖÃÎļþ (my.ini) ÖÐÖ¸¶¨Ò»¸öĬÈϵĵÄ×Ö·û¼¯£¬Èç¹ûûָ¶¨£¬Õâ¸öÖµ¼Ì³Ð×Ô±àÒëʱָ¶¨µÄ£»
Æô¶¯ mysqld ʱ£¬¿ÉÒÔÔÚÃüÁîÐвÎÊýÖÐÖ¸¶¨Ò»¸öĬÈϵĵÄ×Ö·û¼¯£¬Èç¹ûûָ¶¨£¬Õâ¸öÖµ¼Ì³Ð×ÔÅäÖÃÎļþÖеģ»
´Ëʱ character_set_server ±»É趨ΪÕâ¸öĬÈϵÄ×Ö·û¼¯£»
µ±´´½¨Ò»¸öеÄÊý¾Ý¿âʱ£¬³ý·ÇÃ÷È·Ö¸¶¨£¬Õâ¸öÊý¾Ý¿âµÄ×Ö·û¼¯±»È±Ê¡É趨Ϊ character_set_server£»
µ±Ñ¡¶¨ÁËÒ»¸öÊý¾Ý¿âʱ£¬character_set_database ±»É趨ΪÕâ¸öÊý¾Ý¿âĬÈϵÄ×Ö·û¼¯£»
ÔÚÕâ¸öÊý¾Ý¿âÀï´´½¨Ò»Õűíʱ£¬±íĬÈϵÄ×Ö·û¼¯±»É趨Ϊ character_set_database£¬Ò²¾ÍÊÇÕâ¸öÊý¾Ý¿âĬÈϵÄ×Ö·û¼¯£»
µ±ÔÚ±íÄÚÉèÖÃÒ»À¸Ê±£¬³ý·ÇÃ÷È·Ö¸¶¨£¬·ñÔò´ËÀ¸È±Ê¡µÄ×Ö·û¼¯¾ÍÊDZíĬÈϵÄ×Ö·û¼¯£»
Õâ¸ö×Ö·û¼¯¾ÍÊÇÊý¾Ý¿âÖÐʵ¼Ê´æ´¢Êý¾Ý²ÉÓõÄ×Ö·û¼¯£¬mysqldump ³öÀ´µÄÄÚÈݾÍÊÇÕâ¸ö×Ö·û¼¯Ïµģ»
¼òµ¥µÄ×ܽáһϣ¬Èç¹ûʲôµØ·½¶¼²»Ð޸ģ¬ÄÇôËùÓеÄÊý¾Ý¿âµÄËùÓбíµÄËùÓÐÀ¸Î»µÄ¶¼Óà latin1 ´æ´¢£¬²»¹ýÎÒÃÇÈç¹û°²×° MySQL£¬Ò»°ã¶¼»áÑ¡Ôñ¶àÓïÑÔÖ§³Ö£¬Ò²¾ÍÊÇ˵£¬°²×°³ÌÐò»á×Ô¶¯ÔÚÅäÖÃÎļþÖÐ°Ñ default_character_set ÉèÖÃΪ UTF-8£¬Õâ±£Ö¤ÁËȱʡÇé¿öÏ£¬ËùÓеÄÊý¾Ý¿âµÄËùÓбíµÄËùÓÐÀ¸Î»µÄ¶¼Óà UTF-8 ´æ´¢¡£

µ±Ò»¸ö PHP ³ÌÐòÓë MySQL ½¨Á¢Á¬½Óºó£¬Õâ¸ö³ÌÐò·¢Ë͸ø MySQL µÄÊý¾Ý²ÉÓõÄÊÇʲô×Ö·û¼¯£¿MySQL ÎÞ´ÓµÃÖª (Ëü×î¶àÖ»Äܲ²â)£¬ËùÒÔ MySQL 4.1 ÒªÇó¿Í»§¶Ë±ØÐëÖ¸¶¨Õâ¸ö×Ö·û¼¯£¬Ò²¾ÍÊÇ character_set_client£¬MySQL µÄ¹ÖÒìÖ®´¦ÔÚÓÚ£¬µÃµ½µÄÕâ¸ö×Ö·û¼¯²¢²»Á¢¼´×ª»»Îª´æ´¢ÔÚÊý¾Ý¿âÖеÄÄǸö×Ö·û¼¯£¬¶øÊÇÏÈת»»Îª character_set_connection ±äÁ¿Ö¸¶¨µÄÒ»¸ö×Ö·û¼¯£»Õâ¸ö connection ²ã¾¿¾¹ÓÐʲôÓÃÎÒ²»´óÃ÷°×£¬µ«×ª»»Îª character_set_connection µÄÕâ¸ö×Ö·û¼¯Ö®ºó£¬»¹Òª×ª»»ÎªÊý¾Ý¿âĬÈϵÄ×Ö·û¼¯£¬Ò²¾ÍÊÇ˵Ҫ¾­¹ýÁ½´Îת»»£»µ±Õâ¸öÊý¾Ý±»Êä³öʱ£¬ÓÖÒªÓÉÊý¾Ý¿âĬÈϵÄ×Ö·û¼¯×ª»»Îª character_set_results Ö¸¶¨µÄ×Ö·û¼¯¡£

Ò»¸öµäÐ͵Ļ·¾³

µäÐ͵Ļ·¾³ÒÔÎÒ×Ô¼ºµÄµçÄÔÉÏ°²×°µÄ MySQL 4.1 ΪÀý£¬ÎÒ×Ô¼ºµÄµçÄÔÉÏ°²×°×Å Apache 2£¬PHP 5 ºÍ WordPress 1.5.1.3£¬MySQL ÅäÖÃÎļþÖÐÖ¸¶¨ÁË default_character_set Ϊ utf8¡£ÓÚÊÇÎÊÌâ³öÏÖÁË£º

WordPress °´ÕÕĬÈÏÇé¿ö°²×°£¬ËùÒÔËùÓÐµÄ±í¶¼Óà UTF-8 ´æ´¢Êý¾Ý£»
WordPress ĬÈϲÉÓõÄä¯ÀÀ×Ö·û¼¯ÊÇ UTF-8 (Options->Reading ÖÐÉèÖÃ)£¬Òò´ËËùÓÐ WP Ò³ÃæµÄ meta Öлá˵Ã÷ charset ÊÇ utf-8£»
ËùÒÔä¯ÀÀÆ÷»áÒÔ utf-8 ·½Ê½ÏÔʾËùÓÐµÄ WP Ò³Ã棻ÕâÑùÒ»À´ Write µÄËùÓÐ Post£¬ºÍ Comment ¶¼»áÒÔ UTF-8 ¸ñʽ´Óä¯ÀÀÆ÷·¢Ë͸ø Apache£¬ÔÙÓÉ Apache ½»¸ø PHP£»
ËùÒÔ WP ´ÓËùÓÐµÄ±íµ¥Öеõ½µÄÊý¾Ý¶¼ÊÇ utf-8 ±àÂëµÄ£»WP ²»¼Óת»»µÄÖ±½Ó°ÑÕâЩÊý¾Ý·¢Ë͸ø MySQL£»
MySQL ĬÈÏÉèÖÃµÄ character_set_client ºÍ character_set_connection ¶¼ÊÇ latin1£¬´Ëʱ¹ÖÒìµÄÊÂÇé·¢ÉúÁË£¬Êµ¼ÊÉÏÊÇ utf-8 ¸ñʽµÄÊý¾Ý£¬±»¡°µ±×÷ latin1¡±×ª»»³É¡­¡­¾ÓÈ»»¹ÊÇת»»³É latin1£¬È»ºóÔÙÓÉÕâ¸ö latin1 ת»»³É utf-8£¬ÕâôÁ½´Îת»»£¬ÓÐÒ»²¿·Ö utf-8 µÄ×Ö·û¾Í¶ªÊ§ÁË£¬±ä³É ??£¬×îºóÊä³öµÄʱºò character_set_results ĬÈÏÊÇ latin1£¬Ò²¾ÍÊä³öΪÆæ¹ÖµÄ¶«Î÷ÁË¡£
×îÉñÆæµÄ»¹²»ÊÇÕâ¸ö£¬Èç¹û WordPress ÖÐÉèÖÃÒÔ GB2312 ¸ñʽÔĶÁ£¬ÄÇô WP ·¢Ë͸ø MySQL µÄ GB2312 ±àÂëµÄÊý¾Ý£¬±»¡°µ±×÷ latin1¡±×ª»»ºó£¬´æ½øÊý¾Ý¿âµÄÊÇÒ»ÖÖÆæ¹ÖµÄ¸ñʽ (ÕæµÄÊÇÆæ¹ÖµÄ¸ñʽ£¬mysqldump ³öÀ´¾ÍÄÜ·¢ÏÖ£¬ÎÞÂÛµ±×÷ utf-8 »¹Êǵ±×÷ gb2312 À´¶Á¶¼ÊÇÂÒÂë)£¬µ«Èç¹ûÕâÖÖ¸ñʽÒÔ latin1 Êä³ö³öÀ´£¬¾ÓÈ»ÓÖÄܱä»Ø GB2312£¡

Õâ»áµ¼ÖÂʲôÏÖÏóÄØ£¿WP Èç¹ûʹÓà MySQL 4.1 Êý¾Ý¿â£¬°Ñ±àÂë¸ÄÓà GB2312 ¾ÍÕý³£ÁË£¬¿Éϧ£¬ÕâÖÖÕý³£Ö»ÊÇòËÆÕý³£¡£

ÈçºÎ½â¾öÎÊÌâ

Èç¹ûÄãÒѾ­²»ÄÍ·³ÁË (¼¸ºõÊǿ϶¨µÄ)£¬google һϣ¬»á·¢ÏÖ¾ø´ó²¿·ÖµÄ½â´ðÊÇ£¬query ֮ǰÏÈÖ´ÐÐһϣºSET NAMES 'utf8'£¬Ã»´í£¬ÕâÊǽâ¾ö·½°¸£¬µ«±¾ÎĵÄÄ¿µÄÊÇ˵Ã÷£¬ÕâΪʲôÊǽâ¾ö·½°¸¡£

Òª±£Ö¤½á¹ûÕýÈ·£¬±ØÐë±£Ö¤Êý¾Ý±í²ÉÓõĸñʽÊÇÕýÈ·µÄ£¬Ò²¾ÍÊÇ˵£¬ÖÁÉÙÄܹ»´æ·ÅËùÓеĺº×Ö£¬ÄÇôÎÒÃÇÖ»ÓÐÁ½ÖÖÑ¡Ôñ£¬gbk »òÕß utf-8£¬ÏÂÃæÌÖÂÛ utf-8 µÄÇé¿ö¡£

ÒòΪÅäÖÃÎļþÉèÖÃµÄ default_character_set ÊÇ utf8£¬Êý¾Ý±íĬÈϲÉÓõľÍÊÇ utf-8 ½¨Á¢µÄ¡£ÕâÒ²Ó¦¸ÃÊÇËùÓвÉÓà MySQL 4.1 µÄÖ÷»úÌṩÉÌÓ¦¸Ã²ÉÓõÄÅäÖá£ËùÒÔÎÒÃÇÒª±£Ö¤µÄÖ»ÊÇ¿Í»§¶ËÓë MySQL ½»»¥Ö®¼äÖ¸¶¨±àÂëµÄÕýÈ·¡£

ÕâÖ»ÓÐÁ½ÖÖ¿ÉÄÜ£¬¿Í»§¶ËÒÔ gb2312 ¸ñʽ·¢ËÍÊý¾Ý£¬»òÕßÒÔ utf-8 ¸ñʽ·¢ËÍÊý¾Ý¡£

Èç¹ûÒÔ gb2312 ¸ñʽ·¢ËÍ:

SET character_set_client='gb2312'
SET character_set_connection='utf8' »òÕß
SET character_set_connection='gb2312'
¶¼ÊÇ¿ÉÒԵģ¬¶¼Äܹ»±£Ö¤Êý¾ÝÔÚ±àÂëת»»Öв»³öÏÖ¶ªÊ§£¬Ò²¾ÍÊDZ£Ö¤´æ´¢ÈëÊý¾Ý¿âµÄÊÇÕýÈ·µÄÄÚÈÝ¡£

Ôõô±£Ö¤È¡³öµÄÊÇÕýÈ·µÄÄÚÈÝÄØ£¿¿¼Âǵ½¾ø´ó²¿·Ö¿Í»§¶Ë (°üÀ¨ WP)£¬·¢ËÍÊý¾ÝµÄ±àÂëÒ²¾ÍÊÇËüËùÏ£ÍûÊÕµ½Êý¾ÝµÄ±àÂ룬ËùÒÔ:

SET character_set_results='gb2312'
¿ÉÒÔ±£Ö¤È¡³ö¸øä¯ÀÀÆ÷ÏÔʾµÄ¸ñʽ¾ÍÊÇ gb2312¡£

Èç¹ûÊǵڶþÖÖÇé¿ö£¬¿Í»§¶ËÒÔ utf-8 ¸ñʽ·¢ËÍ (WP µÄĬÈÏÇé¿ö)£¬¿ÉÒÔ²ÉÓÃÏÂÊöÅäÖÃ:

SET character_set_client='utf8'
SET character_set_connection='utf8'
SET character_set_results='utf8'
Õâ¸öÅäÖþ͵ȼÛÓÚ SET NAMES 'utf8'¡£

WP Ó¦¸Ã×÷ʲôÐÞ¸Ä

»¹ÊÇÄǾ仰£¬¿Í»§¶ËÒª·¢¸øÊý¾Ý¿âʲô±àÂëµÄÊý¾Ý£¬Êý¾Ý¿âÊDz»¿ÉÄÜÈ·ÇÐÖªµÀµÄ£¬Ö»ÄÜÈÿͻ§¶Ë×Ô¼ºËµÃ÷°×£¬ËùÒÔ£¬WP ÊDZØÐë·¢ËÍÕýÈ·µÄ SET... ¸ø MySQL µÄ¡£Ôõô·¢ËÍ×îºÏÊÊÄØ£¿Ì¨ÍåµÄ pLog ͬÈʸø³öÁËһЩ½¨Ò飺

Ê×ÏÈ£¬²âÊÔ·þÎñÆ÷ÊÇ·ñ >= 4.1£¬±àÒëʱÊÇ·ñ¼ÓÈëÁË UTF-8 Ö§³Ö£»ÊÇÔò¼ÌÐø
È»ºó²âÊÔÊý¾Ý¿âÒÔʲô¸ñʽ´æ´¢ ($dbEncoding)£»
SET NAMES $dbEncoding
¶ÔÓÚµÚ¶þµã£¬WP µÄÇé¿öÊDz»Í¬µÄ£¬°´ÕÕÉÏÃæµÄµäÐÍÅäÖã¬Ö»ÒªÓà WP£¬¿Ï¶¨Êý¾Ý¿âÊÇÓà UTF-8 ´æ´¢µÄ£¬ËùÒÔÒª¸ù¾ÝÓû§ÉèÖõÄÒÔ GB2312 »¹ÊÇ UTF-8 ä¯ÀÀÀ´ÅÐ¶Ï (bloginfo('charset'))£¬µ«Õâ¸öÖµÊÇÒªÁ¬½ÓÊý¾Ý¿âÒÔºó²ÅÄܵõ½µÄ£¬ËùÒÔЧÂÊ×î¸ßµÄ·½Ê½ÊÇÁ¬½ÓÊý¾Ý¿âÖ®ºó£¬¸ù¾ÝÕâ¸öÅäÖÃÉèÖÃÒ»´Î SET NAMES£¬¶ø²»±Øÿ´Î²éѯ֮ǰ¶¼ÉèÖÃÒ»±é¡£

ÎÒµÄÐ޸ķ½Ê½ÊÇÕâÑùµÄ£¬ÔÚ wp_includes/wp-db.php ÖÐÔö¼Ó:

function set_charset($charset)
{
// check mysql version first.
$serverVersion = mysql_get_server_info($this->dbh);
$version = explode('.', $serverVersion);
if ($version[0] < 4) return;

// check if utf8 support was compiled in
$result = mysql_query("SHOW CHARACTER SET like 'utf8'",
$this->dbh);
if (mysql_num_rows($result) < = 0) return;

if ($charset == 'utf-8' || $charset == 'UTF-8')
$charset = 'utf8';
@mysql_query("SET NAMES '$charset'", $this->dbh);
}
ÔÚ wp-settings.php µÄ require (ABSPATH . WPINC . '/vars.php'); ºóÔö¼Ó:

$wpdb->set_charset(get_bloginfo('charset'));
Posted in : Server-Side Author : jjgod
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ