
Linux 内核优化
Lnux服务器内核调整参数如下:
-
增加Linux对socket链接数的限制
使用root用户登录系统,修改/etc/sysctl.conf文件,增加如下信息:
# 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭。 net.ipv4.tcp_tw_reuse = 1 # 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。 net.ipv4.tcp_tw_recycle = 1 # 这个参数决定了保持在TIME-WAIT状态的sockets进程个数。 net.ipv4.tcp_max_tw_buckets = 20480 net.ipv4.tcp_max_syn_backlog = 20480 net.core.netdev_max_backlog = 262144
💡 注:修改完成后使用命令sysctl -p使其生效。
-
修改 Linux 对每个进程可以打开的文件数目
使用root用户登录系统,修改/etc/security/limits.conf文件里增加如下信息:
* soft nofile 102400 * hard nofile 102400
修改完成后需重启机器使之生效。
-
修改Linux允许创建的最大进程数量
使用 root 用户登录系统,ulimit -u 125333 修改完成后需重启机器使之生效。
修改 Linux 内核配置
-
执行以下命令,打开
/etc/sysctl.conf
配置文件:vi /etc/sysctl.conf
-
在
/etc/sysctl.conf
配置文件中添加以下内容:# 修改内核异步 I/O 限制 fs.aio-max-nr=1048576 # 网络优化 net.core.somaxconn = 2048 net.core.netdev_max_backlog = 10000 net.core.rmem_default = 16777216 net.core.wmem_default = 16777216 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.ipv4.ip_local_port_range = 3500 65535 net.ipv4.ip_forward = 0 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.default.accept_source_route = 0 net.ipv4.tcp_syncookies = 0 net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216 net.ipv4.tcp_max_syn_backlog = 16384 net.ipv4.tcp_fin_timeout = 15 net.ipv4.tcp_max_syn_backlog = 16384 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_slow_start_after_idle=0 vm.swappiness = 0 vm.min_free_kbytes = 2097152 fs.file-max = 6573688 # 修改进程可以拥有的虚拟内存区域数量 # vm.max_map_count = 655360 # 此处为 OceanBase 数据库的 data 目录 kernel.core_pattern = /data/core-%e-%p-%t
⚠
max_map_count
配置不合理的情况下,可能会导致严重的内存泄露。 -
更改配置完成后,执行以下命令,加载配置,使配置生效。
sysctl -p
修改进程数量限制
-
执行以下命令,打开
/etc/security/limits.conf
配置文件:vi /etc/security/limits.conf
-
在
/etc/security/limits.conf
配置文件中添加以下内容:* soft nofile 655360 * hard nofile 655360 * soft nproc 655360 * hard nproc 655360 * hard core unlimited * soft core unlimited * hard stack 10240 * soft stack 10240 * hard cpu unlimited * soft cpu unlimited root soft nofile 655350 root hard nofile 655350
-
查看
/etc/security/limits.d/20-nproc.conf
文件中是否存在 nproc 的配置若存在需同步修改该文件中 nproc 的值。
cat /etc/security/limits.d/20-nproc.conf
-
退出当前会话,重新登录。执行以下命令,查看配置是否生效。
ulimit -a
输出如下:
core file size (blocks, -c) unlimited data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 252876 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open files (-n) 655350 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 20480 cpu time (seconds, -t) unlimited max user processes (-u) 655360 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
从结果中可以看出:
- core file size 表示核心文件的最大阈值(以块为单位),对应
limits.conf
配置文件中的 core 参数,需查看值是否为 unlimited。 - open files 表示最大打开文件描述符数,对应
limits.conf
配置文件中的 nofile 参数,需查看值是否为 655350。 - stack size 表示堆栈大小(以千字节为单位),对应
limits.conf
配置文件中的 stack 参数,需查看值是否为 20480。 - max user processes 表示最大用户进程数,对应
limits.conf
配置文件中的 nproc 参数,需查看值是否为 655360
- core file size 表示核心文件的最大阈值(以块为单位),对应
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 Mosy