MySQL服務(wù)器默認(rèn)安裝之后調(diào)節(jié)性能的方法
My favorite question during Interview for people to work as MySQL DBAs or be involved with MySQL Performance in some way is to ask them what should be tuned in MySQL Server straight after installation, assuming it was installed with default settings.
在面試MySQL DBA或者那些打算做MySQL性能優(yōu)化的人時(shí),我最喜歡問(wèn)題是:MySQL服務(wù)器按照默認(rèn)設(shè)置安裝完之后,應(yīng)該做哪些方面的調(diào)節(jié)呢?
I'm surprised how many people fail to provide any reasonable answer to this question, and how many servers are where in wild which are running with default settings.
令我很驚訝的是,有多少人對(duì)這個(gè)問(wèn)題無(wú)法給出合理的答案,又有多少服務(wù)器都運(yùn)行在默認(rèn)的設(shè)置下。
Even though you can tune quite a lot of variables in MySQL Servers only few of them are really important for most common workload. After you get these settings right other changes will most commonly offer only incremental performance improvements.
盡管你可以調(diào)節(jié)很多MySQL服務(wù)器上的變量,但是在大多數(shù)通常的工作負(fù)載下,只有少數(shù)幾個(gè)才真正重要。如果你把這些變量設(shè)置正確了,那么修改其他變量最多只能對(duì)系統(tǒng)性能改善有一定提升。
key_buffer_size - Very important if you use MyISAM tables. Set up to 30-40% of available memory if you use MyISAM tables exclusively. Right size depends on amount of indexes, data size and workload - remember MyISAM uses OS cache to cache the data so you need to leave memory for it as well, and data can be much larger than indexes in many cases. Check however if all of key_buffer is used over time - it is not rare to see key_buffer being set to 4G while combined size of .MYI files is just 1GB. This would be just a waste. If you use few MyISAM tables you'll want to keep it lower but still at least 16-32Mb so it is large enough to accommodate indexes for temporary tables which are created on disk.
key_buffer_size - 這對(duì)MyISAM表來(lái)說(shuō)非常重要。如果只是使用MyISAM表,可以把它設(shè)置為可用內(nèi)存的 30-40%。合理的值取決于索引大小、數(shù)據(jù)量以及負(fù)載 -- 記住,MyISAM表會(huì)使用操作系統(tǒng)的緩存來(lái)緩存數(shù)據(jù),因此需要留出部分內(nèi)存給它們,很多情況下數(shù)據(jù)比索引大多了。盡管如此,需要總是檢查是否所有的 key_buffer 都被利用了 -- .MYI 文件只有 1GB,而 key_buffer 卻設(shè)置為 4GB 的情況是非常少的。這么做太浪費(fèi)了。如果你很少使用MyISAM表,那么也保留低于 16-32MB 的 key_buffer_size 以適應(yīng)給予磁盤(pán)的臨時(shí)表索引所需。
innodb_buffer_pool_size This is very important variable to tune if you're using Innodb tables. Innodb tables are much more sensitive to buffer size compared to MyISAM. MyISAM may work kind of OK with default key_buffer_size even with large data set but it will crawl with default innodb_buffer_pool_size. Also Innodb buffer pool caches both data and index pages so you do not need to leave space for OS cache so values up to 70-80% of memory often make sense for Innodb only installations. Same rules as for key_buffer apply - if you have small data set and it is not going to grow dramatically do not oversize innodb_buffer_pool_size you might find better use for memory available.
innodb_buffer_pool_size - 這對(duì)Innodb表來(lái)說(shuō)非常重要。Innodb相比MyISAM表對(duì)緩沖更為敏感。MyISAM可以在默認(rèn)的 key_buffer_size 設(shè)置下運(yùn)行的可以,然而Innodb在默認(rèn)的 innodb_buffer_pool_size 設(shè)置下卻跟蝸牛似的。由于Innodb把數(shù)據(jù)和索引都緩存起來(lái),無(wú)需留給操作系統(tǒng)太多的內(nèi)存,因此如果只需要用Innodb的話(huà)則可以設(shè)置它高達(dá) 70-80% 的可用內(nèi)存。一些應(yīng)用于 key_buffer 的規(guī)則有 -- 如果你的數(shù)據(jù)量不大,并且不會(huì)暴增,那么無(wú)需把 innodb_buffer_pool_size 設(shè)置的太大了。
innodb_additional_pool_size This one does not really affect performance too much, at least on OS with decent memory allocators. Still you might want to have it 20MB (sometimes larger) so you can see how much memory Innodb allocates for misc needs.
innodb_additional_pool_size - 這個(gè)選項(xiàng)對(duì)性能影響并不太多,至少在有差不多足夠內(nèi)存可分配的操作系統(tǒng)上是這樣。不過(guò)如果你仍然想設(shè)置為 20MB(或者更大),因此就需要看一下Innodb其他需要分配的內(nèi)存有多少。
innodb_log_file_size Very important for write intensive workloads especially for large data sets. Larger sizes offer better performance but increase recovery times so be careful. I normally use values 64M-512M depending on server size.
innodb_log_file_size 在高寫(xiě)入負(fù)載尤其是大數(shù)據(jù)集的情況下很重要。這個(gè)值越大則性能相對(duì)越高,但是要注意到可能會(huì)增加恢復(fù)時(shí)間。我經(jīng)常設(shè)置為 64-512MB,跟據(jù)服務(wù)器大小而異。
innodb_log_buffer_size Default for this one is kind of OK for many workloads with medium write load and shorter transactions. If you have update activity spikes however or work with blobs a lot you might want to increase it. Do not set it too high however as it would be waste of memory - it is flushed every 1 sec anyway so you do not need space for more than 1 sec worth of updates. 8MB-16MB are typically enough. Smaller installations should use smaller values.
innodb_log_buffer_size 默認(rèn)的設(shè)置在中等強(qiáng)度寫(xiě)入負(fù)載以及較短事務(wù)的情況下,服務(wù)器性能還可以。如果存在更新操作峰值或者負(fù)載較大,就應(yīng)該考慮加大它的值了。如果它的值設(shè)置太高了,可能會(huì)浪費(fèi)內(nèi)存 -- 它每秒都會(huì)刷新一次,因此無(wú)需設(shè)置超過(guò)1秒所需的內(nèi)存空間。通常 8-16MB 就足夠了。越小的系統(tǒng)它的值越小。
innodb_flush_logs_at_trx_commit Crying about Innodb being 100 times slower than MyISAM ? You probably forgot to adjust this value. Default value of 1 will mean each update transaction commit (or each statement outside of transaction) will need to flush log to the disk which is rather expensive, especially if you do not have Battery backed up cache. Many applications, especially those moved from MyISAM tables are OK with value 2 which means do not flush log to the disk but only flush it to OS cache. The log is still flushed to the disk each second so you normally would not loose more than 1-2 sec worth of updates. Value 0 is a bit faster but is a bit less secure as you can lose transactions even in case MySQL Server crashes. Value 2 only cause data loss with full OS crash.
innodb_flush_logs_at_trx_commit 是否為Innodb比MyISAM慢1000倍而頭大?看來(lái)也許你忘了修改這個(gè)參數(shù)了。默認(rèn)值是 1,這意味著每次提交的更新事務(wù)(或者每個(gè)事務(wù)之外的語(yǔ)句)都會(huì)刷新到磁盤(pán)中,而這相當(dāng)耗費(fèi)資源,尤其是沒(méi)有電池備用緩存時(shí)。很多應(yīng)用程序,尤其是從 MyISAM轉(zhuǎn)變過(guò)來(lái)的那些,把它的值設(shè)置為 2 就可以了,也就是不把日志刷新到磁盤(pán)上,而只刷新到操作系統(tǒng)的緩存上。日志仍然會(huì)每秒刷新到磁盤(pán)中去,因此通常不會(huì)丟失每秒1-2次更新的消耗。如果設(shè)置為 0 就快很多了,不過(guò)也相對(duì)不安全了 -- MySQL服務(wù)器崩潰時(shí)就會(huì)丟失一些事務(wù)。設(shè)置為 2 指揮丟失刷新到操作系統(tǒng)緩存的那部分事務(wù)。
table_cache - Opening tables can be expensive. For example MyISAM tables mark MYI header to mark table as currently in use. You do not want this to happen so frequently and it is typically best to size your cache so it is large enough to keep most of your tables open. It uses some OS resources and some memory but for modern hardware it is typically not the problem. 1024 is good value for applications with couple hundreds tables (remember each connection needs its own entry) if you have many connections or many tables increase it larger. I've seen values over 100.000 used.
table_cache -- 打開(kāi)一個(gè)表的開(kāi)銷(xiāo)可能很大。例如MyISAM把MYI文件頭標(biāo)志該表正在使用中。你肯定不希望這種操作太頻繁,所以通常要加大緩存數(shù)量,使得足以最大限度地緩存打開(kāi)的表。它需要用到操作系統(tǒng)的資源以及內(nèi)存,對(duì)當(dāng)前的硬件配置來(lái)說(shuō)當(dāng)然不是什么問(wèn)題了。如果你有200多個(gè)表的話(huà),那么設(shè)置為 1024 也許比較合適(每個(gè)線(xiàn)程都需要打開(kāi)表),如果連接數(shù)比較大那么就加大它的值。我曾經(jīng)見(jiàn)過(guò)設(shè)置為 100,000 的情況。
thread_cache Thread creation/destructions can be expensive, which happen at each connect/disconnect. I normally set this value to at least 16. If application has large jumps in amount of concurrent connections and I see fast growth of
Threads_Created variable I boost it higher. The goal is not to have threads created in normal operation.
thread_cache -- 線(xiàn)程的創(chuàng)建和銷(xiāo)毀的開(kāi)銷(xiāo)可能很大,因?yàn)槊總€(gè)線(xiàn)程的連接/斷開(kāi)都需要。我通常至少設(shè)置為 16。如果應(yīng)用程序中有大量的跳躍并發(fā)連接并且 Threads_Created 的值也比較大,那么我就會(huì)加大它的值。它的目的是在通常的操作中無(wú)需創(chuàng)建新線(xiàn)程。
query_cache If your application is read intensive and you do not have application level caches this can be great help. Do not set it too large as it may slow things down as its maintenance may get expensive. Values from 32M to 512M normally make sense. Check it however after a while and see if it is well used. For certain workloads cache hit ratio is lower than would justify having it enabled.
query_cache -- 如果你的應(yīng)用程序有大量讀,而且沒(méi)有應(yīng)用程序級(jí)別的緩存,那么這很有用。不要把它設(shè)置太大了,因?yàn)橄胍S護(hù)它也需要不少開(kāi)銷(xiāo),這會(huì)導(dǎo)致MySQL變慢。通常設(shè)置為 32-512Mb。設(shè)置完之后最好是跟蹤一段時(shí)間,查看是否運(yùn)行良好。在一定的負(fù)載壓力下,如果緩存命中率太低了,就啟用它。
Note: as you can see all of these are global variables. These variables depend on hardware and mix of storage engines, while per session variables are typically workload specific. If you have simple queries there is no reason to increase sort_buffer_size even if you have 64GB of memory to waste. Furthermore doing so may decrease performance.
I normally leave per session variable tuning to second step after I can analyze workload.
注意:就像你看到的上面這些全局表量,它們都是依據(jù)硬件配置以及不同的存儲(chǔ)引擎而不同,但是會(huì)話(huà)變量通常是根據(jù)不同的負(fù)載來(lái)設(shè)定的。如果你只有一些簡(jiǎn)單的查詢(xún),那么就無(wú)需增加 sort_buffer_size 的值了,盡管你有 64GB 的內(nèi)存。搞不好也許會(huì)降低性能。
我通常在分析系統(tǒng)負(fù)載后才來(lái)設(shè)置會(huì)話(huà)變量。
P.S Note MySQL distribution contains bunch of sample my.cnf files which may be great templates to use. Typically they would already be much better than defaults if you chose correct one.
P.S,MySQL的發(fā)行版已經(jīng)包含了各種 my.cnf 范例文件了,可以作為配置模板使用。通常這比你使用默認(rèn)設(shè)置好的多了。
- CentOS 6.4安裝配置LAMP服務(wù)器(Apache+PHP5+MySQL)
- CentOS 6.4安裝配置LNMP服務(wù)器(Nginx+PHP+MySQL)
- CentOS 5.4 服務(wù)器配置 yum安裝Apache+php+Mysql
- Win2003服務(wù)器安裝及設(shè)置教程 MySQL安全設(shè)置圖文教程
- Linux下安裝Mysql多實(shí)例作為數(shù)據(jù)備份服務(wù)器實(shí)現(xiàn)多主到一從多實(shí)例的備份
- centos下安裝mysql服務(wù)器的方法
- apache+mysql+php+ssl服務(wù)器之完全安裝攻略
- 在linux或unix服務(wù)器上安裝、使用MySQL的注意事項(xiàng)
- win2008 r2 服務(wù)器php+mysql+sqlserver2008運(yùn)行環(huán)境配置(從安裝、優(yōu)化、安全等)
- 在一臺(tái)服務(wù)器上安裝兩個(gè)或多個(gè)mysql的實(shí)現(xiàn)步驟
相關(guān)文章
MySQL學(xué)習(xí)第五天 MySQL數(shù)據(jù)庫(kù)基本操作
MySQL學(xué)習(xí)第五天我們將針對(duì)MySQL數(shù)據(jù)庫(kù)進(jìn)行基本操作,創(chuàng)建、修改、刪除數(shù)據(jù)庫(kù)等一系列操作進(jìn)行學(xué)習(xí),感興趣的小伙伴們可以參考一下2016-05-05mysql 遠(yuǎn)程連接數(shù)據(jù)庫(kù)的方法集合
MySQL數(shù)據(jù)庫(kù)不允許從遠(yuǎn)程訪(fǎng)問(wèn)怎么辦?本文提供了三種解決方法:2008-03-03Mysql查詢(xún)語(yǔ)句執(zhí)行過(guò)程及運(yùn)行原理分析
這篇文章主要介紹了Mysql查詢(xún)語(yǔ)句執(zhí)行過(guò)程及運(yùn)行原理分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08MySQL分組查詢(xún)Group By實(shí)現(xiàn)原理詳解
在MySQL 中,GROUP BY 的實(shí)現(xiàn)同樣有多種(三種)方式,其中有兩種方式會(huì)利用現(xiàn)有的索引信息來(lái)完成 GROUP BY,另外一種為完全無(wú)法使用索引的場(chǎng)景下使用。下面我們分別針對(duì)這三種實(shí)現(xiàn)方式做一個(gè)分析2016-05-05詳解oracle mysql 中的“不等于“ <> != ^=&nbs
oracle中的 != 與 <> 和 ^= 都是不等于,邏輯上沒(méi)有區(qū)別而mysql中,<> 和 != 相同,接下來(lái)通過(guò)本文給大家詳細(xì)講解oracle mysql 中的“不等于“ <> != ^= is not,感興趣的朋友一起看看吧2023-04-04mysql觸發(fā)器之創(chuàng)建多個(gè)觸發(fā)器操作實(shí)例分析
這篇文章主要介紹了mysql觸發(fā)器之創(chuàng)建多個(gè)觸發(fā)器操作,結(jié)合實(shí)例形式分析了mysql創(chuàng)建及使用多個(gè)觸發(fā)器的相關(guān)操作技巧,需要的朋友可以參考下2019-12-12