MySQL 5.5.49 大內(nèi)存優(yōu)化配置文件優(yōu)化詳解
一、配置文件說明
my-small.cnf
my-medium.cnf
my-large.cnf
my-huge.cnf
my-innodb-heavy-4G.cnf
二、詳解 my-innodb-heavy-4G.cnf
三、配置文件優(yōu)化
注:環(huán)境說明,CentO5.5 x86_64+MySQL-5.5.32 相關(guān)軟件下載:http://yunpan.cn/QtaCuLHLRKzRq
一、配置文件說明
Mysql-5.5.49是Mysql5.5系列中最后一個(gè)版本,也是最后一個(gè)有配置文件的版本,為什么這么說呢,用過5.6的博友都知道,在mysql5.6中已經(jīng)不提供配置文件選擇,只有一個(gè)默認(rèn)的配置文件,好了,我們今天說的是5.5.49這個(gè)版,就不和大家說5.6了,下面我們來具體說一下,mysql5.5.49中,提供可選的幾個(gè)配置文件,
my-small.cnf
my-medium.cnf
my-large.cnf
my-huge.cnf
my-innodb-heavy-4G.cnf
下面我們就來分別的看一下^_^……
1.my-small.cnf
[root@mysql support-files]# vim my-small.cnf # Example MySQL config file for small systems. # This is for a system with little memory (<= 64M) where MySQL is only used # from time to time and it's important that the mysqld daemon # doesn't use much resources.
這是my-small.cnf配置文件中開頭的簡介,它說明了,這個(gè)配置文件是在內(nèi)存小于等于64M時(shí)使用的,小型數(shù)據(jù)庫系統(tǒng),目的是不占更多的系統(tǒng)資源!
2.my-medium.cnf
[root@mysql support-files]# vim my-medium.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)
這個(gè)配置文件是中型數(shù)據(jù)系統(tǒng)使用,內(nèi)存在128M左右!
3.my-large.cnf
[root@mysql support-files]# vim my-large.cnf # Example MySQL config file for large systems. # This is for a large system with memory = 512M where the system runs mainly # MySQL.
這個(gè)配置文件是大型數(shù)據(jù)庫系統(tǒng)使用,內(nèi)存在512M左右!
4.my-huge.cnf
[root@mysql support-files]# vim my-huge.cnf # Example MySQL config file for very large systems. # This is for a large system with memory of 1G-2G where the system runs mainly # MySQL.
這個(gè)配置文件是巨型數(shù)據(jù)庫系統(tǒng)使用,內(nèi)存在1G-2G左右!
5.my-innodb-heavy-4G.cnf
[root@mysql support-files]# vim my-innodb-heavy-4G.cnf #BEGIN CONFIG INFO #DESCR: 4GB RAM, InnoDB only, ACID, few connections, heavy queries #TYPE: SYSTEM #END CONFIG INFO # This is a MySQL example config file for systems with 4GB of memory # running mostly MySQL using InnoDB only tables and performing complex # queries with few connections.
這個(gè)配置文件主要作用是,支持4G內(nèi)存,支持InnoDB引擎,支持事務(wù)(ACID)等特性所使用!
說明:ACID,指數(shù)據(jù)庫事務(wù)正確執(zhí)行的四個(gè)基本要素的縮寫。包含:原子性(Atomicity)、一致性(Consistency)、隔離性(Isolation)、持久性(Durability)!
6.總結(jié)
從上面的說明我們可以出,基本是通過內(nèi)存大小來選擇mysql的配置文件的,那有博友會說了,現(xiàn)在的服務(wù)器動不動就是32G內(nèi)存或者64G內(nèi)存,甚至更大的內(nèi)存,你那個(gè)配置文件最大只支持4G內(nèi)存是不是有點(diǎn)小了,確認(rèn)會有這樣的問題,從mysql5.6以后,為了更大的發(fā)揮mysql的性能,已經(jīng)去除了配置文件選擇,只有一個(gè)默認(rèn)的配置文件,里面只有一些基本配置,所有設(shè)置管理員都可以根據(jù)自己實(shí)際的需求進(jìn)行自行設(shè)置,好了說了這么多,我們就來說一說,在企業(yè)的用的最多的my-innodb-heavy-4G.cnf配置文件!
二、詳解 my-innodb-heavy-4G.cnf
1.詳細(xì)說明
注:下面是my-innodb-heavy-4G.cnf默認(rèn)配置我沒有做任何修改,下面我們就來詳細(xì)的說一說!
[root@mysql support-files]# vim my-innodb-heavy-4G.cnf #BEGIN CONFIG INFO #DESCR: 4GB RAM, InnoDB only, ACID, few connections, heavy queries #TYPE: SYSTEM #END CONFIG INFO # # This is a MySQL example config file for systems with 4GB of memory # running mostly MySQL using InnoDB only tables and performing complex # queries with few connections. # # MySQL programs look for option files in a set of # locations which depend on the deployment platform. # You can copy this option file to one of those # locations. For information about these locations, see: # http://dev.mysql.com/doc/mysql/en/option-files.html # # 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. # # More detailed information about the individual options can also be # found in the manual. # # # The following options will be read by MySQL client applications. # Note that only client applications shipped by MySQL are guaranteed # to read this section. If you want your own MySQL client program to # honor these values, you need to specify it as an option during the # MySQL client library initialization. # # 以下選項(xiàng)會被MySQL客戶端應(yīng)用讀取, 注意只有MySQL附帶的客戶端應(yīng)用程序保證可以讀取這段內(nèi)容,如果你想你自己的MySQL應(yīng)用程序獲取這些值,需要在MySQL客戶端庫初始化的時(shí)候指定這些選項(xiàng) [client] #password = [your_password] #mysql客戶端連接mysql時(shí)的密碼 port = 3306 #mysql客戶端連接時(shí)的默認(rèn)端口 socket = /tmp/mysql.sock #與mysql服務(wù)器本地通信所使用的socket文件路徑 # *** Application-specific options follow here *** # # The MySQL server # [mysqld] # generic configuration options #一般配置選項(xiàng) port = 3306 #mysql服務(wù)器監(jiān)聽的默認(rèn)端口 socket = /tmp/mysql.sock #socket本地通信文件路徑 # back_log is the number of connections the operating system can keep in # the listen queue, before the MySQL connection manager thread has # processed them. If you have a very high connection rate and experience # "connection refused" errors, you might need to increase this value. # Check your OS documentation for the maximum value of this parameter. # Attempting to set back_log higher than your operating system limit # will have no effect. # back_log 是操作系統(tǒng)在監(jiān)聽隊(duì)列中所能保持的連接數(shù), # 隊(duì)列保存了在MySQL連接管理器線程處理之前的連接. # 如果你有非常高的連接率并且出現(xiàn)“connection refused”報(bào)錯(cuò), # 你就應(yīng)該增加此處的值. # 檢查你的操作系統(tǒng)能打開文件數(shù)來獲取這個(gè)變量的最大值. # 如果將back_log設(shè)定到比你操作系統(tǒng)限制更高的值,將會沒有效果 back_log = 50 # 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! # 不在TCP/IP端口上進(jìn)行監(jiān)聽. # 如果所有的進(jìn)程都是在同一臺服務(wù)器連接到本地的mysqld, # 這樣設(shè)置將是增強(qiáng)安全的方法 # 所有mysqld的連接都是通過Unix sockets 或者命名管道進(jìn)行的. # 注意在windows下如果沒有打開命名管道選項(xiàng)而只是用此項(xiàng) # (通過 “enable-named-pipe” 選項(xiàng)) 將會導(dǎo)致mysql服務(wù)沒有任何作用! #skip-networking #默認(rèn)是沒有開啟的 # The maximum amount of concurrent sessions the MySQL server will # allow. One of these connections will be reserved for a user with # SUPER privileges to allow the administrator to login even if the # connection limit has been reached. # MySQL 服務(wù)器所允許的同時(shí)會話數(shù)的上限 # 其中一個(gè)連接將被SUPER權(quán)限保留作為管理員登錄. # 即便已經(jīng)達(dá)到了連接數(shù)的上限. max_connections = 100 # Maximum amount of errors allowed per host. If this limit is reached, # the host will be blocked from connecting to the MySQL server until # "FLUSH HOSTS" has been run or the server was restarted. Invalid # passwords and other errors during the connect phase result in # increasing this value. See the "Aborted_connects" status variable for # global counter. # 每個(gè)客戶端連接最大的錯(cuò)誤允許數(shù)量,如果達(dá)到了此限制. # 這個(gè)客戶端將會被MySQL服務(wù)阻止直到執(zhí)行了”FLUSH HOSTS” 或者服務(wù)重啟 # 非法的密碼以及其他在鏈接時(shí)的錯(cuò)誤會增加此值. # 查看 “Aborted_connects” 狀態(tài)來獲取全局計(jì)數(shù)器. max_connect_errors = 10 # The number of open tables for all threads. Increasing this value # increases the number of file descriptors that mysqld requires. # Therefore you have to make sure to set the amount of open files # allowed to at least 4096 in the variable "open-files-limit" in # section [mysqld_safe] # 所有線程所打開表的數(shù)量. # 增加此值就增加了mysqld所需要的文件描述符的數(shù)量 # 這樣你需要確認(rèn)在[mysqld_safe]中 “open-files-limit” 變量設(shè)置打開文件數(shù)量允許至少2048 table_open_cache = 2048 # Enable external file level locking. Enabled file locking will have a # negative impact on performance, so only use it in case you have # multiple database instances running on the same files (note some # restrictions still apply!) or if you use other software relying on # locking MyISAM tables on file level. # 允許外部文件級別的鎖. 打開文件鎖會對性能造成負(fù)面影響 # 所以只有在你在同樣的文件上運(yùn)行多個(gè)數(shù)據(jù)庫實(shí)例時(shí)才使用此選項(xiàng)(注意仍會有其他約束!) # 或者你在文件層面上使用了其他一些軟件依賴來鎖定MyISAM表 #external-locking #默認(rèn)是沒有開啟的 # The maximum size of a query packet the server can handle as well as # maximum query size server can process (Important when working with # large BLOBs). enlarged dynamically, for each connection. # 服務(wù)所能處理的請求包的最大大小以及服務(wù)所能處理的最大的請求大小(當(dāng)與大的BLOB字段一起工作時(shí)相當(dāng)必要) # 每個(gè)連接獨(dú)立的大小.大小動態(tài)增加 max_allowed_packet = 16M # The size of the cache to hold the SQL statements for the binary log # during a transaction. If you often use big, multi-statement # transactions you can increase this value to get more performance. All # statements from transactions are buffered in the binary log cache and # are being written to the binary log at once after the COMMIT. If the # transaction is larger than this value, temporary file on disk is used # instead. This buffer is allocated per connection on first update # statement in transaction # 在一個(gè)事務(wù)中binlog為了記錄SQL狀態(tài)所持有的cache大小 # 如果你經(jīng)常使用大的,多聲明的事務(wù),你可以增加此值來獲取更大的性能. # 所有從事務(wù)來的狀態(tài)都將被緩沖在binlog緩沖中然后在提交后一次性寫入到binlog中 # 如果事務(wù)比此值大, 會使用磁盤上的臨時(shí)文件來替代. # 此緩沖在每個(gè)連接的事務(wù)第一次更新狀態(tài)時(shí)被創(chuàng)建 binlog_cache_size = 1M # Maximum allowed size for a single HEAP (in memory) table. This option # is a protection against the accidential creation of a very large HEAP # table which could otherwise use up all memory resources. # 獨(dú)立的內(nèi)存表所允許的最大容量. # 此選項(xiàng)為了防止意外創(chuàng)建一個(gè)超大的內(nèi)存表導(dǎo)致永盡所有的內(nèi)存資源. max_heap_table_size = 64M # Size of the buffer used for doing full table scans. # Allocated per thread, if a full scan is needed. #MySql讀入緩沖區(qū)大小。對表進(jìn)行順序掃描的請求將分配一個(gè)讀入緩沖區(qū),MySql會為它分#配一段內(nèi)存緩沖區(qū)。read_buffer_size變量控制這一緩沖區(qū)的大小。如果對表的順序掃描請求非常頻繁,#并且你認(rèn)為頻繁掃描進(jìn)行得太慢,可以通過增加該變量值以及內(nèi)存緩沖區(qū)大小提高其性能。 read_buffer_size = 2M # When reading rows in sorted order after a sort, the rows are read # through this buffer to avoid disk seeks. You can improve ORDER BY # performance a lot, if set this to a high value. # Allocated per thread, when needed. #是MySql的隨機(jī)讀緩沖區(qū)大小。當(dāng)按任意順序讀取行時(shí)(例如,按照排序順序),將分配一個(gè)隨機(jī)讀緩存區(qū)。進(jìn)行排序查詢時(shí),MySql會首先掃描一遍該緩沖,以避免磁盤搜索,提高查詢速度,如果需#要排序大量數(shù)據(jù),可適當(dāng)調(diào)高該值。但MySql會為每個(gè)客戶連接發(fā)放該緩沖空間,所以應(yīng)盡量適當(dāng)設(shè)置該值,以避免內(nèi)存開銷過大。 read_rnd_buffer_size = 16M # Sort buffer is used to perform sorts for some ORDER BY and GROUP BY # queries. If sorted data does not fit into the sort buffer, a disk # based merge sort is used instead - See the "Sort_merge_passes" # status variable. Allocated per thread if sort is needed. # 排序緩沖被用來處理類似ORDER BY以及GROUP BY隊(duì)列所引起的排序 # 如果排序后的數(shù)據(jù)無法放入排序緩沖, # 一個(gè)用來替代的基于磁盤的合并分類會被使用 # 查看 “Sort_merge_passes” 狀態(tài)變量. # 在排序發(fā)生時(shí)由每個(gè)線程分配 sort_buffer_size = 8M # This buffer is used for the optimization of full JOINs (JOINs without # indexes). Such JOINs are very bad for performance in most cases # anyway, but setting this variable to a large value reduces the # performance impact. See the "Select_full_join" status variable for a # count of full JOINs. Allocated per thread if full join is found # 此緩沖被使用來優(yōu)化全聯(lián)合(full JOINs 不帶索引的聯(lián)合). # 類似的聯(lián)合在極大多數(shù)情況下有非常糟糕的性能表現(xiàn), # 但是將此值設(shè)大能夠減輕性能影響. # 通過 “Select_full_join” 狀態(tài)變量查看全聯(lián)合的數(shù)量 # 當(dāng)全聯(lián)合發(fā)生時(shí),在每個(gè)線程中分配 join_buffer_size = 8M # How many threads we should keep in a cache for reuse. When a client # disconnects, the client's threads are put in the cache if there aren't # more than thread_cache_size threads from before. This greatly reduces # the amount of thread creations needed if you have a lot of new # connections. (Normally this doesn't give a notable performance # improvement if you have a good thread implementation.) # 我們在cache中保留多少線程用于重用 # 當(dāng)一個(gè)客戶端斷開連接后,如果cache中的線程還少于thread_cache_size, # 則客戶端線程被放入cache中. # 這可以在你需要大量新連接的時(shí)候極大的減少線程創(chuàng)建的開銷 # (一般來說如果你有好的線程模型的話,這不會有明顯的性能提升.) thread_cache_size = 8 # This permits the application to give the threads system a hint for the # desired number of threads that should be run at the same time. This # value only makes sense on systems that support the thread_concurrency() # function call (Sun Solaris, for example). # You should try [number of CPUs]*(2..4) for thread_concurrency # 此允許應(yīng)用程序給予線程系統(tǒng)一個(gè)提示在同一時(shí)間給予渴望被運(yùn)行的線程的數(shù)量. # 此值只對于支持 thread_concurrency() 函數(shù)的系統(tǒng)有意義( 例如Sun Solaris). # 你可可以嘗試使用 [CPU數(shù)量]*(2..4) 來作為thread_concurrency的值 thread_concurrency = 8 # Query cache is used to cache SELECT results and later return them # without actual executing the same query once again. Having the query # cache enabled may result in significant speed improvements, if your # have a lot of identical queries and rarely changing tables. See the # "Qcache_lowmem_prunes" status variable to check if the current value # is high enough for your load. # Note: In case your tables change very often or if your queries are # textually different every time, the query cache may result in a # slowdown instead of a performance improvement. # 查詢緩沖常被用來緩沖 SELECT 的結(jié)果并且在下一次同樣查詢的時(shí)候不再執(zhí)行直接返回結(jié)果. # 打開查詢緩沖可以極大的提高服務(wù)器速度, 如果你有大量的相同的查詢并且很少修改表. # 查看 “Qcache_lowmem_prunes” 狀態(tài)變量來檢查是否當(dāng)前值對于你的負(fù)載來說是否足夠高. # 注意: 在你表經(jīng)常變化的情況下或者如果你的查詢原文每次都不同, # 查詢緩沖也許引起性能下降而不是性能提升. query_cache_size = 64M # Only cache result sets that are smaller than this limit. This is to # protect the query cache of a very large result set overwriting all # other query results. # 只有小于此設(shè)定值的結(jié)果才會被緩沖 # 此設(shè)置用來保護(hù)查詢緩沖,防止一個(gè)極大的結(jié)果集將其他所有的查詢結(jié)果都覆蓋. query_cache_limit = 2M # Minimum word length to be indexed by the full text search index. # You might wish to decrease it if you need to search for shorter words. # Note that you need to rebuild your FULLTEXT index, after you have # modified this value. # 被全文檢索索引的最小的字長. # 你也許希望減少它,如果你需要搜索更短字的時(shí)候. # 注意在你修改此值之后, # 你需要重建你的 FULLTEXT 索引 ft_min_word_len = 4 # If your system supports the memlock() function call, you might want to # enable this option while running MySQL to keep it locked in memory and # to avoid potential swapping out in case of high memory pressure. Good # for performance. # 如果你的系統(tǒng)支持 memlock() 函數(shù),你也許希望打開此選項(xiàng)用以讓運(yùn)行中的mysql在在內(nèi)存高度緊張的時(shí)候,數(shù)據(jù)在內(nèi)存中保持鎖定并且防止可能被swapping out # 此選項(xiàng)對于性能有益 #memlock # Table type which is used by default when creating new tables, if not # specified differently during the CREATE TABLE statement. # 當(dāng)創(chuàng)建新表時(shí)作為默認(rèn)使用的表類型, # 如果在創(chuàng)建表示沒有特別執(zhí)行表類型,將會使用此值 default-storage-engine = MYISAM # Thread stack size to use. This amount of memory is always reserved at # connection time. MySQL itself usually needs no more than 64K of # memory, while if you use your own stack hungry UDF functions or your # OS requires more stack for some operations, you might need to set this # to a higher value. # 線程使用的堆大小. 此容量的內(nèi)存在每次連接時(shí)被預(yù)留. # MySQL 本身常不會需要超過64K的內(nèi)存 # 如果你使用你自己的需要大量堆的UDF函數(shù) # 或者你的操作系統(tǒng)對于某些操作需要更多的堆, # 你也許需要將其設(shè)置的更高一點(diǎn). thread_stack = 192K # Set the default transaction isolation level. Levels available are: # 設(shè)定默認(rèn)的事務(wù)隔離級別.可用的級別如下: # READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, SERIALIZABLE transaction_isolation = REPEATABLE-READ # Maximum size for internal (in-memory) temporary tables. If a table # grows larger than this value, it is automatically converted to disk # based table This limitation is for a single table. There can be many # of them. # 內(nèi)部(內(nèi)存中)臨時(shí)表的最大大小 # 如果一個(gè)表增長到比此值更大,將會自動轉(zhuǎn)換為基于磁盤的表. # 此限制是針對單個(gè)表的,而不是總和. tmp_table_size = 64M # Enable binary logging. This is required for acting as a MASTER in a # replication configuration. You also need the binary log if you need # the ability to do point in time recovery from your latest backup. # 打開二進(jìn)制日志功能. # 在復(fù)制(replication)配置中,作為MASTER主服務(wù)器必須打開此項(xiàng) # 如果你需要從你最后的備份中做基于時(shí)間點(diǎn)的恢復(fù),你也同樣需要二進(jìn)制日志. log-bin=mysql-bin # binary logging format - mixed recommended #設(shè)定記錄二進(jìn)制日志的格式,有三種格式,基于語句 statement、 基于行 row、 混合方式 mixed binlog_format=mixed # If you're using replication with chained slaves (A->B->C), you need to # enable this option on server B. It enables logging of updates done by # the slave thread into the slave's binary log. # 如果你在使用鏈?zhǔn)綇姆?wù)器結(jié)構(gòu)的復(fù)制模式 (A->B->C), # 你需要在服務(wù)器B上打開此項(xiàng). # 此選項(xiàng)打開在從線程上重做過的更新的日志, # 并將其寫入從服務(wù)器的二進(jìn)制日志. #log_slave_updates # Enable the full query log. Every query (even ones with incorrect # syntax) that the server receives will be logged. This is useful for # debugging, it is usually disabled in production use. # 打開查詢?nèi)罩? 所有的由服務(wù)器接收到的查詢 (甚至對于一個(gè)錯(cuò)誤語法的查詢) # 都會被記錄下來. 這對于調(diào)試非常有用, 在生產(chǎn)環(huán)境中常常關(guān)閉此項(xiàng). #log #默認(rèn)是沒有開啟的,會影響服務(wù)器性能 # Print warnings to the error log file. If you have any problem with # MySQL you should enable logging of warnings and examine the error log # for possible explanations. # 將警告打印輸出到錯(cuò)誤log文件. 如果你對于MySQL有任何問題 # 你應(yīng)該打開警告log并且仔細(xì)審查錯(cuò)誤日志,查出可能的原因. #log_warnings # Log slow queries. Slow queries are queries which take more than the # amount of time defined in "long_query_time" or which do not use # indexes well, if log_short_format is not enabled. It is normally good idea # to have this turned on if you frequently add new queries to the # system. # 記錄慢速查詢. 慢速查詢是指消耗了比 “l(fā)ong_query_time” 定義的更多時(shí)間的查詢. # 如果 log_long_format 被打開,那些沒有使用索引的查詢也會被記錄. # 如果你經(jīng)常增加新查詢到已有的系統(tǒng)內(nèi)的話. 一般來說這是一個(gè)好主意 slow_query_log # All queries taking more than this amount of time (in seconds) will be # trated as slow. Do not use "1" as a value here, as this will result in # even very fast queries being logged from time to time (as MySQL # currently measures time with second accuracy only). # 所有的使用了比這個(gè)時(shí)間(以秒為單位)更多的查詢會被認(rèn)為是慢速查詢. # 不要在這里使用”1″, 否則會導(dǎo)致所有的查詢,甚至非??斓牟樵冺摫挥涗浵聛?由于MySQL 目前時(shí)間的精確度只能達(dá)到秒的級別). long_query_time = 2 # *** Replication related settings # *** 主從復(fù)制相關(guān)的設(shè)置 # Unique server identification number between 1 and 2^32-1. This value # is required for both master and slave hosts. It defaults to 1 if # "master-host" is not set, but will MySQL will not function as a master # if it is omitted. # 唯一的服務(wù)辨識號,數(shù)值位于 1 到 2^32-1之間. # 此值在master和slave上都需要設(shè)置. # 如果 “master-host” 沒有被設(shè)置,則默認(rèn)為1, 但是如果忽略此選項(xiàng),MySQL不會作為master生效. server-id = 1 # Replication Slave (comment out master section to use this) #復(fù)制的Slave (去掉master段的注釋來使其生效) # # To configure this host as a replication slave, you can choose between # two methods : #為了配置此主機(jī)作為復(fù)制的slave服務(wù)器,你可以選擇兩種方法: # # 1) Use the CHANGE MASTER TO command (fully described in our manual) - # the syntax is: #使用 CHANGE MASTER TO 命令 (在我們的手冊中有完整描述) - # 語法如下: # # CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>, # MASTER_USER=<user>, MASTER_PASSWORD=<password> ; # # where you replace <host>, <user>, <password> by quoted strings and # <port> by the master's port number (3306 by default). # 你需要替換掉 , , 等被尖括號包圍的字段以及使用master的端口號替換 (默認(rèn)3306). # 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 # changes in this file to the variable 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) # #設(shè)置以下的變量. 不論如何, 在你選擇這種方法的情況下, 然后第一次啟動復(fù)制(甚至不成功的情況下, # 例如如果你輸入錯(cuò)密碼在master-password字段并且slave無法連接), # slave會創(chuàng)建一個(gè) master.info 文件,并且之后任何對于包含在此文件內(nèi)的參數(shù)的變化都會被忽略 # 并且由 master.info 文件內(nèi)的內(nèi)容覆蓋, 除非你關(guān)閉slave服務(wù), 刪除 master.info 并且重啟slave 服務(wù). # 由于這個(gè)原因,你也許不想碰一下的配置(注釋掉的) 并且使用 CHANGE MASTER TO (查看上面) 來代替 # 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 # 所需要的唯一id號位于 2 和 2^32 – 1之間 # (并且和master不同) # 如果master-host被設(shè)置了.則默認(rèn)值是2 # 但是如果省略,則不會生效 #server-id = 2 # # The replication master for this slave – required # 復(fù)制結(jié)構(gòu)中的master – 必須 #master-host = <hostname> # # The username the slave will use for authentication when connecting # to the master – required # 當(dāng)連接到master上時(shí)slave所用來認(rèn)證的用戶名 – 必須 #master-user = <username> # # The password the slave will authenticate with when connecting to # the master – required # 當(dāng)連接到master上時(shí)slave所用來認(rèn)證的密碼 – 必須 #master-password = <password> # # The port the master is listening on. # optional - defaults to 3306 # master監(jiān)聽的端口. # 可選 – 默認(rèn)是3306 #master-port = <port> # Make the slave read-only. Only users with the SUPER privilege and the # replication slave thread will be able to modify data on it. You can # use this to ensure that no applications will accidently modify data on # the slave instead of the master # 使得slave只讀.只有用戶擁有SUPER權(quán)限和在上面的slave線程能夠修改數(shù)據(jù). # 你可以使用此項(xiàng)去保證沒有應(yīng)用程序會意外的修改slave而不是master上的數(shù)據(jù) #read_only #*** MyISAM Specific options #*** MyISAM 相關(guān)選項(xiàng) # Size of the Key Buffer, used to cache index blocks for MyISAM tables. # Do not set it larger than 30% of your available memory, as some memory # is also required by the OS to cache rows. Even if you're not using # MyISAM tables, you should still set it to 8-64M as it will also be # used for internal temporary disk tables. # 關(guān)鍵詞緩沖的大小, 一般用來緩沖MyISAM表的索引塊. # 不要將其設(shè)置大于你可用內(nèi)存的30%, # 因?yàn)橐徊糠謨?nèi)存同樣被OS用來緩沖行數(shù)據(jù) # 甚至在你并不使用MyISAM 表的情況下, 你也需要仍舊設(shè)置起 8-64M 內(nèi)存由于它同樣會被內(nèi)部臨時(shí)磁盤表使用. key_buffer_size = 32M # MyISAM uses special tree-like cache to make bulk inserts (that is, # INSERT ... SELECT, INSERT ... VALUES (...), (...), ..., and LOAD DATA # INFILE) faster. This variable limits the size of the cache tree in # bytes per thread. Setting it to 0 will disable this optimisation. Do # not set it larger than "key_buffer_size" for optimal performance. # This buffer is allocated when a bulk insert is detected. # MyISAM 使用特殊的類似樹的cache來使得突發(fā)插入 # (這些插入是,INSERT … SELECT, INSERT … VALUES (…), (…), …, 以及 LOAD DATA # INFILE) 更快. 此變量限制每個(gè)進(jìn)程中緩沖樹的字節(jié)數(shù). # 設(shè)置為 0 會關(guān)閉此優(yōu)化. # 為了最優(yōu)化不要將此值設(shè)置大于 “key_buffer_size”. # 當(dāng)突發(fā)插入被檢測到時(shí)此緩沖將被分配. bulk_insert_buffer_size = 64M # This buffer is allocated when MySQL needs to rebuild the index in # REPAIR, OPTIMIZE, ALTER table statements as well as in LOAD DATA INFILE # into an empty table. It is allocated per thread so be careful with # large settings. # 此緩沖當(dāng)MySQL需要在 REPAIR, OPTIMIZE, ALTER 以及 LOAD DATA INFILE 到一個(gè)空表中引起重建索引時(shí)被分配. # 這在每個(gè)線程中被分配.所以在設(shè)置大值時(shí)需要小心. myisam_sort_buffer_size = 128M # The maximum size of the temporary file MySQL is allowed to use while # recreating the index (during REPAIR, ALTER TABLE or LOAD DATA INFILE. # If the file-size would be bigger than this, the index will be created # through the key cache (which is slower). # MySQL重建索引時(shí)所允許的最大臨時(shí)文件的大小 (當(dāng) REPAIR, ALTER TABLE 或者 LOAD DATA INFILE). # 如果文件大小比此值更大,索引會通過鍵值緩沖創(chuàng)建(更慢) myisam_max_sort_file_size = 10G # If a table has more than one index, MyISAM can use more than one # thread to repair them by sorting in parallel. This makes sense if you # have multiple CPUs and plenty of memory. # 如果一個(gè)表擁有超過一個(gè)索引, MyISAM 可以通過并行排序使用超過一個(gè)線程去修復(fù)他們. # 這對于擁有多個(gè)CPU以及大量內(nèi)存情況的用戶,是一個(gè)很好的選擇. myisam_repair_threads = 1 # Automatically check and repair not properly closed MyISAM tables. # 自動檢查和修復(fù)沒有適當(dāng)關(guān)閉的 MyISAM 表. myisam_recover # *** INNODB Specific options *** # *** INNODB 相關(guān)選項(xiàng) *** # Use this option if you have a MySQL server with InnoDB support enabled # but you do not plan to use it. This will save memory and disk space # and speed up some things. # 如果你的MySQL服務(wù)包含InnoDB支持但是并不打算使用的話, # 使用此選項(xiàng)會節(jié)省內(nèi)存以及磁盤空間,并且加速某些部分 #skip-innodb # Additional memory pool that is used by InnoDB to store metadata # information. If InnoDB requires more memory for this purpose it will # start to allocate it from the OS. As this is fast enough on most # recent operating systems, you normally do not need to change this # value. SHOW INNODB STATUS will display the current amount used. # 附加的內(nèi)存池被InnoDB用來保存 metadata 信息 # 如果InnoDB為此目的需要更多的內(nèi)存,它會開始從OS這里申請內(nèi)存. # 由于這個(gè)操作在大多數(shù)現(xiàn)代操作系統(tǒng)上已經(jīng)足夠快, 你一般不需要修改此值. # SHOW INNODB STATUS 命令會顯示當(dāng)先使用的數(shù)量. innodb_additional_mem_pool_size = 16M # InnoDB, unlike MyISAM, uses a buffer pool to cache both indexes and # row data. The bigger you set this the less disk I/O is needed to # access data in tables. On a dedicated database server you may set this # parameter up to 80% of the machine physical memory size. Do not set it # too large, though, because competition of the physical memory may # cause paging in the operating system. Note that on 32bit systems you # might be limited to 2-3.5G of user level memory per process, so do not # set it too high. # InnoDB使用一個(gè)緩沖池來保存索引和原始數(shù)據(jù), 不像 MyISAM. # 這里你設(shè)置越大,你在存取表里面數(shù)據(jù)時(shí)所需要的磁盤I/O越少. # 在一個(gè)獨(dú)立使用的數(shù)據(jù)庫服務(wù)器上,你可以設(shè)置這個(gè)變量到服務(wù)器物理內(nèi)存大小的80% # 不要設(shè)置過大,否則,由于物理內(nèi)存的競爭可能導(dǎo)致操作系統(tǒng)的換頁顛簸. # 注意在32位系統(tǒng)上你每個(gè)進(jìn)程可能被限制在 2-3.5G 用戶層面內(nèi)存限制, # 所以不要設(shè)置的太高. innodb_buffer_pool_size = 2G # InnoDB stores data in one or more data files forming the tablespace. # If you have a single logical drive for your data, a single # autoextending file would be good enough. In other cases, a single file # per device is often a good choice. You can configure InnoDB to use raw # disk partitions as well - please refer to the manual for more info # about this. # InnoDB 將數(shù)據(jù)保存在一個(gè)或者多個(gè)數(shù)據(jù)文件中成為表空間. # 如果你只有單個(gè)邏輯驅(qū)動保存你的數(shù)據(jù),一個(gè)單個(gè)的自增文件就足夠好了. # 其他情況下.每個(gè)設(shè)備一個(gè)文件一般都是個(gè)好的選擇. # 你也可以配置InnoDB來使用裸盤分區(qū) – 請參考手冊來獲取更多相關(guān)內(nèi)容 innodb_data_file_path = ibdata1:10M:autoextend # Set this option if you would like the InnoDB tablespace files to be # stored in another location. By default this is the MySQL datadir. # 設(shè)置此選項(xiàng)如果你希望InnoDB表空間文件被保存在其他分區(qū). # 默認(rèn)保存在MySQL的datadir中. #innodb_data_home_dir = <directory> # Number of IO threads to use for async IO operations. This value is # hardcoded to 8 on Unix, but on Windows disk I/O may benefit from a # larger number. # 用來同步IO操作的IO線程的數(shù)量. This value is # 此值在Unix下被硬編碼為8,但是在Windows磁盤I/O可能在一個(gè)大數(shù)值下表現(xiàn)的更好. innodb_write_io_threads = 8 innodb_read_io_threads = 8 # If you run into InnoDB tablespace corruption, setting this to a nonzero # value will likely help you to dump your tables. Start from value 1 and # increase it until you're able to dump the table successfully. # 如果你發(fā)現(xiàn)InnoDB表空間損壞, 設(shè)置此值為一個(gè)非零值可能幫助你導(dǎo)出你的表. # 從1開始并且增加此值知道你能夠成功的導(dǎo)出表. #innodb_force_recovery=1 # Number of threads allowed inside the InnoDB kernel. The optimal value # depends highly on the application, hardware as well as the OS # scheduler properties. A too high value may lead to thread thrashing. # 在InnoDb核心內(nèi)的允許線程數(shù)量. # 最優(yōu)值依賴于應(yīng)用程序,硬件以及操作系統(tǒng)的調(diào)度方式. # 過高的值可能導(dǎo)致線程的互斥顛簸. innodb_thread_concurrency = 16 # If set to 1, InnoDB will flush (fsync) the transaction logs to the # disk at each commit, which offers full ACID behavior. If you are # willing to compromise this safety, and you are running small # transactions, you may set this to 0 or 2 to reduce disk I/O to the # logs. Value 0 means that the log is only written to the log file and # the log file flushed to disk approximately once per second. Value 2 # means the log is written to the log file at each commit, but the log # file is only flushed to disk approximately once per second. # 如果設(shè)置為1 ,InnoDB會在每次提交后刷新(fsync)事務(wù)日志到磁盤上, # 這提供了完整的ACID行為. # 如果你愿意對事務(wù)安全折衷, 并且你正在運(yùn)行一個(gè)小的食物, 你可以設(shè)置此值到0或者2來減少由事務(wù)日志引起的磁盤I/O # 0代表日志只大約每秒寫入日志文件并且日志文件刷新到磁盤. # 2代表日志寫入日志文件在每次提交后,但是日志文件只有大約每秒才會刷新到磁盤上. innodb_flush_log_at_trx_commit = 1 # Speed up InnoDB shutdown. This will disable InnoDB to do a full purge # and insert buffer merge on shutdown. It may increase shutdown time a # lot, but InnoDB will have to do it on the next startup instead. # 加速InnoDB的關(guān)閉. 這會阻止InnoDB在關(guān)閉時(shí)做全清除以及插入緩沖合并. # 這可能極大增加關(guān)機(jī)時(shí)間, 但是取而代之的是InnoDB可能在下次啟動時(shí)做這些操作. #innodb_fast_shutdown # The size of the buffer InnoDB uses for buffering log data. As soon as # it is full, InnoDB will have to flush it to disk. As it is flushed # once per second anyway, it does not make sense to have it very large # (even with long transactions). # 用來緩沖日志數(shù)據(jù)的緩沖區(qū)的大小. # 當(dāng)此值快滿時(shí), InnoDB將必須刷新數(shù)據(jù)到磁盤上. # 由于基本上每秒都會刷新一次,所以沒有必要將此值設(shè)置的太大(甚至對于長事務(wù)而言) innodb_log_buffer_size = 8M # Size of each log file in a log group. You should set the combined size # of log files to about 25%-100% of your buffer pool size to avoid # unneeded buffer pool flush activity on log file overwrite. However, # note that a larger logfile size will increase the time needed for the # recovery process. # 在日志組中每個(gè)日志文件的大小. # 你應(yīng)該設(shè)置日志文件總合大小到你緩沖池大小的25%~100% # 來避免在日志文件覆寫上不必要的緩沖池刷新行為. # 不論如何, 請注意一個(gè)大的日志文件大小會增加恢復(fù)進(jìn)程所需要的時(shí)間. innodb_log_file_size = 256M # Total number of files in the log group. A value of 2-3 is usually good # enough. # 在日志組中的文件總數(shù). # 通常來說2~3是比較好的. innodb_log_files_in_group = 3 # Location of the InnoDB log files. Default is the MySQL datadir. You # may wish to point it to a dedicated hard drive or a RAID1 volume for # improved performance # InnoDB的日志文件所在位置. 默認(rèn)是MySQL的datadir. # 你可以將其指定到一個(gè)獨(dú)立的硬盤上或者一個(gè)RAID1卷上來提高其性能 #innodb_log_group_home_dir # Maximum allowed percentage of dirty pages in the InnoDB buffer pool. # If it is reached, InnoDB will start flushing them out agressively to # not run out of clean pages at all. This is a soft limit, not # guaranteed to be held. # 在InnoDB緩沖池中最大允許的臟頁面的比例. # 如果達(dá)到限額, InnoDB會開始刷新他們防止他們妨礙到干凈數(shù)據(jù)頁面. # 這是一個(gè)軟限制,不被保證絕對執(zhí)行. innodb_max_dirty_pages_pct = 90 # The flush method InnoDB will use for Log. The tablespace always uses # doublewrite flush logic. The default value is "fdatasync", another # option is "O_DSYNC". # InnoDB用來刷新日志的方法. # 表空間總是使用雙重寫入刷新方法 # 默認(rèn)值是 “fdatasync”, 另一個(gè)是 “O_DSYNC”. #innodb_flush_method=O_DSYNC # How long an InnoDB transaction should wait for a lock to be granted # before being rolled back. InnoDB automatically detects transaction # deadlocks in its own lock table and rolls back the transaction. If you # use the LOCK TABLES command, or other transaction-safe storage engines # than InnoDB in the same transaction, then a deadlock may arise which # InnoDB cannot notice. In cases like this the timeout is useful to # resolve the situation. # 在被回滾前,一個(gè)InnoDB的事務(wù)應(yīng)該等待一個(gè)鎖被批準(zhǔn)多久. # InnoDB在其擁有的鎖表中自動檢測事務(wù)死鎖并且回滾事務(wù). # 如果你使用 LOCK TABLES 指令, 或者在同樣事務(wù)中使用除了InnoDB以外的其他事務(wù)安全的存儲引擎 # 那么一個(gè)死鎖可能發(fā)生而InnoDB無法注意到. # 這種情況下這個(gè)timeout值對于解決這種問題就非常有幫助. innodb_lock_wait_timeout = 120 [mysqldump] # Do not buffer the whole result set in memory before writing it to # file. Required for dumping very large tables # 不要在將內(nèi)存中的整個(gè)結(jié)果寫入磁盤之前緩存. 在導(dǎo)出非常巨大的表時(shí)需要此項(xiàng) quick max_allowed_packet = 16M [mysql] no-auto-rehash # Only allow UPDATEs and DELETEs that use keys. # 僅僅允許使用鍵值的 UPDATEs 和 DELETEs . #safe-updates [myisamchk] key_buffer_size = 512M sort_buffer_size = 512M read_buffer = 8M write_buffer = 8M [mysqlhotcopy] interactive-timeout [mysqld_safe] # Increase the amount of open files allowed per process. Warning: Make # sure you have set the global system limit high enough! The high value # is required for a large number of opened tables # 增加每個(gè)進(jìn)程的可打開文件數(shù)量. # 警告: 確認(rèn)你已經(jīng)將全系統(tǒng)限制設(shè)定的足夠高! # 打開大量表需要將此值設(shè)大 open-files-limit = 8192
三、配置文件優(yōu)化(根據(jù)實(shí)際情況優(yōu)化)
說明,上文中我對my-innodb-heavy-4G.cnf中默認(rèn)的所有選項(xiàng)進(jìn)行了說明,下面我就根據(jù)我們公司的實(shí)際情況進(jìn)行優(yōu)化!
1.服務(wù)器的運(yùn)行環(huán)境
硬件服務(wù)器:Dell R710,雙至強(qiáng)E5620 CPU、16G內(nèi)存、6*500G硬盤
操作系統(tǒng):CentOS5.5 X86_64 系統(tǒng)
Mysql版本:MySQL 5.5.32
適用于:日IP 100-200W ,日PV 200-500W 的站點(diǎn)
2.具體優(yōu)化配置如下
[client] port = 3306 socket = /tmp/mysql.sock default-character-set = utf8 #設(shè)置客戶端的字符編碼 [mysqld] # generic configuration options port = 3306 socket = /tmp/mysql.sock #*** char set *** character-set-server = utf8 #設(shè)置服務(wù)器端的字符編碼 #*** network *** back_log = 512 #skip-networking #默認(rèn)沒有開啟 max_connections = 3000 max_connect_errors = 30 table_open_cache = 4096 #external-locking #默認(rèn)沒有開啟 max_allowed_packet = 32M max_heap_table_size = 128M # *** global cache *** read_buffer_size = 8M read_rnd_buffer_size = 64M sort_buffer_size = 16M join_buffer_size = 16M # *** thread *** thread_cache_size = 16 thread_concurrency = 8 thread_stack = 512K # *** query cache *** query_cache_size = 128M query_cache_limit = 4M # *** index *** ft_min_word_len = 8 #memlock #默認(rèn)沒有開啟 default-storage-engine = INNODB transaction_isolation = REPEATABLE-READ # *** tmp table *** tmp_table_size = 64M # *** bin log *** log-bin=mysql-bin binlog_cache_size = 4M binlog_format=mixed #log_slave_updates #默認(rèn)沒有開啟 #log #默認(rèn)沒有開啟,此處是查詢?nèi)罩荆_啟會影響服務(wù)器性能 log_warnings #開啟警告日志 # *** slow query log *** slow_query_log long_query_time = 10 # *** Replication related settings server-id = 1 #server-id = 2 #master-host = <hostname> #master-user = <username> #master-password = <password> #master-port = <port> #read_only #*** MyISAM Specific options key_buffer_size = 128M bulk_insert_buffer_size = 256M myisam_sort_buffer_size = 256M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 myisam_recover # *** INNODB Specific options *** #skip-innodb #默認(rèn)沒有開啟 innodb_additional_mem_pool_size = 64M innodb_buffer_pool_size = 6G #注意在32位系統(tǒng)上你每個(gè)進(jìn)程可能被限制在 2-3.5G 用戶層面內(nèi)存限制, 所以不要設(shè)置的太高. innodb_data_file_path = ibdata1:10M:autoextend #innodb_data_home_dir = <directory> innodb_write_io_threads = 8 innodb_read_io_threads = 8 #innodb_force_recovery=1 innodb_thread_concurrency = 16 innodb_flush_log_at_trx_commit = 2 #說明:innodb_flush_log_at_trx_commit = 2 如果是游戲服務(wù)器,建議此值設(shè)置為2;如果是對數(shù)據(jù)安全要求極高的應(yīng)用,建議設(shè)置為1;設(shè)置為0性能最高,但如果發(fā)生故障,數(shù)據(jù)可能會有丟失的危險(xiǎn)!默認(rèn)值1的意思是每一次事務(wù)提交或事務(wù)外的指令都需要把日志寫入(flush)硬盤,這是很費(fèi)時(shí)的。特別是使用電池供電緩存(Battery backed up cache)時(shí)。設(shè)成2對于很多運(yùn)用,特別是從MyISAM表轉(zhuǎn)過來的是可以的,它的意思是不寫入硬盤而是寫入系統(tǒng)緩存。日志仍然會每秒flush到硬盤,所以你一般不會丟失超過1-2秒的更新。設(shè)成0會更快一點(diǎn),但安全方面比較差,即使MySQL掛了也可能會丟失事務(wù)的數(shù)據(jù)。而值2只會在整個(gè)操作系統(tǒng)掛了時(shí)才可能丟數(shù)據(jù)。 #innodb_fast_shutdown innodb_log_buffer_size = 16M innodb_log_file_size = 512M innodb_log_files_in_group = 3 #innodb_log_group_home_dir innodb_max_dirty_pages_pct = 90 #innodb_flush_method=O_DSYNC innodb_lock_wait_timeout = 120 [mysqldump] quick max_allowed_packet = 32M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 2048M sort_buffer_size = 2048M read_buffer = 32M write_buffer = 32M [mysqlhotcopy] interactive-timeout [mysqld_safe] open-files-limit = 10240
3.總結(jié)
MySQL 配置文件的優(yōu)化是根據(jù)線上環(huán)境的實(shí)際需要進(jìn)行優(yōu)化,不能隨便沒有根據(jù)的進(jìn)行優(yōu)化,寫這篇博文就是給博友們一些參考!
4.MySQL狀態(tài)查看的常用命令
mysql> show status; #顯示狀態(tài)信息 mysql> show variables; #顯示系統(tǒng)變量 mysql> show engines; #查看所有引擎 mysql> show engine innodb status; #顯示InnoDB存儲引擎的狀態(tài)
本文出自 “Share your knowledge …” 博客
經(jīng)過腳本之家小編的測試,這樣的效率很不錯(cuò),直接拿來就可以使用。
相關(guān)文章
深入探究MySQL事務(wù)實(shí)現(xiàn)原理
數(shù)據(jù)庫事務(wù)是指一組數(shù)據(jù)庫操作,這些操作必須被視為一個(gè)不可分割的單元,要么全部執(zhí)行成功,要么全部失敗回滾,本文詳細(xì)的給大家介紹了MySQL事務(wù)的實(shí)現(xiàn)原理,對我們學(xué)習(xí)MySQL有一定的幫助,感興趣的同學(xué)可以跟著小編一起來探究2023-06-06navicat不能創(chuàng)建函數(shù)解決方法分享
這篇文章主要介紹了navicat不能創(chuàng)建函數(shù)解決方法分享,小編覺得還是挺不錯(cuò)的,這里分享給大家,供需要的朋友參考。2017-10-10MySQL高效導(dǎo)入多個(gè).sql文件方法詳解
MySQL有多種方法導(dǎo)入多個(gè).sql文件,常用的有兩個(gè)命令:mysql和source,如何提高導(dǎo)入速度,在導(dǎo)入大的sql文件時(shí),建議使用mysql命令2018-10-10MySQL從命令行導(dǎo)入SQL腳本時(shí)出現(xiàn)中文亂碼的解決方法
這篇文章主要介紹了MySQL從命令行導(dǎo)入SQL腳本時(shí)出現(xiàn)中文亂碼的解決方法,分析了中文亂碼出現(xiàn)的原因并給出了兩種解決方法供大家參考,需要的朋友可以參考下2016-09-09