從年后換了工作到現在差不多兩個月了,比較忙,所以寫博客的時間越來越少了。
以前學生時代用MySQL,從安裝開始就是“下一步”,設置向導弄中設置用戶、端口、編碼什么的就好了。后來工作了公司用的Oracle,但是普通程序員也接觸不到。現在正好有機會自己去接觸一下數據庫(MySQL),盡力把握住,搞清楚MySQL源碼是如何編譯的,是怎么配置的,存儲引擎之間的區別等等。
這片博文記錄一下自己理解的my.cnf(Window平台上的是my.ini)中的配置,這些配置官網應該都是有解釋的吧?!可能是我自己沒有找到好的資源。
my.cnf默認是不存在的,可以在mysql的安裝目錄的support-files中看到下面這些文件:
可以將my-huge.cnf、my-innodb-heavy-4G.cnf、my-large.cnf、my-medium.cnf、my-small.cnf中合適自己機器的文件拷貝命名成my.cnf。
下面詳細看一下這幾個配置文件的內容。
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.
# 一個擁有1至2G內存,主要用於運行MySQL的系統
# 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.
# The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3306 端口號
socket = /tmp/mysql.sock mysql.sock文件是服務器與本地客戶端進行通信的Unix套接字文件
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
port = 3306 端口號
socket = /tmp/mysql.sock mysql.sock文件是服務器與本地客戶端進行通信的Unix套接字文件
skip-locking 避免MySQL的外部鎖定,減少出錯幾率增強穩定性。
key_buffer_size = 384M 指定索引緩沖區的大小,它決定索引處理的速度,尤其是索引讀的速度
通過檢查狀態值Key_read_requests和Key_reads,可以知道key_buffer_size設置是否合理。比例key_reads /key_read_requests應該盡可能的低,至少是1:100,1:1000更好(上述狀態值可以使用SHOW STATUS LIKE ‘key_read%’獲得)
max_allowed_packet = 1M 設置最大包,限制server接受的數據包大小,避免超長SQL的執行有問題(當MySQL客戶端或mysqld服務器收到大於max_allowed_packet字節的信息包時,將發出“信息包過大”錯誤,並關閉連接)
table_open_cache = 512 mysql每打開一個表都會讀入一些數據到table_open_cache中,當mysql在這個緩存中找不到對應的信息時,才會去磁盤上直接讀取
sort_buffer_size = 2M 查詢排序時所能使用的緩沖區大小。注意:該參數對應的分配內存是每連接獨占!如果有100個連接,那么實際分配的總共排序緩沖區大小為100 × 2 = 200MB。所以,對於內存在4GB左右的服務器推薦設置為6-8M。
read_buffer_size = 2M 讀查詢操作所能使用的緩沖區大小。和sort_buffer_size一樣,該參數對應的分配內存也是每連接獨享!read_buffer_size只對MyISAM表有效
read_rnd_buffer_size = 8M 對所有存儲引擎的表都有限
myisam_sort_buffer_size = 64M MySQL重建索引時所允許的最大臨時文件的大小 (當 REPAIR, ALTER TABLE 或者 LOAD DATA INFILE)
thread_cache_size = 8 服務器線程緩存這個值表示可以重新利用保存在緩存中線程的數量,當斷開連接時如果緩存中還有空間,那么客戶端的線程將被放到緩存中,如果線程重新被請求,那么請求將從緩存中讀取,如果緩存中是空的或者是新的請求,那么這個線程將被重新創建,如果有很多新的線程,增加這個值可以改善系統性能
query_cache_size = 32M query_cache_size的工作原理:一個SELECT查詢在DB中工作后,DB會把該語句緩存下來,當同樣的一個SQL再次來到DB里調用時,DB在該表沒發生變化的情況下把結果從緩存中返回給Client。這里有一個關建點,就是DB在利用Query_cache工作時,要求該語句涉及的表在這段時間內沒有發生變更。那如果該表在發生變更時,Query_cache里的數據又怎么處理呢?首先要把Query_cache和該表相關的語句全部置為失效,然后在寫入更新。那么如果Query_cache非常大,該表的查詢結構又比較多,查詢語句失效也慢,一個更新或是Insert就會很慢,這樣看到的就是Update或是Insert怎么這么慢了。所以在數據庫寫入量或是更新量也比較大的系統,該參數不適合分配過大。而且在高並發,寫入量大的系統,建系把該功能禁掉。
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8 設置thread_concurrency的值的正確與否, 對mysql的性能影響很大, 在多個cpu(或多核)的情況下,錯誤設置了thread_concurrency的值, 會導致mysql不能充分利用多cpu(或多核), 出現同一時刻只能一個cpu(或核)在工作的情況。thread_concurrency應設為CPU核數的2倍. 比如有一個雙核的CPU, 那么thread_concurrency的應該為4; 2個雙核的cpu, thread_concurrency的值應為8
# 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 開啟該選項可以徹底關閉MySQL的TCP/IP連接方式,如果WEB服務器是以遠程連接的方式訪問MySQL數據庫服務器則不要開啟該選項!否則將無法正常連接!
# Replication Master Server (default)
# binary logging is required for replication
log-bin=mysql-bin 二進制日志包含所有更新數據的語句,其目的是在恢復數據庫時用它來把數據盡可能恢復到最后的狀態。另外,如果做同步復制( Replication )的話,也需要使用二進制日志傳送修改情況。 開啟二進制日志,需要設置參數log-bin。log_bin指定日志文件,如果不提供文件名,MySQL將自己產生缺省文件名
# 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=<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).
#
# 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 = <hostname>
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user = <username>
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password = <password>
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port = <port>
#
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin
#
# binary logging format - mixed recommended
#binlog_format=mixed
# Uncomment the following if you are using InnoDB tables 使用innoDB的配置
#innodb_data_home_dir = /usr/local/mysql/data INNODB保存數據目錄
#innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend 指定表數據和索引存儲的空間,可以是一個或者多個文件。最后一個數據文件必須是自動擴充的,也只有最后一 個文件允許自動擴充。這樣,當空間用完后,自動擴充數據文件就會自動增長
#innodb_log_group_home_dir = /usr/local/mysql/data 日志位置
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 384M 定義了 InnoDB 存儲引擎的表數據和索引數據的最大內存緩沖區大小。和 MyISAM 存儲引擎不同, MyISAM 的 key_buffer_size 只能緩存索引鍵,而 innodb_buffer_pool_size 卻可以緩存數據塊和索引鍵。適當的增加這個參數的大小,可以有效的減少 InnoDB 類型的表的磁盤 I/O 。在一個以 InnoDB 為主的專用數據庫服務器上,可以考慮把該參數設置為物理內存大小的 60%-80% 。
#innodb_additional_mem_pool_size = 20M 這個參數用來設置 InnoDB 存儲的數據目錄信息和其它內部數據結構的內存池大小。應用程序里的表越多,你需要在這里分配越多的內存。對於一個相對穩定的應用,這個參數的大小也是相對 穩定的,也沒有必要預留非常大的值。如果 InnoDB 用光了這個池內的內存, InnoDB 開始從操作系統分配內存,並且往 MySQL 錯誤日志寫警告信息。默認值是 1MB ,當發現錯誤日志中已經有相關的警告信息時,就應該適當的增加該參數的大小。
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 100M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1如果設置為1,InnoDB會在每個提交磁盤沖洗事務日志,這提供了完整的ACID的行為。如果你想安全,並且正在執行一些小操作,你可以設置為0或者2在減少磁盤I / O日志。
#innodb_lock_wait_timeout = 50 InnoDB 有其內置的死鎖檢測機制,能導致未完成的事務回滾。但是,如果結合InnoDB使用MyISAM的lock tables 語句或第三方事務引擎,則InnoDB無法識別死鎖。為消除這種可能性,可以將innodb_lock_wait_timeout設置為一個整數值,指示 MySQL在允許其他事務修改那些最終受事務回滾的數據之前要等待多長時間(秒數)
[mysqldump]
quick 沒有指定 --quick 或 --opt 選項,則會將整個結果集放在內存中。如果導出大數據庫的話可能會出現問題
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout 服務器關閉交互式連接前等待活動的秒數。交互式客戶端定義為在mysql_real_connect()中使用
其他文件:my-large.cnf my-medium.cnf my-small.cnf是針對不同的機器性能的配置。my-innodb-heavy-4G.cnf是一個針對 4G 內存系統(主要運行只有 InnoDB 表的 MySQL 並使用幾個連接數執行復雜的查詢)的 MySQL 配置文件例子。
通過修改my.cnf的配置可以優化mysql的性能。my.cnf文件的存儲位置會影響配置的有效性。