Centos運行Mysql因爲內存不足進程被殺

今天剛剛申請了一個新的域名,在申請完域名剛準備綁定給小夥伴分享註冊新域名的喜悅時,剛把網站發到我們小夥伴們的討論羣裏,卻發現訪問不了了,提示,數據庫連接失敗!

真的時一個尷尬.....    所有人都進不了我的網站,然後登錄centos後臺,發現了這樣的提示



out of memory !  Kill   process (Mysqld)  我英語未過四級,但是這個簡單英文還是能看懂的,大致意思時內存不夠了,然後把mysql的進程給殺掉了,阿西吧!!!


尷尬了,怎麼辦?  小夥伴們不能讓我太尷尬啊,所以給我提了建議,說可以通過修改mysql的配置文件my.ini實現降低mysql的運行所佔用內容。

然後找了資料,有一片就不錯的文章,在這裏就不造輪子重寫了,在此轉載過來,同時表示謝意。

1G 內存如何優化mysql

文章內容如下:  供大家參考

同時在線訪問量繼續增大 對於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 可以看到目前的 
參數 有3個配置參數是最重要的,即 
key_buffer_size,query_cache_size,table_cache
1.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,以下的狀態值都是本人通過phpmyadmin 獲得的。
實例分析:
這個服務器已經運行了20 天
key_buffer_size – 128M
key_read_requests – 650759289
key_reads - 79112
比例接近1:8000 健康狀況非常好 ,另外一個估計key_buffer_size 的辦法 把你網站數據庫的每個表的索引所佔空間大小加起來看看 
以此服務器爲例:比較大的幾個表索引加起來大概125M 這個數字會隨着表變大 而變大 
2.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 的值非常大,則表明查詢緩衝使用非常頻繁,此時需要增加緩衝大小 
Qcache_hits 的值不大,則表明你的查詢重複率很低,這種情況下使用查詢緩衝反而會影響效率,那麼可以考慮不用查詢緩衝。此外,在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 應該要往上加點
3.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 
4.log-bin
如果你不需要記錄2 進制log 就把這個功能關掉,注意關掉以後就不能恢復出問 題前的數據了,需要您手動備份,二進制日誌包含所有更新數據的語句,其目的是在恢復數據庫時用它來把數據
儘可能恢復到最後的狀態。另 外,如果做同步複製( Replication )的話,也需要使用二進制日誌傳送修改情況。 log_bin 指定日誌文件,如果不提供文件名,MySQL 將自己產生缺省文件名。MySQL會在文件名後面自動添加數字引,每次啓動服務時,都會重新生成一個新的二進制文件。此外,使用log-bin-index 可以指定索引文件;使用binlog-do-db 可以指定記 
錄的數據庫;使用binlogignore- db 可以指定不記錄的數據庫。注意的是:binlog-do-db和binlog-ignore-db 一次只指定一個數據庫,指定多個數據庫需要多個語句。而且,MySQL會將所有的數據庫名稱改成小寫,在指定數據庫時必須全部使用小寫名字,否則不會起作用。 
關掉這個功能只需要在他前面加上#號
#log-bin
5.開啓慢查詢日誌( 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
6.其他一些重要參數
筆者設置:
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 連接的偵聽隊列的大小。你的操作系統在這個隊列大小上有它自己的限制。 檢查你的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 全部文件


[sql] view plain copy
  1. # Example MySQL config file for medium systems.  
  2. #  
  3. # This is for a system with little memory (32M - 64M) where MySQL plays  
  4. # an important part, or systems up to 128M where MySQL is used together  
  5. with  
  6. # other programs (such as a web server)  
  7. directory is /var/lib/mysql) or  
  8. # ~/.my.cnf to set user-specific options.  
  9. #  
  10. In this file, you can use all long options that a program supports.  
  11. # If you want to know which options a program supports, run the program  
  12. with the "--help" option.  
  13. # The following options will be passed to all MySQL clients  
  14. [client]  
  15. #password = your_password  
  16. port = 3306  
  17. socket = /tmp/mysql.sock  
  18. #socket = /var/lib/mysql/mysql.sock  
  19. # Here follows entries for some specific programs  
  20. # The MySQL server  
  21. [mysqld]  
  22. port = 3306  
  23. socket = /tmp/mysql.sock  
  24. #socket = /var/lib/mysql/mysql.sock  
  25. skip-locking  
  26. key_buffer = 128M  
  27. max_allowed_packet = 1M  
  28. table_cache = 256  
  29. sort_buffer_size = 1M  
  30. net_buffer_length = 16K  
  31. myisam_sort_buffer_size = 1M  
  32. max_connections=120  
  33. #addnew config  
  34. wait_timeout =120  
  35. back_log=100  
  36. read_buffer_size = 1M  
  37. thread_cache=32  
  38. skip-innodb  
  39. skip-bdb  
  40. skip-name-resolve  
  41. join_buffer_size=512k  
  42. query_cache_size = 32M  
  43. interactive_timeout=120  
  44. long_query_time=10  
  45. TCP/IP port at all. This can be a security enhancement,  
  46. # if all processes that need to connect to mysqld run on the same host.  
  47. All interaction with mysqld must be made via Unix sockets or named pipes.  
  48. # Note that using this option without enabling named pipes on Windows  
  49. # (via the "enable-named-pipe" option) will render mysqld useless!  
  50. #  
  51. #skip-networking  
  52. # Replication Master Server (default)  
  53. binary logging is required for replication  
  54. #log-bin  
  55. # required unique id between 1 and 2^32 - 1  
  56. # defaults to 1 if master-host is not set  
  57. # but will not function as a master if omitted  
  58. server-id = 1  
  59. # Replication Slave (comment out master section to use this)  
  60. #  
  61. To configure this host as a replication slave, you can choose between  
  62. # two methods :  
  63. #  
  64. # 1) Use the CHANGE MASTER TO command (fully described in our manual) -  
  65. # the syntax is:  
  66. #  
  67. # CHANGE MASTER TO MASTER_HOST=, MASTER_PORT= ,  
  68. # MASTER_USER=, MASTER_PASSWORD= ;  
  69. #  
  70. where you replace , , by quoted strings and  
  71. by the master's port number (3306 by default).  
  72. #  
  73. # Example:  
  74. #  
  75. # CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,  
  76. # MASTER_USER='joe', MASTER_PASSWORD='secret';  
  77. #  
  78. OR  
  79. #  
  80. # 2) Set the variables below. However, in case you choose this method,  
  81. then  
  82. # start replication for the first time (even unsuccessfully, for example  
  83. # if you mistyped the password in master-password and the slave fails to  
  84. connect), the slave will create a master.info file, and any later  
  85. # change in this file to the variables' values below will be ignored and  
  86. # overridden by the content of the master.info file, unless you shutdown  
  87. # the slave server, delete master.info and restart the slaver server.  
  88. For that reason, you may want to leave the lines below untouched  
  89. # (commented) and instead use CHANGE MASTER TO (see above)  
  90. #  
  91. # required unique id between 2 and 2^32 - 1  
  92. # (and different from the master)  
  93. # defaults to 2 if master-host is set  
  94. # but will not function as a slave if omitted  
  95. #server-id = 2  
  96. #  
  97. # The replication master for this slave - required  
  98. #master-host =  
  99. #  
  100. # The username the slave will use for authentication when connecting  
  101. to the master - required  
  102. #master-user =  
  103. #  
  104. # The password the slave will authenticate with when connecting to  
  105. # the master - required  
  106. #master-password =  
  107. #  
  108. # The port the master is listening on.  
  109. # optional - defaults to 3306  
  110. #master-port =  
  111. #  
  112. binary logging - not required for slaves, but recommended  
  113. #log-bin  
  114. # Point the following paths to different dedicated disks  
  115. #tmpdir = /tmp/  
  116. #log-update = /path-to-dedicated-directory/hostname  
  117. # Uncomment the following if you are using BDB tables  
  118. #bdb_cache_size = 4M  
  119. #bdb_max_lock = 10000  
  120. # Uncomment the following if you are using InnoDB tables  
  121. #innodb_log_buffer_size = 8M  
  122. #innodb_flush_log_at_trx_commit = 1  
  123. #innodb_lock_wait_timeout = 50  
  124. [mysqldump]  
  125. quick  
  126. max_allowed_packet = 16M  
  127. [mysql]  
  128. no-auto-rehash  
  129. # Remove the next comment character if you are not familiar with SQL  
  130. #safe-updates  
  131. [isamchk]  
  132. key_buffer = 20M  
  133. sort_buffer_size = 20M  
  134. read_buffer = 2M  
  135. write_buffer = 2M  
  136. [myisamchk]  
  137. key_buffer = 20M  
  138. sort_buffer_size = 20M  
  139. read_buffer = 2M  
  140. write_buffer = 2M  
  141. [mysqlhotcopy]  
  142. interactive-timeout  


[sql] view plain copy
  1. # Example MySQL config file for medium systems.  
  2. #  
  3. # This is for a system with little memory (32M - 64M) where MySQL plays  
  4. # an important part, or systems up to 128M where MySQL is used together  
  5. with  
  6. # other programs (such as a web server)  
  7. directory is /var/lib/mysql) or  
  8. # ~/.my.cnf to set user-specific options.  
  9. #  
  10. In this file, you can use all long options that a program supports.  
  11. # If you want to know which options a program supports, run the program  
  12. with the "--help" option.  
  13. # The following options will be passed to all MySQL clients  
  14. [client]  
  15. #password = your_password  
  16. port = 3306  
  17. socket = /tmp/mysql.sock  
  18. #socket = /var/lib/mysql/mysql.sock  
  19. # Here follows entries for some specific programs  
  20. # The MySQL server  
  21. [mysqld]  
  22. port = 3306  
  23. socket = /tmp/mysql.sock  
  24. #socket = /var/lib/mysql/mysql.sock  
  25. skip-locking  
  26. key_buffer = 128M  
  27. max_allowed_packet = 1M  
  28. table_cache = 256  
  29. sort_buffer_size = 1M  
  30. net_buffer_length = 16K  
  31. myisam_sort_buffer_size = 1M  
  32. max_connections=120  
  33. #addnew config  
  34. wait_timeout =120  
  35. back_log=100  
  36. read_buffer_size = 1M  
  37. thread_cache=32  
  38. skip-innodb  
  39. skip-bdb  
  40. skip-name-resolve  
  41. join_buffer_size=512k  
  42. query_cache_size = 32M  
  43. interactive_timeout=120  
  44. long_query_time=10  
  45. TCP/IP port at all. This can be a security enhancement,  
  46. # if all processes that need to connect to mysqld run on the same host.  
  47. All interaction with mysqld must be made via Unix sockets or named pipes.  
  48. # Note that using this option without enabling named pipes on Windows  
  49. # (via the "enable-named-pipe" option) will render mysqld useless!  
  50. #  
  51. #skip-networking  
  52. # Replication Master Server (default)  
  53. binary logging is required for replication  
  54. #log-bin  
  55. # required unique id between 1 and 2^32 - 1  
  56. # defaults to 1 if master-host is not set  
  57. # but will not function as a master if omitted  
  58. server-id = 1  
  59. # Replication Slave (comment out master section to use this)  
  60. #  
  61. To configure this host as a replication slave, you can choose between  
  62. # two methods :  
  63. #  
  64. # 1) Use the CHANGE MASTER TO command (fully described in our manual) -  
  65. # the syntax is:  
  66. #  
  67. # CHANGE MASTER TO MASTER_HOST=, MASTER_PORT= ,  
  68. # MASTER_USER=, MASTER_PASSWORD= ;  
  69. #  
  70. where you replace , , by quoted strings and  
  71. by the master's port number (3306 by default).  
  72. #  
  73. # Example:  
  74. #  
  75. # CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,  
  76. # MASTER_USER='joe', MASTER_PASSWORD='secret';  
  77. #  
  78. OR  
  79. #  
  80. # 2) Set the variables below. However, in case you choose this method,  
  81. then  
  82. # start replication for the first time (even unsuccessfully, for example  
  83. # if you mistyped the password in master-password and the slave fails to  
  84. connect), the slave will create a master.info file, and any later  
  85. # change in this file to the variables' values below will be ignored and  
  86. # overridden by the content of the master.info file, unless you shutdown  
  87. # the slave server, delete master.info and restart the slaver server.  
  88. For that reason, you may want to leave the lines below untouched  
  89. # (commented) and instead use CHANGE MASTER TO (see above)  
  90. #  
  91. # required unique id between 2 and 2^32 - 1  
  92. # (and different from the master)  
  93. # defaults to 2 if master-host is set  
  94. # but will not function as a slave if omitted  
  95. #server-id = 2  
  96. #  
  97. # The replication master for this slave - required  
  98. #master-host =  
  99. #  
  100. # The username the slave will use for authentication when connecting  
  101. to the master - required  
  102. #master-user =  
  103. #  
  104. # The password the slave will authenticate with when connecting to  
  105. # the master - required  
  106. #master-password =  
  107. #  
  108. # The port the master is listening on.  
  109. # optional - defaults to 3306  
  110. #master-port =  
  111. #  
  112. binary logging - not required for slaves, but recommended  
  113. #log-bin  
  114. # Point the following paths to different dedicated disks  
  115. #tmpdir = /tmp/  
  116. #log-update = /path-to-dedicated-directory/hostname  
  117. # Uncomment the following if you are using BDB tables  
  118. #bdb_cache_size = 4M  
  119. #bdb_max_lock = 10000  
  120. # Uncomment the following if you are using InnoDB tables  
  121. #innodb_log_buffer_size = 8M  
  122. #innodb_flush_log_at_trx_commit = 1  
  123. #innodb_lock_wait_timeout = 50  
  124. [mysqldump]  
  125. quick  
  126. max_allowed_packet = 16M  
  127. [mysql]  
  128. no-auto-rehash  
  129. # Remove the next comment character if you are not familiar with SQL  
  130. #safe-updates  
  131. [isamchk]  
  132. key_buffer = 20M  
  133. sort_buffer_size = 20M  
  134. read_buffer = 2M  
  135. write_buffer = 2M  
  136. [myisamchk]  
  137. key_buffer = 20M  
  138. sort_buffer_size = 20M  
  139. read_buffer = 2M  
  140. write_buffer = 2M  
  141. [mysqlhotcopy]  
  142. interactive-timeout  
發佈了53 篇原創文章 · 獲贊 35 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章