1. 性能优化概述
IT系统性能优化是提高系统效率、减少响应时间、提高资源利用率的过程。有效的性能优化可以提升用户体验,降低运营成本,确保系统稳定运行。更多学习教程www.fgedu.net.cn
# uptime
10:00:00 up 1 day, 2:00, 1 user, load average: 0.50, 0.60, 0.70
# 检查CPU使用率
# top -b -n 1 | grep “Cpu(s)”
%Cpu(s): 5.0 us, 2.0 sy, 0.0 ni, 90.0 id, 0.0 wa, 0.0 hi, 3.0 si, 0.0 st
# 检查内存使用情况
# free -h
total used free shared buff/cache available
Mem: 62G 10G 45G 8.5M 7G 50G
Swap: 32G 0B 32G
2. 系统监控
系统监控是性能优化的基础,通过监控可以了解系统的运行状态,发现性能瓶颈。学习交流加群风哥微信: itpux-com
# yum install -y htop sysstat iotop
# 使用htop监控系统资源
# htop
# 使用iostat监控磁盘I/O
# iostat -x 1
Linux 3.10.0-1160.el7.x86_64 (server) 03/30/2026 _x86_64_ (32 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
5.00 0.00 2.00 0.00 0.00 93.00
device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
vda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
vdb 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
# 使用vmstat监控虚拟内存
# vmstat 1
procs ———–memory———- —swap– —–io—- -system– ——cpu—– r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 47184000 786080 7341200 0 0 0 0 65 98 5 2 93 0 0
3. CPU优化
CPU优化包括调整CPU调度策略、减少CPU使用率、优化进程优先级等。
# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 32
On-line CPU(s) list: 0-31
Thread(s) per core: 2
Core(s) per socket: 8
Socket(s): 2
NUMA node(s): 2
Vendor ID: GenuineIntel
CPU family: 6
Model: 85
Model name: Intel(R) Xeon(R) Gold 6248 CPU @ 2.50GHz
# 调整进程优先级
# nice -n 10 ./myprocess
# 实时调整进程优先级
# renice -n 5 1234
1234 (process ID) old priority 0, new priority 5
# 查看进程优先级
# ps -eo pid,ni,cmd | grep myprocess
1234 10 ./myprocess
4. 内存优化
内存优化包括增加内存容量、调整内存分配、优化内存使用等。
# free -h
total used free shared buff/cache available
Mem: 62G 10G 45G 8.5M 7G 50G
Swap: 32G 0B 32G
# 调整内核参数
# vi /etc/sysctl.conf
# 内存管理参数
vm.swappiness = 10
vm.dirty_background_ratio = 5
vm.dirty_ratio = 10
# 使内核参数生效
# sysctl -p
# 检查内存使用详情
# smem -t
PID User Command Swap USS PSS RSS
1234 root /usr/bin/python3 /usr/sbin/firewalld 0 10M 12M 20M
5678 root /usr/sbin/sshd 0 5M 8M 15M
…
——————————————————————————-
123 processes: 0B 1.2G 1.5G 2.5G
5. 磁盘优化
磁盘优化包括使用SSD、调整文件系统、优化I/O调度等。
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 50G 0 disk /
vdb 252:16 0 500G 0 disk /data
# 检查文件系统
# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 32G 0 32G 0% /dev
tmpfs 32G 0 32G 0% /dev/shm
tmpfs 32G 8.5M 32G 1% /run
tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/vda1 50G 15G 36G 30% /
/dev/vdb1 500G 50G 450G 10% /data
# 调整I/O调度策略
# echo deadline > /sys/block/vda/queue/scheduler
# 查看当前I/O调度策略
# cat /sys/block/vda/queue/scheduler
[deadline] cfq noop
# 优化文件系统
# tune2fs -o journal_data_writeback /dev/vda1
# tune2fs -m 1 /dev/vda1
6. 网络优化
网络优化包括调整网络参数、优化网络协议、使用负载均衡等。
# ifconfig
eth0: flags=4163
inet 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::100 prefixlen 64 scopeid 0x20 ether 00:11:22:33:44:55 txqueuelen 1000 (Ethernet)
RX packets 100000 bytes 100000000 (95.3 MiB)
TX packets 50000 bytes 50000000 (47.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# 调整网络参数
# vi /etc/sysctl.conf
# 网络参数
net.core.netdev_max_backlog = 10000
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
# 使网络参数生效
# sysctl -p
# 查看网络连接状态
# netstat -an | grep ESTABLISHED | wc -l
100
7. 应用优化
应用优化包括代码优化、配置优化、缓存优化等。
# vi /etc/nginx/nginx.conf
# 工作进程数
worker_processes auto;
# 连接数限制
worker_connections 65535;
# 启用epoll
use epoll;
# 启用gzip压缩
gzip on;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# 重启Nginx
# systemctl restart nginx
# 查看Nginx状态
# systemctl status nginx
● nginx.service – nginx – high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2026-03-30 10:00:00 CST; 1h ago
Docs: man:nginx(8)
Process: 1234 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 1235 (nginx)
CGroup: /system.slice/nginx.service
├─1235 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
├─1236 nginx: worker process
└─1237 nginx: worker process
8. 数据库优化
数据库优化包括SQL优化、索引优化、配置优化等。
# vi /etc/my.cnf
[mysqld]
# 缓冲区大小
innodb_buffer_pool_size = 4G
key_buffer_size = 256M
# 查询缓存
query_cache_size = 128M
query_cache_type = 1
# 连接数
max_connections = 1000
# 重启MySQL
# systemctl restart mysqld
# 查看MySQL状态
# mysqladmin status
Uptime: 3600 Threads: 10 Questions: 1000 Slow queries: 0 Opens: 100 Flush tables: 1 Open tables: 50 Queries per second avg: 0.277
# 优化SQL查询
# EXPLAIN SELECT * FROM users WHERE id = 1;
+—-+————-+——-+————+——-+—————+———+———+——-+——+———-+——-+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+—-+————-+——-+————+——-+—————+———+———+——-+——+———-+——-+
| 1 | SIMPLE | users | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
+—-+————-+——-+————+——-+—————+———+———+——-+——+———-+——-+
9. 内核优化
内核优化包括调整内核参数、优化系统调用、调整文件描述符限制等。
# uname -r
3.10.0-1160.el7.x86_64
# 调整文件描述符限制
# vi /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
# 调整系统调用限制
# vi /etc/sysctl.conf
# 文件描述符
fs.file-max = 65536
# 系统调用
kernel.sysrq = 1
kernel.core_uses_pid = 1
# 使内核参数生效
# sysctl -p
# 查看文件描述符限制
# ulimit -n
65536
10. 性能优化最佳实践
遵循性能优化最佳实践可以提高系统性能,确保系统稳定运行。
# yum install -y yum-utils
# package-cleanup –oldkernels –count=1
# 启用透明大页
# echo always > /sys/kernel/mm/transparent_hugepage/enabled
# 检查透明大页状态
# cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
# 优化系统启动项
# systemctl disable firewalld
# systemctl disable NetworkManager
# 查看系统启动项
# systemctl list-unit-files | grep enabled
crond.service enabled
sshd.service enabled
rsyslog.service enabled
tuned.service enabled
# 启用tuned性能调优
# systemctl start tuned
# tuned-adm profile throughput-performance
# 查看当前tuned配置
# tuned-adm active
Current active profile: throughput-performance
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
