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

ÔÚLinux¸ß¸ºÔØÏÂmysqlÊý¾Ý¿â³¹µ×ÓÅ»¯

·¢²¼Ê±¼ä:2006-09-06 09:32:42À´Ô´:ºìÁª×÷Õß:ÎÔÁú
¡¡¡¡Í¬Ê±ÔÚÏß·ÃÎÊÁ¿¼ÌÐøÔö´ó ¶ÔÓÚ1GÄÚ´æµÄ·þÎñÆ÷Ã÷ÏԸоõµ½³ÔÁ¦ÑÏÖØʱÉõÖÁÿÌ춼»áËÀ»ú »òÕßʱ²»Ê±µÄ·þÎñÆ÷¿¨Ò»Ï Õâ¸öÎÊÌâÔø¾­À§ÈÅÁËÎÒ°ë¸ö¶àÔÂMySQLʹÓÃÊǺܾßÉìËõÐÔµÄËã·¨£¬Òò´ËÄãͨ³£ÄÜÓúÜÉÙµÄÄÚ´æÔËÐлò¸øMySQL¸ü¶àµÄ±»´æÒԵõ½¸üºÃµÄÐÔÄÜ¡£

¡¡¡¡°²×°ºÃmysqlºó£¬ÅäÖÆÎļþÓ¦¸ÃÔÚ/usr/local/mysql/share/mysqlĿ¼ÖУ¬ÅäÖÆÎļþÓм¸¸ö£¬ÓÐmy-huge.cnf my-medium.cnf my-large.cnf my-small.cnf,²»Í¬µÄÁ÷Á¿µÄÍøÕ¾ºÍ²»Í¬ÅäÖƵķþÎñÆ÷»·¾³£¬µ±È»ÐèÒªÓв»Í¬µÄÅäÖÆÎļþÁË¡£

¡¡¡¡Ò»°ãµÄÇé¿öÏ£¬my-medium.cnfÕâ¸öÅäÖÆÎļþ¾ÍÄÜÂú×ãÎÒÃǵĴó¶àÐèÒª£»Ò»°ãÎÒÃÇ»á°ÑÅäÖÃÎļþ¿½±´µ½/etc/my.cnf Ö»ÐèÒªÐÞ¸ÄÕâ¸öÅäÖÃÎļþ¾Í¿ÉÒÔÁË£¬Ê¹ÓÃmysqladmin variables extended-status -u root -p ¿ÉÒÔ¿´µ½Ä¿Ç°µÄ²ÎÊý£¬ÓУ³¸öÅäÖòÎÊýÊÇ×îÖØÒªµÄ£¬¼´key_buffer_size,query_cache_size,table_cache¡£

¡¡¡¡key_buffer_sizeÖ»¶ÔMyISAM±íÆð×÷Óã¬

¡¡¡¡key_buffer_sizeÖ¸¶¨Ë÷Òý»º³åÇøµÄ´óС£¬Ëü¾ö¶¨Ë÷Òý´¦ÀíµÄËٶȣ¬ÓÈÆäÊÇË÷Òý¶ÁµÄËٶȡ£Ò»°ãÎÒÃÇÉèΪ16M,ʵ¼ÊÉÏÉÔ΢´óÒ»µãµÄÕ¾µã¡¡Õâ¸öÊý×ÖÊÇÔ¶Ô¶²»¹»µÄ£¬Í¨¹ý¼ì²é״ֵ̬Key_read_requestsºÍKey_reads,¿ÉÒÔÖªµÀkey_buffer_sizeÉèÖÃÊÇ·ñºÏÀí¡£±ÈÀýkey_reads / key_read_requestsÓ¦¸Ã¾¡¿ÉÄܵĵͣ¬ÖÁÉÙÊÇ1:100£¬1:1000¸üºÃ£¨ÉÏÊö״ֵ̬¿ÉÒÔʹÓÃSHOW STATUS LIKE ¡®key_read%¡¯»ñµÃ£©¡£ »òÕßÈç¹ûÄã×°ÁËphpmyadmin ¿ÉÒÔͨ¹ý·þÎñÆ÷ÔËÐÐ״̬¿´µ½,±ÊÕßÍƼöÓÃphpmyadmin¹ÜÀímysql£¬ÒÔϵÄ״ֵ̬¶¼ÊDZ¾ÈËͨ¹ýphpmyadmin»ñµÃµÄʵÀý·ÖÎö:

¡¡¡¡Õâ¸ö·þÎñÆ÷ÒѾ­ÔËÐÐÁË20Ìì

[code]key_buffer_size - 128M
key_read_requests - 650759289
key_reads - 79112[/code]

¡¡¡¡±ÈÀý½Ó½ü1:8000 ½¡¿µ×´¿ö·Ç³£ºÃ

¡¡¡¡ÁíÍâÒ»¸ö¹À¼Ækey_buffer_sizeµÄ°ì·¨¡¡°ÑÄãÍøÕ¾Êý¾Ý¿âµÄÿ¸ö±íµÄË÷ÒýËùÕ¼¿Õ¼ä´óС¼ÓÆðÀ´¿´¿´ÒÔ´Ë·þÎñÆ÷ΪÀý:±È½Ï´óµÄ¼¸¸ö±íË÷Òý¼ÓÆðÀ´´ó¸Å125M Õâ¸öÊý×Ö»áËæ×űí±ä´ó¶ø±ä´ó¡£

¡¡¡¡´Ó4.0.1¿ªÊ¼£¬MySQLÌṩÁ˲éѯ»º³å»úÖÆ¡£Ê¹Óòéѯ»º³å£¬MySQL½«SELECTÓï¾äºÍ²éѯ½á¹û´æ·ÅÔÚ»º³åÇøÖУ¬½ñºó¶ÔÓÚͬÑùµÄSELECTÓï¾ä£¨Çø·Ö´óСд£©£¬½«Ö±½Ó´Ó»º³åÇøÖжÁÈ¡½á¹û¡£¸ù¾ÝMySQLÓû§ÊֲᣬʹÓòéѯ»º³å×î¶à¿ÉÒÔ´ïµ½238%µÄЧÂÊ¡£

¡¡¡¡Í¨¹ýµ÷½ÚÒÔϼ¸¸ö²ÎÊý¿ÉÒÔÖªµÀquery_cache_sizeÉèÖõÃÊÇ·ñºÏÀí

[code]Qcache inserts
Qcache hits
Qcache lowmem prunes
Qcache free blocks
Qcache total blocks[/code]
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 1 ÌõÆÀÂÛ

  1. ÎÔÁú ÓÚ 2006-09-06 09:34:35·¢±í:

    ¡¡¡¡Qcache_lowmem_prunesµÄÖµ·Ç³£´ó£¬Ôò±íÃ÷¾­³£³öÏÖ»º³å²»¹»µÄÇé¿ö,ͬʱQcache_hitsµÄÖµ·Ç³£´ó£¬Ôò±íÃ÷²éѯ»º³åʹÓ÷dz£Æµ·±£¬´ËʱÐèÒªÔö¼Ó»º³å´óСQcache_hitsµÄÖµ²»´ó£¬Ôò±íÃ÷ÄãµÄ²éѯÖظ´Âʺܵͣ¬ÕâÖÖÇé¿öÏÂʹÓòéѯ»º³å·´¶ø»áÓ°ÏìЧÂÊ£¬ÄÇô¿ÉÒÔ¿¼ÂDz»Óòéѯ»º³å¡£´ËÍ⣬ÔÚSELECTÓï¾äÖмÓÈëSQL_NO_CACHE¿ÉÒÔÃ÷È·±íʾ²»Ê¹Óòéѯ»º³å¡£

    ¡¡¡¡Qcache_free_blocks£¬Èç¹û¸ÃÖµ·Ç³£´ó£¬Ôò±íÃ÷»º³åÇøÖÐËéƬºÜ¶àquery_cache_typeÖ¸¶¨ÊÇ·ñʹÓòéѯ»º³å

    ¡¡¡¡ÎÒÉèÖÃ:

    [code]query_cache_size = 32M

    query_cache_type= 1[/code]

    ¡¡¡¡µÃµ½ÈçÏÂ״ֵ̬:

    [code]Qcache queries in cache 12737 ±íÃ÷Ä¿Ç°»º´æµÄÌõÊý

    Qcache inserts 20649006

    Qcache hits 79060095 ¡¡¿´À´Öظ´²éѯÂÊ»¹Í¦¸ßµÄ

    Qcache lowmem prunes 617913¡¡ÓÐÕâô¶à´Î³öÏÖ»º´æ¹ýµÍµÄÇé¿ö

    Qcache not cached 189896 ¡¡¡¡

    Qcache free memory 18573912¡¡¡¡Ä¿Ç°Ê£Ó໺´æ¿Õ¼ä

    Qcache free blocks 5328 Õâ¸öÊý×ÖËƺõÓеã´ó¡¡ËéƬ²»ÉÙ

    Qcache total blocks 30953[/code]

    ¡¡¡¡Èç¹ûÄÚ´æÔÊÐí32MÓ¦¸ÃÒªÍùÉϼӵã

    ¡¡¡¡table_cacheÖ¸¶¨±í¸ßËÙ»º´æµÄ´óС¡£Ã¿µ±MySQL·ÃÎÊÒ»¸ö±íʱ£¬Èç¹ûÔÚ±í»º³åÇøÖл¹Óпռ䣬¸Ã±í¾Í±»´ò¿ª²¢·ÅÈëÆäÖУ¬ÕâÑù¿ÉÒÔ¸ü¿ìµØ·ÃÎʱíÄÚÈÝ¡£Í¨¹ý¼ì²é·åֵʱ¼äµÄ״ֵ̬Open_tablesºÍOpened_tables£¬¿ÉÒÔ¾ö¶¨ÊÇ·ñÐèÒªÔö¼Ótable_cacheµÄÖµ¡£Èç¹ûÄã·¢ÏÖopen_tablesµÈÓÚtable_cache£¬²¢ÇÒopened_tablesÔÚ²»¶ÏÔö³¤£¬ÄÇôÄã¾ÍÐèÒªÔö¼Ótable_cacheµÄÖµÁË£¨ÉÏÊö״ֵ̬¿ÉÒÔʹÓÃSHOW STATUS LIKE ¡®Open%tables¡¯»ñµÃ£©¡£×¢Ò⣬²»ÄÜäĿµØ°Ñtable_cacheÉèÖóɺܴóµÄÖµ¡£Èç¹ûÉèÖõÃÌ«¸ß£¬¿ÉÄÜ»áÔì³ÉÎļþÃèÊö·û²»×㣬´Ó¶øÔì³ÉÐÔÄܲ»Îȶ¨»òÕßÁ¬½Óʧ°Ü¡£

    ¡¡¡¡¶ÔÓÚÓÐ1GÄÚ´æµÄ»úÆ÷£¬ÍƼöÖµÊÇ128£­256¡£

    ¡¡¡¡±ÊÕßÉèÖÃtable_cache = 256

    ¡¡¡¡µÃµ½ÒÔÏÂ״̬:

    [code]Open tables 256

    Opened tables 9046[/code]

    ¡¡¡¡ËäÈ»open_tablesÒѾ­µÈÓÚtable_cache£¬µ«ÊÇÏà¶ÔÓÚ·þÎñÆ÷ÔËÐÐʱ¼äÀ´Ëµ,ÒѾ­ÔËÐÐÁË20Ì죬opened_tablesµÄÖµÒ²·Ç³£µÍ¡£Òò´Ë£¬Ôö¼Ótable_cacheµÄÖµÓ¦¸ÃÓô¦²»´ó¡£Èç¹ûÔËÐÐÁË6¸öСʱ¾Í³öÏÖÉÏÊöÖµ ÄǾÍÒª¿¼ÂÇÔö´ótable_cache¡£

    ¡¡¡¡Èç¹ûÄã²»ÐèÒª¼Ç¼2½øÖÆlog ¾Í°ÑÕâ¸ö¹¦Äܹصô£¬×¢Òâ¹ØµôÒÔºó¾Í²»Äָܻ´³öÎÊÌâÇ°µÄÊý¾ÝÁË£¬ÐèÒªÄúÊÖ¶¯±¸·Ý£¬¶þ½øÖÆÈÕÖ¾°üº¬ËùÓиüÐÂÊý¾ÝµÄÓï¾ä£¬ÆäÄ¿µÄÊÇÔÚ»Ö¸´Êý¾Ý¿âʱÓÃËüÀ´°ÑÊý¾Ý¾¡¿ÉÄָܻ´µ½×îºóµÄ״̬¡£ÁíÍ⣬Èç¹û×öͬ²½¸´ÖÆ( Replication )µÄ»°£¬Ò²ÐèҪʹÓöþ½øÖÆÈÕÖ¾´«ËÍÐÞ¸ÄÇé¿ö¡£

    ¡¡¡¡log_binÖ¸¶¨ÈÕÖ¾Îļþ£¬Èç¹û²»ÌṩÎļþÃû£¬MySQL½«×Ô¼º²úÉúȱʡÎļþÃû¡£MySQL»áÔÚÎļþÃûºóÃæ×Ô¶¯Ìí¼ÓÊý×ÖÒý£¬Ã¿´ÎÆô¶¯·þÎñʱ£¬¶¼»áÖØÐÂÉú³ÉÒ»¸öеĶþ½øÖÆÎļþ¡£´ËÍ⣬ʹÓÃlog-bin-index¿ÉÒÔÖ¸¶¨Ë÷ÒýÎļþ£»Ê¹ÓÃbinlog-do-db¿ÉÒÔÖ¸¶¨¼Ç¼µÄÊý¾Ý¿â£»Ê¹ÓÃbinlog-ignore-db¿ÉÒÔÖ¸¶¨²»¼Ç¼µÄÊý¾Ý¿â¡£×¢ÒâµÄÊÇ£ºbinlog-do-dbºÍbinlog-ignore-dbÒ»´ÎÖ»Ö¸¶¨Ò»¸öÊý¾Ý¿â£¬Ö¸¶¨¶à¸öÊý¾Ý¿âÐèÒª¶à¸öÓï¾ä¡£¶øÇÒ£¬MySQL»á½«ËùÓеÄÊý¾Ý¿âÃû³Æ¸Ä³ÉСд£¬ÔÚÖ¸¶¨Êý¾Ý¿âʱ±ØÐëÈ«²¿Ê¹ÓÃСдÃû×Ö£¬·ñÔò²»»áÆð×÷Óá£

    ¡¡¡¡¹ØµôÕâ¸ö¹¦ÄÜÖ»ÐèÒªÔÚËûÇ°Ãæ¼ÓÉÏ#ºÅ

    [code]#log-bin[/code]

    ¡¡¡¡¿ªÆôÂý²éѯÈÕÖ¾( slow query log )

    ¡¡¡¡Âý²éѯÈÕÖ¾¶ÔÓÚ¸ú×ÙÓÐÎÊÌâµÄ²éѯ·Ç³£ÓÐÓá£Ëü¼Ç¼ËùÓвé¹ýlong_query_timeµÄ²éѯ£¬Èç¹ûÐèÒª£¬»¹¿ÉÒԼǼ²»Ê¹ÓÃË÷ÒýµÄ¼Ç¼¡£ÏÂÃæÊÇÒ»¸öÂý²éѯÈÕÖ¾µÄÀý×Ó£º

    ¡¡¡¡¿ªÆôÂý²éѯÈÕÖ¾£¬ÐèÒªÉèÖòÎÊýlog_slow_queries¡¢long_query_times¡¢log-queries-not-using-indexes¡£

    ¡¡¡¡log_slow_queriesÖ¸¶¨ÈÕÖ¾Îļþ£¬Èç¹û²»ÌṩÎļþÃû£¬MySQL½«×Ô¼º²úÉúȱʡÎļþÃû¡£long_query_timesÖ¸¶¨Âý²éѯµÄãÐÖµ£¬È±Ê¡ÊÇ10Ãë¡£log-queries-not-using-indexesÊÇ4.1.0ÒÔºóÒýÈëµÄ²ÎÊý£¬Ëüָʾ¼Ç¼²»Ê¹ÓÃË÷ÒýµÄ²éѯ¡£±ÊÕßÉèÖÃlong_query_time=10

    ¡¡¡¡±ÊÕßÉèÖÃ:

    [code]sort_buffer_size = 1M
    max_connections=120
    wait_timeout =120
    back_log=100
    read_buffer_size = 1M
    thread_cache=32
    interactive_timeout=120
    thread_concurrency = 4[/code]

    ¡¡¡¡²ÎÊý˵Ã÷:

    [code]back_log[/code]

    ¡¡¡¡ÒªÇóMySQLÄÜÓеÄÁ¬½ÓÊýÁ¿¡£µ±Ö÷ÒªMySQLÏß³ÌÔÚÒ»¸öºÜ¶Ìʱ¼äÄڵõ½·Ç³£¶àµÄÁ¬½ÓÇëÇó£¬Õâ¾ÍÆð×÷Óã¬È»ºóÖ÷Ï̻߳¨Ð©Ê±¼ä(¾¡¹ÜºÜ¶Ì)¼ì²éÁ¬½Ó²¢ÇÒÆô¶¯Ò»¸öÐÂÏ̡߳£back_logÖµÖ¸³öÔÚMySQLÔÝʱֹͣ»Ø´ðÐÂÇëÇó֮ǰµÄ¶Ìʱ¼äÄÚ¶àÉÙ¸öÇëÇó¿ÉÒÔ±»´æÔÚ¶ÑÕ»ÖС£Ö»ÓÐÈç¹ûÆÚÍûÔÚÒ»¸ö¶Ìʱ¼äÄÚÓкܶàÁ¬½Ó£¬ÄãÐèÒªÔö¼ÓËü£¬»»¾ä»°Ëµ£¬ÕâÖµ¶Ôµ½À´µÄTCP/IPÁ¬½ÓµÄÕìÌý¶ÓÁеĴóС¡£ÄãµÄ²Ù×÷ϵͳÔÚÕâ¸ö¶ÓÁдóСÉÏÓÐËü×Ô¼ºµÄÏÞÖÆ¡£ Unix listen(2)ϵͳµ÷ÓõÄÊÖ²áÒ³Ó¦¸ÃÓиü¶àµÄϸ½Ú¡£¼ì²éÄãµÄOSÎĵµÕÒ³öÕâ¸ö±äÁ¿µÄ×î´óÖµ¡£ÊÔͼÉ趨back_log¸ßÓÚÄãµÄ²Ù×÷ϵͳµÄÏÞÖƽ«ÊÇÎÞЧµÄ¡£

    [code]max_connections[/code]

    ¡¡¡¡²¢·¢Á¬½ÓÊýÄ¿×î´ó£¬120 ³¬¹ýÕâ¸öÖµ¾Í»á×Ô¶¯»Ö¸´£¬³öÁËÎÊÌâÄÜ×Ô¶¯½â¾ö

    [code]thread_cache[/code]

    ¡¡¡¡Ã»ÕÒµ½¾ßÌå˵Ã÷£¬²»¹ýÉèÖÃΪ32ºó 20Ìì²Å´´½¨ÁË400¶à¸öÏß³Ì ¶øÒÔÇ°Ò»Ìì¾Í´´½¨ÁËÉÏǧ¸öÏß³Ì ËùÒÔ»¹ÊÇÓÐÓõÄ

    [code]thread_concurrency[/code]

    ¡¡¡¡#ÉèÖÃΪÄãµÄcpuÊýÄ¿x2,ÀýÈ磬ֻÓÐÒ»¸öcpu,ÄÇôthread_concurrency=2

    ¡¡¡¡#ÓÐ2¸öcpu,ÄÇôthread_concurrency=4

    [code]skip-innodb[/code]

    ¡¡¡¡#È¥µôinnodbÖ§³Ö