本次案例的數據庫為 Percona-Server-5.7.17-11
起因是 導入大量數據的時候, 數據庫被oom kill 了
查看 /var/log/message 發現如下信息 :
Dec 12 14:27:21 localhost kernel: Out of memory: Kill process 2904 (mysqld) score 958 or sacrifice child Dec 12 14:27:21 localhost kernel: Killed process 2904 (mysqld) total-vm:8670416kB, anon-rss:7580372kB, file-rss:0kB, shmem-rss:0kB
最開始是懷疑是內存不足,在磁盤性能充足的情況下打開了swap ,但結果還是OOM:
Dec 12 14:49:05 localhost kernel: Out of memory: Kill process 8009 (mysqld) score 975 or sacrifice child Dec 12 14:49:05 localhost kernel: Killed process 8009 (mysqld) total-vm:19620452kB, anon-rss:7566848kB, file-rss:0kB, shmem-rss:0kB
由於數據庫使用量很少, 應該不至於OOM的, 通過檢查 /etc/my.cnf 最終發現一個可疑配置導致了內存不足
open_files_limit=65535
這個配置本來是控制文件打開數的
文件打開數 open_files_limit mysql> show variables like 'open%'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | open_files_limit | 1024 | #mysql總共能夠打開的文件的數量 +------------------+-------+ mysql> show global status like 'open%file%'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Open_files | 79 | # 系統當前打開的文件數 | Opened_files | 278 | # 系統打開過的文件總數 +---------------+-------+ 比較合適的設置:Open_files / open_files_limit * 100% <= 75%
最后將配置設置為10000后 ,問題消失 ,數據庫啟動后占用的內存也沒這么多了
官方文檔說明如下
open_files_limit
System Variable Name open_files_limit
Scope Global
Dynamic No
Permitted Values Type integer
Default 5000, with possible adjustment
Minimum 0
Maximum platform dependent
The number of files that the operating system permits mysqld to open. The value of this variable at runtime is the real value permitted by the system and might be different from the value you specify at server startup. The value is 0 on systems where MySQL cannot change the number of open files.
The effective open_files_limit value is based on the value specified at system startup (if any) and the values of max_connections and table_open_cache, using these formulas:
1) 10 + max_connections + (table_open_cache * 2)
2) max_connections * 5
3) operating system limit if positive
4) if operating system limit is Infinity:
open_files_limit value specified at startup, 5000 if none
The server attempts to obtain the number of file descriptors using the maximum of those three values. If that many descriptors cannot be obtained, the server attempts to obtain as many as the system will permit.
