DBA不可不知的操作系统内核参数

数据库关心的OS内核参数

512GB 内存为例

参数

fs.aio-max-nr  

支持系统

CentOS 6, 7       

参数解释

1aio-nr & aio-max-nr: 2. 3aio-nr is the running total of the number of events specified on the 4io_setup system call for all currently active aio contexts. 5. 6If aio-nr reaches aio-max-nr then io_setup will fail with EAGAIN. 7. 8Note that raising aio-max-nr does not result in the pre-allocation or re-sizing 9of any kernel data structures. 10. 11aio-nr & aio-max-nr: 12. 13aio-nr shows the current system-wide number of asynchronous io requests. 14. 15aio-max-nr allows you to change the maximum value aio-nr can grow to.

推荐设置

1fs.aio-max-nr = 1xxxxxx 2. 3PostgreSQL, Greenplum 均未使用io_setup创建aio contexts. 无需设置。 4如果Oracle数据库,要使用aio的话,需要设置它。 5设置它也没什么坏处,如果将来需要适应异步IO,可以不需要重新修改这个设置。

2.

参数

fs.file-max  

支持系统

CentOS 6, 7       

参数解释

1file-max & file-nr: 2. 3The value in file-max denotes the maximum number of file handles that the Linux kernel will allocate. 4. 5When you get lots of error messages about running out of file handles, 6you might want to increase this limit. 7. 8Historically, the kernel was able to allocate file handles dynamically, 9but not to free them again. 10. 11The three values in file-nr denote : 12the number of allocated file handles , 13the number of allocated but unused file handles , 14the maximum number of file handles. 15. 16Linux 2.6 always reports 0 as the number of free 17file handles -- this is not an error, it just means that the 18number of allocated file handles exactly matches the number of 19used file handles. 20. 21Attempts to allocate more file descriptors than file-max are reported with printk, 22look for "VFS: file-max limit <number> reached".

推荐设置

1fs.file-max = 7xxxxxxx 2. 3PostgreSQL 有一套自己管理的VFS,真正打开的FD与内核管理的文件打开关闭有一套映射的机制,所以真实情况不需要使用那么多的file handlers。 4max_files_per_process 参数。 5假设1GB内存支撑100个连接,每个连接打开1000个文件,那么一个PG实例需要打开10万个文件,一台机器按512G内存来算可以跑500PG实例,则需要5000万个file handler。 6以上设置绰绰有余。

3.

参数

kernel.core_pattern  

支持系统

CentOS 6, 7       

参数解释

1core_pattern: 2. 3core_pattern is used to specify a core dumpfile pattern name. 4. max length 128 characters; default value is "core" 5. core_pattern is used as a pattern template for the output filename; 6 certain string patterns (beginning with '%') are substituted with 7 their actual values. 8. backward compatibility with core_uses_pid: 9 If core_pattern does not include "%p" (default does not) 10 and core_uses_pid is set, then .PID will be appended to 11 the filename. 12. corename format specifiers: 13 %<NUL> '%' is dropped 14 %% output one '%' 15 %p pid 16 %P global pid (init PID namespace) 17 %i tid 18 %I global tid (init PID namespace) 19 %u uid 20 %g gid 21 %d dump mode, matches PR_SET_DUMPABLE and 22 /proc/sys/fs/suid_dumpable 23 %s signal number 24 %t UNIX time of dump 25 %h hostname 26 %e executable filename (may be shortened) 27 %E executable path 28 %<OTHER> both are dropped 29. If the first character of the pattern is a '|', the kernel will treat 30 the rest of the pattern as a command to run. The core dump will be 31 written to the standard input of that program instead of to a file.

推荐设置

1kernel.core_pattern = /xxx/core_%e_%u_%t_%s.%p 2. 3这个目录要777的权限,如果它是个软链,则真实目录需要777的权限 4mkdir /xxx 5chmod 777 /xxx 6留足够的空间

4.

参数

kernel.sem   

支持系统

CentOS 6, 7       

参数解释

1kernel.sem = 4096 2147483647 2147483646 512000 2. 34096 每组多少信号量 (>=17, PostgreSQL16个进程一组, 每组需要17个信号量) , 42147483647 总共多少信号量 (2^31-1 , 且大于4096*512000 ) , 52147483646 每个semop()调用支持多少操作 (2^31-1), 6512000 多少组信号量 (假设每GB支持100个连接, 512GB支持51200个连接, 加上其他进程, > 51200*2/16 绰绰有余) 7. 8# sysctl -w kernel.sem="4096 2147483647 2147483646 512000" 9. 10# ipcs -s -l 11 ------ Semaphore Limits -------- 12max number of arrays = 512000 13max semaphores per array = 4096 14max semaphores system wide = 2147483647 15max ops per semop call = 2147483646 16semaphore max value = 32767

推荐设置

1kernel.sem = 4096 2147483647 2147483646 512000 2. 34096可能能够适合更多的场景, 所以大点无妨,关键是512000 arrays也够了。

5.

参数

1kernel.shmall = 107374182 2kernel.shmmax = 274877906944 3kernel.shmmni = 819200

支持系统

CentOS 6, 7        

参数解释

1假设主机内存 512GB 2. 3shmmax 单个共享内存段最大 256GB (主机内存的一半,单位字节) 4shmall 所有共享内存段加起来最大 (主机内存的80%,单位PAGE) 5shmmni 一共允许创建819200个共享内存段 (每个数据库启动需要2个共享内存段。 将来允许动态创建共享内存段,可能需求量更大) 6. 7# getconf PAGE_SIZE 84096

推荐设置

1kernel.shmall = 107374182 2kernel.shmmax = 274877906944 3kernel.shmmni = 819200 4. 59.2以及以前的版本,数据库启动时,对共享内存段的内存需求非常大,需要考虑以下几点 6Connections: (1800 + 270 * max_locks_per_transaction) * max_connections 7Autovacuum workers: (1800 + 270 * max_locks_per_transaction) * autovacuum_max_workers 8Prepared transactions: (770 + 270 * max_locks_per_transaction) * max_prepared_transactions 9Shared disk buffers: (block_size + 208) * shared_buffers 10WAL buffers: (wal_block_size + 8) * wal_buffers 11Fixed space requirements: 770 kB 12. 13以上建议参数根据9.2以前的版本设置,后期的版本同样适用。

6.

参数

net.core.netdev_max_backlog  

支持系统

CentOS 6, 7     

参数解释

1netdev_max_backlog 2 ------------------ 3Maximum number of packets, queued on the INPUT side, 4when the interface receives packets faster than kernel can process them.

推荐设置

1net.core.netdev_max_backlog=1xxxx 2. 3INPUT链表越长,处理耗费越大,如果用了iptables管理的话,需要加大这个值。

7.

参数

1net.core.rmem_default 2net.core.rmem_max 3net.core.wmem_default 4net.core.wmem_max

支持系统

CentOS 6, 7     

参数解释

1rmem_default 2 ------------ 3The default setting of the socket receive buffer in bytes. 4. 5rmem_max 6 -------- 7The maximum receive socket buffer size in bytes. 8. 9wmem_default 10 ------------ 11The default setting (in bytes) of the socket send buffer. 12. 13wmem_max 14 -------- 15The maximum send socket buffer size in bytes.

推荐设置

1net.core.rmem_default = 262144 2net.core.rmem_max = 4194304 3net.core.wmem_default = 262144 4net.core.wmem_max = 4194304

8.

参数

net.core.somaxconn   

支持系统

CentOS 6, 7        

参数解释

1somaxconn - INTEGER 2 Limit of socket listen() backlog, known in userspace as SOMAXCONN. 3 Defaults to 128. 4 See also tcp_max_syn_backlog for additional tuning for TCP sockets.

推荐设置

net.core.somaxconn=4xxx    

9.

参数

net.ipv4.tcp_max_syn_backlog  

支持系统

CentOS 6, 7         

参数解释

1tcp_max_syn_backlog - INTEGER 2 Maximal number of remembered connection requests, which have not 3 received an acknowledgment from connecting client. 4 The minimal value is 128 for low memory machines, and it will 5 increase in proportion to the memory of machine. 6 If server suffers from overload, try increasing this number.

推荐设置

1net.ipv4.tcp_max_syn_backlog=4xxx 2pgpool-II 使用了这个值,用于将超过num_init_child以外的连接queue。 3所以这个值决定了有多少连接可以在队列里面等待。

10.

参数

1net.ipv4.tcp_keepalive_intvl=20 2net.ipv4.tcp_keepalive_probes=3 3net.ipv4.tcp_keepalive_time=60

支持系统

CentOS 6, 7        

参数解释

1tcp_keepalive_time - INTEGER 2 How often TCP sends out keepalive messages when keepalive is enabled. 3 Default: 2hours. 4. 5tcp_keepalive_probes - INTEGER 6 How many keepalive probes TCP sends out, until it decides that the 7 connection is broken. Default value: 9. 8. 9tcp_keepalive_intvl - INTEGER 10 How frequently the probes are send out. Multiplied by 11 tcp_keepalive_probes it is time to kill not responding connection, 12 after probes started. Default value: 75sec i.e. connection 13 will be aborted after ~11 minutes of retries.

推荐设置

1net.ipv4.tcp_keepalive_intvl=20 2net.ipv4.tcp_keepalive_probes=3 3net.ipv4.tcp_keepalive_time=60 4. 5连接空闲60秒后, 每隔20秒发心跳包, 尝试3次心跳包没有响应,关闭连接。 从开始空闲,到关闭连接总共历时120秒。

11.

参数

net.ipv4.tcp_mem=8388608 12582912 16777216    

支持系统

CentOS 6, 7    

参数解释

1tcp_mem - vector of 3 INTEGERs: min, pressure, max 2单位 page 3 min: below this number of pages TCP is not bothered about its 4 memory appetite. 5. 6 pressure: when amount of memory allocated by TCP exceeds this number 7 of pages, TCP moderates its memory consumption and enters memory 8 pressure mode, which is exited when memory consumption falls 9 under "min". 10. 11 max: number of pages allowed for queueing by all TCP sockets. 12. 13 Defaults are calculated at boot time from amount of available 14 memory. 1564GB 内存,自动计算的值是这样的 16net.ipv4.tcp_mem = 1539615 2052821 3079230 17. 18512GB 内存,自动计算得到的值是这样的 19net.ipv4.tcp_mem = 49621632 66162176 99243264 20. 21这个参数让操作系统启动时自动计算,问题也不大

推荐设置

1net.ipv4.tcp_mem=8388608 12582912 16777216 2. 3这个参数让操作系统启动时自动计算,问题也不大

12.

参数

net.ipv4.tcp_fin_timeout  

支持系统

CentOS 6, 7        

参数解释

1tcp_fin_timeout - INTEGER 2 The length of time an orphaned (no longer referenced by any 3 application) connection will remain in the FIN_WAIT_2 state 4 before it is aborted at the local end. While a perfectly 5 valid "receive only" state for an un-orphaned connection, an 6 orphaned connection in FIN_WAIT_2 state could otherwise wait 7 forever for the remote to close its end of the connection. 8 Cf. tcp_max_orphans 9 Default: 60 seconds

推荐设置

1net.ipv4.tcp_fin_timeout=5 2. 3加快僵尸连接回收速度

13.

参数

net.ipv4.tcp_synack_retries  

支持系统

CentOS 6, 7         

参数解释

1tcp_synack_retries - INTEGER 2 Number of times SYNACKs for a passive TCP connection attempt will 3 be retransmitted. Should not be higher than 255. Default value 4 is 5, which corresponds to 31seconds till the last retransmission 5 with the current initial RTO of 1second. With this the final timeout 6 for a passive TCP connection will happen after 63seconds.

推荐设置

1net.ipv4.tcp_synack_retries=2 2. 3缩短tcp syncack超时时间

14.

参数

net.ipv4.tcp_syncookies  

支持系统

CentOS 6, 7         

参数解释

1tcp_syncookies - BOOLEAN 2 Only valid when the kernel was compiled with CONFIG_SYN_COOKIES 3 Send out syncookies when the syn backlog queue of a socket 4 overflows. This is to prevent against the common 'SYN flood attack' 5 Default: 1 6. 7 Note, that syncookies is fallback facility. 8 It MUST NOT be used to help highly loaded servers to stand 9 against legal connection rate. If you see SYN flood warnings 10 in your logs, but investigation shows that they occur 11 because of overload with legal connections, you should tune 12 another parameters until this warning disappear. 13 See: tcp_max_syn_backlog, tcp_synack_retries, tcp_abort_on_overflow. 14. 15 syncookies seriously violate TCP protocol, do not allow 16 to use TCP extensions, can result in serious degradation 17 of some services (f.e. SMTP relaying), visible not by you, 18 but your clients and relays, contacting you. While you see 19 SYN flood warnings in logs not being really flooded, your server 20 is seriously misconfigured. 21. 22 If you want to test which effects syncookies have to your 23 network connections you can set this knob to 2 to enable 24 unconditionally generation of syncookies.

推荐设置

1net.ipv4.tcp_syncookies=1 2. 3防止syn flood攻击

15.

参数

net.ipv4.tcp_timestamps  

支持系统

CentOS 6, 7         

参数解释

1tcp_timestamps - BOOLEAN 2 Enable timestamps as defined in RFC1323.

推荐设置

1net.ipv4.tcp_timestamps=1 2. 3tcp_timestamps 是 tcp 协议中的一个扩展项,通过时间戳的方式来检测过来的包以防止 PAWS(Protect Against Wrapped Sequence numbers),可以提高 tcp 的性能。

16.

参数

1net.ipv4.tcp_tw_recycle 2net.ipv4.tcp_tw_reuse 3net.ipv4.tcp_max_tw_buckets

支持系统

CentOS 6, 7         

参数解释

1tcp_tw_recycle - BOOLEAN 2 Enable fast recycling TIME-WAIT sockets. Default value is 0. 3 It should not be changed without advice/request of technical 4 experts. 5. 6tcp_tw_reuse - BOOLEAN 7 Allow to reuse TIME-WAIT sockets for new connections when it is 8 safe from protocol viewpoint. Default value is 0. 9 It should not be changed without advice/request of technical 10 experts. 11. 12tcp_max_tw_buckets - INTEGER 13 Maximal number of timewait sockets held by system simultaneously. 14 If this number is exceeded time-wait socket is immediately destroyed 15 and warning is printed. 16 This limit exists only to prevent simple DoS attacks, 17 you _must_ not lower the limit artificially, 18 but rather increase it (probably, after increasing installed memory), 19 if network conditions require more than default value.

推荐设置

1net.ipv4.tcp_tw_recycle=0 2net.ipv4.tcp_tw_reuse=1 3net.ipv4.tcp_max_tw_buckets = 2xxxxx 4. 5net.ipv4.tcp_tw_recycle和net.ipv4.tcp_timestamps不建议同时开启

17.

参数

1net.ipv4.tcp_rmem 2net.ipv4.tcp_wmem

支持系统

CentOS 6, 7         

参数解释

1tcp_wmem - vector of 3 INTEGERs: min, default, max 2 min: Amount of memory reserved for send buffers for TCP sockets. 3 Each TCP socket has rights to use it due to fact of its birth. 4 Default: 1 page 5. 6 default: initial size of send buffer used by TCP sockets. This 7 value overrides net.core.wmem_default used by other protocols. 8 It is usually lower than net.core.wmem_default. 9 Default: 16K 10. 11 max: Maximal amount of memory allowed for automatically tuned 12 send buffers for TCP sockets. This value does not override 13 net.core.wmem_max. Calling setsockopt() with SO_SNDBUF disables 14 automatic tuning of that socket's send buffer size, in which case 15 this value is ignored. 16 Default: between 64K and 4MB, depending on RAM size. 17. 18tcp_rmem - vector of 3 INTEGERs: min, default, max 19 min: Minimal size of receive buffer used by TCP sockets. 20 It is guaranteed to each TCP socket, even under moderate memory 21 pressure. 22 Default: 1 page 23. 24 default: initial size of receive buffer used by TCP sockets. 25 This value overrides net.core.rmem_default used by other protocols. 26 Default: 87380 bytes. This value results in window of 65535 with 27 default setting of tcp_adv_win_scale and tcp_app_win:0 and a bit 28 less for default tcp_app_win. See below about these variables. 29. 30 max: maximal size of receive buffer allowed for automatically 31 selected receiver buffers for TCP socket. This value does not override 32 net.core.rmem_max. Calling setsockopt() with SO_RCVBUF disables 33 automatic tuning of that socket's receive buffer size, in which 34 case this value is ignored. 35 Default: between 87380B and 6MB, depending on RAM size.

推荐设置

1net.ipv4.tcp_rmem=8192 87380 16777216 2net.ipv4.tcp_wmem=8192 65536 16777216 3. 4许多数据库的推荐设置,提高网络性能

18.

参数

1net.nf_conntrack_max 2net.netfilter.nf_conntrack_max

支持系统

CentOS 6    

参数解释

1nf_conntrack_max - INTEGER 2 Size of connection tracking table. 3 Default value is nf_conntrack_buckets value * 4.

推荐设置

1net.nf_conntrack_max=1xxxxxx 2net.netfilter.nf_conntrack_max=1xxxxxx

19.

参数

1vm.dirty_background_bytes 2vm.dirty_expire_centisecs 3vm.dirty_ratio 4vm.dirty_writeback_centisecs

支持系统

CentOS 6, 7        

参数解释

1============================================================== 2. 3dirty_background_bytes 4. 5Contains the amount of dirty memory at which the background kernel 6flusher threads will start writeback. 7. 8Note: dirty_background_bytes is the counterpart of dirty_background_ratio. Only 9one of them may be specified at a time. When one sysctl is written it is 10immediately taken into account to evaluate the dirty memory limits and the 11other appears as 0 when read. 12. 13============================================================== 14. 15dirty_background_ratio 16. 17Contains, as a percentage of total system memory, the number of pages at which 18the background kernel flusher threads will start writing out dirty data. 19. 20============================================================== 21. 22dirty_bytes 23. 24Contains the amount of dirty memory at which a process generating disk writes 25will itself start writeback. 26. 27Note: dirty_bytes is the counterpart of dirty_ratio. Only one of them may be 28specified at a time. When one sysctl is written it is immediately taken into 29account to evaluate the dirty memory limits and the other appears as 0 when 30read. 31. 32Note: the minimum value allowed for dirty_bytes is two pages (in bytes); any 33value lower than this limit will be ignored and the old configuration will be 34retained. 35. 36============================================================== 37. 38dirty_expire_centisecs 39. 40This tunable is used to define when dirty data is old enough to be eligible 41for writeout by the kernel flusher threads. It is expressed in 100'ths 42of a second. Data which has been dirty in-memory for longer than this 43interval will be written out next time a flusher thread wakes up. 44. 45============================================================== 46. 47dirty_ratio 48. 49Contains, as a percentage of total system memory, the number of pages at which 50a process which is generating disk writes will itself start writing out dirty 51data. 52. 53============================================================== 54. 55dirty_writeback_centisecs 56. 57The kernel flusher threads will periodically wake up and write `old' data 58out to disk. This tunable expresses the interval between those wakeups, in 59100'ths of a second. 60. 61Setting this to zero disables periodic writeback altogether. 62. 63==============================================================

推荐设置

1vm.dirty_background_bytes = 4096000000 2vm.dirty_expire_centisecs = 6000 3vm.dirty_ratio = 80 4vm.dirty_writeback_centisecs = 50 5. 6减少数据库进程刷脏页的频率,dirty_background_bytes根据实际IOPS能力以及内存大小设置

20.

参数

vm.extra_free_kbytes  

支持系统

CentOS 6    

参数解释

1extra_free_kbytes 2. 3This parameter tells the VM to keep extra free memory 4between the threshold where background reclaim (kswapd) kicks in, 5and the threshold where direct reclaim (by allocating processes) kicks in. 6. 7This is useful for workloads that require low latency memory allocations 8and have a bounded burstiness in memory allocations, 9for example a realtime application that receives and transmits network traffic 10(causing in-kernel memory allocations) with a maximum total message burst 11size of 200MB may need 200MB of extra free memory to avoid direct reclaim 12related latencies. 13. 14目标是尽量让后台进程回收内存,比用户进程提早多少kbytes回收,因此用户进程可以快速分配内存。

推荐设置

vm.extra_free_kbytes=4xxxxxx    

21.

参数

vm.min_free_kbytes  

支持系统

CentOS 6, 7         

参数解释

1min_free_kbytes: 2. 3This is used to force the Linux VM to keep a minimum number 4of kilobytes free. The VM uses this number to compute a 5watermark[WMARK_MIN] value for each lowmem zone in the system. 6Each lowmem zone gets a number of reserved free pages based 7proportionally on its size. 8. 9Some minimal amount of memory is needed to satisfy PF_MEMALLOC 10allocations; if you set this to lower than 1024KB, your system will 11become subtly broken, and prone to deadlock under high loads. 12. 13Setting this too high will OOM your machine instantly.

推荐设置

1vm.min_free_kbytes = 2xxxxxx 2. 3防止在高负载时系统无响应,减少内存分配死锁概率。

22.

参数

vm.mmap_min_addr  

支持系统

CentOS 6, 7       

参数解释

1mmap_min_addr 2. 3This file indicates the amount of address space which a user process will 4be restricted from mmapping. Since kernel null dereference bugs could 5accidentally operate based on the information in the first couple of pages 6of memory userspace processes should not be allowed to write to them. By 7default this value is set to 0 and no protections will be enforced by the 8security module. Setting this value to something like 64k will allow the 9vast majority of applications to work correctly and provide defense in depth 10against future potential kernel bugs.

推荐设置

1vm.mmap_min_addr=6xxxx 2. 3防止内核隐藏的BUG导致的问题

23.

参数

1vm.overcommit_memory 2vm.overcommit_ratio

支持系统

CentOS 6, 7         

参数解释

1============================================================== 2. 3overcommit_kbytes: 4. 5When overcommit_memory is set to 2, the committed address space is not 6permitted to exceed swap plus this amount of physical RAM. See below. 7. 8Note: overcommit_kbytes is the counterpart of overcommit_ratio. Only one 9of them may be specified at a time. Setting one disables the other (which 10then appears as 0 when read). 11. 12============================================================== 13. 14overcommit_memory: 15. 16This value contains a flag that enables memory overcommitment. 17. 18When this flag is 0, 19the kernel attempts to estimate the amount 20of free memory left when userspace requests more memory. 21. 22When this flag is 1, 23the kernel pretends there is always enough memory until it actually runs out. 24. 25When this flag is 2, 26the kernel uses a "never overcommit" 27policy that attempts to prevent any overcommit of memory. 28Note that user_reserve_kbytes affects this policy. 29. 30This feature can be very useful because there are a lot of 31programs that malloc() huge amounts of memory "just-in-case" 32and don't use much of it. 33. 34The default value is 0. 35. 36See Documentation/vm/overcommit-accounting and 37security/commoncap.c::cap_vm_enough_memory() for more information. 38. 39============================================================== 40. 41overcommit_ratio: 42. 43When overcommit_memory is set to 2, 44the committed address space is not permitted to exceed 45 swap + this percentage of physical RAM. 46See above. 47. 48==============================================================

推荐设置

1vm.overcommit_memory = 0 2vm.overcommit_ratio = 90 3. 4vm.overcommit_memory = 0 时 vm.overcommit_ratio可以不设置

24.

参数

vm.swappiness   

支持系统

CentOS 6, 7         

参数解释

1swappiness 2. 3This control is used to define how aggressive the kernel will swap 4memory pages. 5Higher values will increase agressiveness, lower values 6decrease the amount of swap. 7. 8The default value is 60.

推荐设置

vm.swappiness = 0    

25.

参数

vm.zone_reclaim_mode   

支持系统

CentOS 6, 7         

参数解释

1zone_reclaim_mode: 2. 3Zone_reclaim_mode allows someone to set more or less aggressive approaches to 4reclaim memory when a zone runs out of memory. If it is set to zero then no 5zone reclaim occurs. Allocations will be satisfied from other zones / nodes 6in the system. 7. 8This is value ORed together of 9. 101 = Zone reclaim on 112 = Zone reclaim writes dirty pages out 124 = Zone reclaim swaps pages 13. 14zone_reclaim_mode is disabled by default. For file servers or workloads 15that benefit from having their data cached, zone_reclaim_mode should be 16left disabled as the caching effect is likely to be more important than 17data locality. 18. 19zone_reclaim may be enabled if it's known that the workload is partitioned 20such that each partition fits within a NUMA node and that accessing remote 21memory would cause a measurable performance reduction. The page allocator 22will then reclaim easily reusable pages (those page cache pages that are 23currently not used) before allocating off node pages. 24. 25Allowing zone reclaim to write out pages stops processes that are 26writing large amounts of data from dirtying pages on other nodes. Zone 27reclaim will write out dirty pages if a zone fills up and so effectively 28throttle the process. This may decrease the performance of a single process 29since it cannot use all of system memory to buffer the outgoing writes 30anymore but it preserve the memory on other nodes so that the performance 31of other processes running on other nodes will not be affected. 32. 33Allowing regular swap effectively restricts allocations to the local 34node unless explicitly overridden by memory policies or cpuset 35configurations.

推荐设置

1vm.zone_reclaim_mode=0 2. 3不使用NUMA

26.

参数

net.ipv4.ip_local_port_range  

支持系统

CentOS 6, 7         

参数解释

1ip_local_port_range - 2 INTEGERS 2 Defines the local port range that is used by TCP and UDP to 3 choose the local port. The first number is the first, the 4 second the last local port number. The default values are 5 32768 and 61000 respectively. 6. 7ip_local_reserved_ports - list of comma separated ranges 8 Specify the ports which are reserved for known third-party 9 applications. These ports will not be used by automatic port 10 assignments (e.g. when calling connect() or bind() with port 11 number 0). Explicit port allocation behavior is unchanged. 12. 13 The format used for both input and output is a comma separated 14 list of ranges (e.g. "1,2-4,10-10" for ports 1, 2, 3, 4 and 15 10). Writing to the file will clear all previously reserved 16 ports and update the current list with the one given in the 17 input. 18. 19 Note that ip_local_port_range and ip_local_reserved_ports 20 settings are independent and both are considered by the kernel 21 when determining which ports are available for automatic port 22 assignments. 23. 24 You can reserve ports which are not in the current 25 ip_local_port_range, e.g.: 26. 27 $ cat /proc/sys/net/ipv4/ip_local_port_range 28 32000 61000 29 $ cat /proc/sys/net/ipv4/ip_local_reserved_ports 30 8080,9148 31. 32 although this is redundant. However such a setting is useful 33 if later the port range is changed to a value that will 34 include the reserved ports. 35. 36 Default: Empty

推荐设置

1net.ipv4.ip_local_port_range=40000 65535 2. 3限制本地动态端口分配范围,防止占用监听端口。

27.

参数

  vm.nr_hugepages  

支持系统

CentOS 6, 7  

参数解释

1============================================================== 2nr_hugepages 3Change the minimum size of the hugepage pool. 4See Documentation/vm/hugetlbpage.txt 5============================================================== 6nr_overcommit_hugepages 7Change the maximum size of the hugepage pool. The maximum is 8nr_hugepages + nr_overcommit_hugepages. 9See Documentation/vm/hugetlbpage.txt 10. 11The output of "cat /proc/meminfo" will include lines like: 12...... 13HugePages_Total: vvv 14HugePages_Free: www 15HugePages_Rsvd: xxx 16HugePages_Surp: yyy 17Hugepagesize: zzz kB 18. 19where: 20HugePages_Total is the size of the pool of huge pages. 21HugePages_Free is the number of huge pages in the pool that are not yet 22 allocated. 23HugePages_Rsvd is short for "reserved," and is the number of huge pages for 24 which a commitment to allocate from the pool has been made, 25 but no allocation has yet been made. Reserved huge pages 26 guarantee that an application will be able to allocate a 27 huge page from the pool of huge pages at fault time. 28HugePages_Surp is short for "surplus," and is the number of huge pages in 29 the pool above the value in /proc/sys/vm/nr_hugepages. The 30 maximum number of surplus huge pages is controlled by 31 /proc/sys/vm/nr_overcommit_hugepages. 32. 33/proc/filesystems should also show a filesystem of type "hugetlbfs" configured 34in the kernel. 35. 36/proc/sys/vm/nr_hugepages indicates the current number of "persistent" huge 37pages in the kernel's huge page pool. "Persistent" huge pages will be 38returned to the huge page pool when freed by a task. A user with root 39privileges can dynamically allocate more or free some persistent huge pages 40by increasing or decreasing the value of 'nr_hugepages'.

推荐设置

1如果要使用PostgreSQL的huge page,建议设置它。 2大于数据库需要的共享内存即可。

28.

参数

  fs.nr_open

支持系统

CentOS 6, 7

参数解释

1nr_open: 2 3This denotes the maximum number of file-handles a process can 4allocate. Default value is 1024*1024 (1048576) which should be 5enough for most machines. Actual limit depends on RLIMIT_NOFILE 6resource limit. 7 8它还影响security/limits.conf 的文件句柄限制,单个进程的打开句柄不能大于fs.nr_open,所以要加大文件句柄限制,首先要加大nr_open

推荐设置

1对于有很多对象(表、视图、索引、序列、物化视图等)的PostgreSQL数据库,建议设置为2000万, 2例如fs.nr_open=20480000

数据库关心的资源限制

1. 通过/etc/security/limits.conf设置,或者ulimit设置

2. 通过/proc/$pid/limits查看当前进程的设置

1# - core - limits the core file size (KB) 2# - memlock - max locked-in-memory address space (KB) 3# - nofile - max number of open files 建议设置为1000, 但是必须设置sysctl, fs.nr_open大于它,否则会导致系统无法登陆。 4# - nproc - max number of processes 5以上四个是非常关心的配置 6.... 7# - data - max data size (KB) 8# - fsize - maximum filesize (KB) 9# - rss - max resident set size (KB) 10# - stack - max stack size (KB) 11# - cpu - max CPU time (MIN) 12# - as - address space limit (KB) 13# - maxlogins - max number of logins for this user 14# - maxsyslogins - max number of logins on the system 15# - priority - the priority to run user process with 16# - locks - max number of file locks the user can hold 17# - sigpending - max number of pending signals 18# - msgqueue - max memory used by POSIX message queues (bytes) 19# - nice - max nice priority allowed to raise to values: [-20, 19] 20# - rtprio - max realtime priority

数据库关心的IO调度规则

1. 目前操作系统支持的IO调度策略包括cfq, deadline, noop 等。

1/kernel-doc-xxx/Documentation/block 2-r--r--r-- 1 root root 674 Apr 8 16:33 00-INDEX 3-r--r--r-- 1 root root 55006 Apr 8 16:33 biodoc.txt 4-r--r--r-- 1 root root 618 Apr 8 16:33 capability.txt 5-r--r--r-- 1 root root 12791 Apr 8 16:33 cfq-iosched.txt 6-r--r--r-- 1 root root 13815 Apr 8 16:33 data-integrity.txt 7-r--r--r-- 1 root root 2841 Apr 8 16:33 deadline-iosched.txt 8-r--r--r-- 1 root root 4713 Apr 8 16:33 ioprio.txt 9-r--r--r-- 1 root root 2535 Apr 8 16:33 null_blk.txt 10-r--r--r-- 1 root root 4896 Apr 8 16:33 queue-sysfs.txt 11-r--r--r-- 1 root root 2075 Apr 8 16:33 request.txt 12-r--r--r-- 1 root root 3272 Apr 8 16:33 stat.txt 13-r--r--r-- 1 root root 1414 Apr 8 16:33 switching-sched.txt 14-r--r--r-- 1 root root 3916 Apr 8 16:33 writeback_cache_control.txt

如果你要详细了解这些调度策略的规则,可以查看WIKI或者看内核文档。

从这里可以看到它的调度策略

1cat /sys/block/vdb/queue/scheduler 2noop [deadline] cfq

修改

echo deadline > /sys/block/hda/queue/scheduler  

或者修改启动参数

1grub.conf 2elevator=deadline

从很多测试结果来看,数据库使用deadline调度,性能会更稳定一些。

其他

1. 关闭透明大页

2. 禁用NUMA

3. SSD的对齐

点赞
收藏

评论区

加载中...

相关推荐

Linux上TCP的几个内核参数调优

Linux作为一个强大的操作系统,提供了一系列内核参数供我们进行调优。光TCP的调优参数就有50多个。在和线上问题斗智斗勇的过程中,笔者积累了一些在内网环境应该进行调优的参数。在此分享出来,希望对大家有所帮助。调优清单好了,在这里先列出调优清单。请记住,这里只是笔者在内网进行TCP内核参数调优的经验,仅供参考。同时,笔者还会在余下的博客里面

Mysql数据库的优化

一:Mysql参数优化1.查看mysql参数最大连接!(https://oscimg.oschina.net/oscnet/0e4e34a1cc875042ecb1d51e365443b7a66.png)调大mysql参数连接(最大可设置12384)!(https://oscimg.oschina.net/oscnet/b213d5

Oracle的kernel.shmmax和kernel.shmall设置

Oracle的kernel.shmmax和kernel.shmall设置不会推车的娘们2019120412:02:301135收藏3分类专栏:linuxORACLE文章标签:sysctl系统内核oracle操作版权1. kernel.shmmax:是核心参数中最重要的参数之一,用于定义单个共享内存段的最

MySQL数据库优化技巧

MySQL优化三大方向①优化MySQL所在服务器内核(此优化一般由运维人员完成)。②对MySQL配置参数进行优化(my.cnf)此优化需要进行压力测试来进行参数调整。③对SQL语句以及表优化。MySQL参数优化1:MySQL默认的最大连接数为100,可以在mysql客户端使用以下命令查看mysql

SpringMVC 页面传递参数到controller的五种方式

一共是五种传参方式:一:直接将请求参数名作为Controller中方法的形参public Stringlogin(Stringusername,Stringpassword) :解释:括号中的参数必须与页面Form表单中的name名字相同二:使用@RequestParam绑定请求参数参数值举例:publicStri

mysql中时间比较的实现

MySql中时间比较的实现unix\_timestamp()unix\_timestamp函数可以接受一个参数,也可以不使用参数。它的返回值是一个无符号的整数。不使用参数,它返回自1970年1月1日0时0分0秒到现在所经过的秒数,如果使用参数,参数的类型为时间类型或者时间类型的字符串表示,则是从1970010100:00:0