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

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

·¢²¼Ê±¼ä:2006-10-19 22:14:01À´Ô´:ºìÁª×÷Õß:ckernel
¡¡¡¡Í¬Ê±ÔÚÏß·ÃÎÊÁ¿¼ÌÐøÔö´ó ¶ÔÓÚ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
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Ìì
key_buffer_size - 128M
key_read_requests - 650759289
key_reads - 79112
±ÈÀý½Ó½ü1:8000 ½¡¿µ×´¿ö·Ç³£ºÃ

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

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

ͨ¹ýµ÷½ÚÒÔϼ¸¸ö²ÎÊý¿ÉÒÔÖªµÀquery_cache_sizeÉèÖõÃÊÇ·ñºÏÀí
Qcache inserts
Qcache hits
Qcache lowmem prunes
Qcache free blocks
Qcache total blocks

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

ÎÒÉèÖÃ:
query_cache_size = 32M
query_cache_type= 1
µÃµ½ÈçÏÂ״ֵ̬:
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
Èç¹ûÄÚ´æÔÊÐí32MÓ¦¸ÃÒªÍùÉϼӵã

table_cache
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
µÃµ½ÒÔÏÂ״̬:
Open tables 256
Opened tables 9046
ËäÈ»open_tablesÒѾ­µÈÓÚtable_cache£¬µ«ÊÇÏà¶ÔÓÚ·þÎñÆ÷ÔËÐÐʱ¼äÀ´Ëµ,ÒѾ­ÔËÐÐÁË20Ì죬opened_tablesµÄÖµÒ²·Ç³£µÍ¡£Òò´Ë£¬Ôö¼Ótable_cacheµÄÖµÓ¦¸ÃÓô¦²»´ó¡£Èç¹ûÔËÐÐÁË6¸öСʱ¾Í³öÏÖÉÏÊöÖµ ÄǾÍÒª¿¼ÂÇÔö´ótable_cache¡£

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

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

¹ØµôÕâ¸ö¹¦ÄÜÖ»ÐèÒªÔÚËûÇ°Ãæ¼ÓÉÏ#ºÅ
#log-bin
¿ªÆôÂý²éѯÈÕÖ¾
¿ªÆôÂý²éѯÈÕÖ¾( 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

ÆäËûһЩÖØÒª²ÎÊý
±ÊÕßÉèÖÃ:
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

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

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

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

skip-innodb
#È¥µôinnodbÖ§³Ö

¸½my.cnfÈ«²¿Îļþ
´úÂë:

# Example MySQL config file for medium systems.
#
# This is for a system with little memory (32M - 64M) where MySQL plays
# an important part, or systems up to 128M where MySQL is used together with
# other programs (such as a web server)
#
# You can copy this file to
# /etc/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options (in this
# installation this directory is /var/lib/mysql) or
# ~/.my.cnf to set user-specific options.
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.

# The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
#socket = /var/lib/mysql/mysql.sock
# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port = 3306
socket = /tmp/mysql.sock
#socket = /var/lib/mysql/mysql.sock
skip-locking
key_buffer = 128M
max_allowed_packet = 1M
table_cache = 256
sort_buffer_size = 1M
net_buffer_length = 16K
myisam_sort_buffer_size = 1M
max_connections=120
#addnew config
wait_timeout =120
back_log=100
read_buffer_size = 1M
thread_cache=32
skip-innodb
skip-bdb
skip-name-resolve
join_buffer_size=512k
query_cache_size = 32M
interactive_timeout=120
long_query_time=10
log_slow_queries= /usr/local/mysql4/logs/slow_query.log
query_cache_type= 1
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 4

#end new config
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking

# Replication Master Server (default)
# binary logging is required for replication
#log-bin

# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id = 1

# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
# the syntax is:
#
# CHANGE MASTER TO MASTER_HOST=, MASTER_PORT=,
# MASTER_USER=, MASTER_PASSWORD= ;
#
# where you replace , , by quoted strings and
# by the master's port number (3306 by default).
#
# Example:
#
# CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
# MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
# start replication for the first time (even unsuccessfully, for example
# if you mistyped the password in master-password and the slave fails to
# connect), the slave will create a master.info file, and any later
# change in this file to the variables' values below will be ignored and
# overridden by the content of the master.info file, unless you shutdown
# the slave server, delete master.info and restart the slaver server.
# For that reason, you may want to leave the lines below untouched
# (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id = 2
#
# The replication master for this slave - required
#master-host =
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user =
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password =
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port =
#
# binary logging - not required for slaves, but recommended
#log-bin

# Point the following paths to different dedicated disks
#tmpdir = /tmp/
#log-update = /path-to-dedicated-directory/hostname

# Uncomment the following if you are using BDB tables
#bdb_cache_size = 4M
#bdb_max_lock = 10000

# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = /var/lib/mysql/
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /var/lib/mysql/
#innodb_log_arch_dir = /var/lib/mysql/
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[isamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[myisamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ