Rocky Linux企业级服务器性能优化指南从系统配置到资源调优提升服务器运行效率与稳定性的实用技巧
Rocky Linux作为CentOS的替代品,已经成为企业级服务器操作系统的热门选择。它提供了稳定、安全且高性能的平台,适合各种企业应用场景。然而,默认安装的Rocky Linux系统往往没有针对特定工作负载进行优化,这就需要系统管理员进行一系列的性能调优,以充分发挥硬件潜力,提升服务器的运行效率和稳定性。
服务器性能优化是一个系统工程,涉及从硬件到软件的多个层面。本文将详细介绍Rocky Linux企业级服务器的性能优化方法,从系统基础配置到资源调优,提供一系列实用技巧,帮助管理员打造高效稳定的服务环境。
系统基础配置优化
内核参数调整
内核是Linux系统的核心,通过调整内核参数可以显著提升系统性能。Rocky Linux使用sysctl工具来管理内核参数,这些参数可以在/etc/sysctl.conf
文件或/etc/sysctl.d/
目录下的配置文件中设置。
以下是一些重要的内核参数优化:
# 编辑sysctl配置文件 vi /etc/sysctl.d/99-performance.conf # 添加以下内容 # 增加文件句柄限制 fs.file-max = 1000000 # 增加inode数量限制 fs.inotify.max_user_instances = 8192 # 优化网络栈 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 1200 net.ipv4.tcp_max_syn_backlog = 8192 net.ipv4.tcp_max_tw_buckets = 5000 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 0 net.ipv4.ip_local_port_range = 10000 65000 # 优化虚拟内存管理 vm.swappiness = 10 vm.dirty_ratio = 60 vm.dirty_background_ratio = 2 # 优化内核调度 kernel.sched_min_granularity_ns = 10000000 kernel.sched_wakeup_granularity_ns = 15000000
应用这些设置:
# 使配置立即生效 sysctl -p /etc/sysctl.d/99-performance.conf
文件系统优化
文件系统的选择和配置对服务器性能有重大影响。Rocky Linux支持多种文件系统,如XFS、ext4等,每种文件系统都有其特点和适用场景。
对于XFS文件系统(Rocky Linux的默认文件系统)的优化:
# 格式化XFS文件系统时指定合适的参数 mkfs.xfs -f -l size=128m -d agcount=16 /dev/sdb1 # 挂载选项优化 vi /etc/fstab # 添加以下挂载选项 /dev/sdb1 /data xfs defaults,noatime,nodiratime,largeio,inode64,swalloc,logbufs=8,logbsize=256k 0 0 # 重新挂载文件系统 mount -o remount /data
对于ext4文件系统的优化:
# 格式化ext4文件系统 mkfs.ext4 -F -m 1 -E stride=128,stripe-width=256 /dev/sdb1 # 挂载选项优化 vi /etc/fstab # 添加以下挂载选项 /dev/sdb1 /data ext4 defaults,noatime,nodiratime,data=writeback,barrier=0,nobh 0 0 # 重新挂载文件系统 mount -o remount /data
网络配置优化
网络性能对于服务器至关重要,特别是对于Web服务器、数据库服务器等网络密集型应用。
# 编辑网络配置文件 vi /etc/sysconfig/network-scripts/ifcfg-eth0 # 添加或修改以下参数 DEVICE=eth0 BOOTPROTO=static ONBOOT=yes IPADDR=192.168.1.100 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 DNS1=8.8.8.8 DNS2=8.8.4.4 # 启用网络多队列 ethtool -L eth0 combined 8 # 关闭IPv6(如果不需要) echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.d/99-performance.conf echo "net.ipv6.conf.default.disable_ipv6 = 1" >> /etc/sysctl.d/99-performance.conf # 优化网络中断亲和性 # 安装irqbalance dnf install -y irqbalance systemctl enable --now irqbalance # 配置网络中断亲和性 #!/bin/bash # 将以下脚本保存为set_irq_affinity.sh并执行 NIC="eth0" CPU_COUNT=$(lscpu | grep "^CPU(s):" | awk '{print $2}') IRQS=$(cat /proc/interrupts | grep $NIC | awk '{print $1}' | sed 's/://') for IRQ in $IRQS; do CPU=$((IRQ % CPU_COUNT)) echo $CPU > /proc/irq/$IRQ/smp_affinity_list done
硬件资源监控与管理
CPU资源管理
CPU是服务器最关键的资源之一,合理管理和优化CPU资源可以显著提升服务器性能。
首先,安装必要的监控工具:
dnf install -y epel-release dnf install -y htop sysstat perf
使用top
或htop
命令监控CPU使用情况:
htop
使用mpstat
命令监控每个CPU核心的使用情况:
mpstat -P ALL 1
使用perf
工具进行更深入的性能分析:
# 记录CPU性能数据 perf record -a # 分析性能数据 perf report
CPU亲和性设置可以提高缓存命中率,减少进程迁移开销:
# 安装taskset工具 dnf install -y util-linux # 将进程绑定到特定CPU核心 taskset -cp 0,1,2,3 <PID> # 启动时绑定进程到CPU核心 taskset -c 0,1,2,3 <command>
CPU频率调整可以根据负载情况动态调整CPU频率,平衡性能和能耗:
# 安装cpufreq工具 dnf install -y cpupowerutils # 查看当前CPU频率策略 cpupower frequency-info # 设置CPU频率调节器为performance cpupower frequency-set -g performance # 或者设置为ondemand,根据负载动态调整 cpupower frequency-set -g ondemand
内存管理
内存管理是服务器性能优化的关键环节,合理的内存配置可以减少swap使用,提高系统响应速度。
使用free
命令查看内存使用情况:
free -h
使用vmstat
命令监控内存使用情况:
vmstat 1
优化内存使用,减少swap:
# 调整swappiness参数,减少swap使用 echo "vm.swappiness = 10" >> /etc/sysctl.d/99-performance.conf sysctl -p /etc/sysctl.d/99-performance.conf # 清理页面缓存 echo 1 > /proc/sys/vm/drop_caches # 清理目录项和inode echo 2 > /proc/sys/vm/drop_caches # 清理页面缓存、目录项和inode echo 3 > /proc/sys/vm/drop_caches
使用透明大页(Transparent Huge Pages)可以提高内存访问效率:
# 查看当前透明大页状态 cat /sys/kernel/mm/transparent_hugepage/enabled cat /sys/kernel/mm/transparent_hugepage/defrag # 临时禁用透明大页 echo never > /sys/kernel/mm/transparent_hugepage/enabled echo never > /sys/kernel/mm/transparent_hugepage/defrag # 永久禁用透明大页(在GRUB配置中添加) vi /etc/default/grub # 在GRUB_CMDLINE_LINUX行中添加 transparent_hugepage=never GRUB_CMDLINE_LINUX="... transparent_hugepage=never" # 更新GRUB配置 grub2-mkconfig -o /boot/grub2/grub.cfg # 如果是UEFI系统,使用以下命令 grub2-mkconfig -o /boot/efi/EFI/rocky/grub.cfg # 重启系统 reboot
存储I/O优化
存储I/O性能往往是服务器性能的瓶颈,优化存储I/O可以显著提升服务器整体性能。
使用iostat
命令监控磁盘I/O:
iostat -xz 1
使用iotop
命令监控进程I/O使用情况:
dnf install -y iotop iotop -o
I/O调度器选择对磁盘性能有重要影响:
# 查看当前I/O调度器 cat /sys/block/sda/queue/scheduler # 临时设置I/O调度器为deadline echo deadline > /sys/block/sda/queue/scheduler # 永久设置I/O调度器(在GRUB配置中添加) vi /etc/default/grub # 在GRUB_CMDLINE_LINUX行中添加 elevator=deadline GRUB_CMDLINE_LINUX="... elevator=deadline" # 更新GRUB配置 grub2-mkconfig -o /boot/grub2/grub.cfg # 如果是UEFI系统,使用以下命令 grub2-mkconfig -o /boot/efi/EFI/rocky/grub.cfg # 重启系统 reboot
对于SSD存储,可以启用TRIM支持以维持长期性能:
# 检查SSD是否支持TRIM hdparm -I /dev/sda | grep "TRIM supported" # 启用TRIM fstrim -av # 设置定时TRIM任务 systemctl enable fstrim.timer systemctl start fstrim.timer
使用RAID配置可以提高存储性能和可靠性:
# 安装mdadm工具 dnf install -y mdadm # 创建RAID 0阵列(提高性能) mdadm --create /dev/md0 --level=0 --raid-devices=2 /dev/sdb /dev/sdc # 创建RAID 1阵列(提高可靠性) mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc # 创建RAID 10阵列(平衡性能和可靠性) mdadm --create /dev/md0 --level=10 --raid-devices=4 /dev/sdb /dev/sdc /dev/sdd /dev/sde # 格式化RAID设备 mkfs.xfs -f /dev/md0 # 挂载RAID设备 mkdir /data mount /dev/md0 /data # 更新/etc/fstab echo "/dev/md0 /data xfs defaults 0 0" >> /etc/fstab
服务与应用优化
Web服务器优化
Web服务器是企业中最常见的服务之一,优化Web服务器可以显著提高网站响应速度和并发处理能力。
Nginx优化
# 安装Nginx dnf install -y nginx # 编辑Nginx主配置文件 vi /etc/nginx/nginx.conf # 优化Nginx配置 user nginx; worker_processes auto; worker_cpu_affinity auto; worker_rlimit_nofile 100000; error_log /var/log/nginx/error.log crit; pid /run/nginx.pid; events { worker_connections 4096; multi_accept on; use epoll; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log off; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 30; keepalive_requests 100000; reset_timedout_connection on; client_body_timeout 10; send_timeout 2; # Gzip压缩 gzip on; gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_min_length 1000; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript; # 缓存设置 open_file_cache max=200000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; include /etc/nginx/conf.d/*.conf; } # 启动Nginx并设置开机自启 systemctl enable --now nginx
Apache优化
# 安装Apache dnf install -y httpd # 编辑Apache主配置文件 vi /etc/httpd/conf/httpd.conf # 优化Apache配置 KeepAlive On KeepAliveTimeout 5 MaxKeepAliveRequests 100 <IfModule prefork.c> StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxClients 150 MaxRequestsPerChild 0 </IfModule> <IfModule worker.c> StartServers 2 MaxClients 150 MinSpareThreads 25 MaxSpareThreads 75 ThreadsPerChild 25 MaxRequestsPerChild 0 </IfModule> # 启用HTTP/2支持 vi /etc/httpd/conf.d/ssl.conf # 添加以下行 Protocols h2 http/1.1 # 启动Apache并设置开机自启 systemctl enable --now httpd
数据库优化
数据库是企业应用的核心组件,优化数据库性能对整个系统的响应速度至关重要。
MySQL/MariaDB优化
# 安装MariaDB dnf install -y mariadb-server # 编辑MariaDB配置文件 vi /etc/my.cnf.d/mariadb-server.cnf # 添加以下配置 [mysqld] # 基本设置 character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci default-storage-engine = InnoDB # 内存设置 innodb_buffer_pool_size = 4G # 设置为系统内存的50-70% innodb_buffer_pool_instances = 4 innodb_log_file_size = 512M innodb_log_buffer_size = 64M innodb_flush_log_at_trx_commit = 2 innodb_flush_method = O_DIRECT innodb_file_per_table = 1 innodb_read_io_threads = 8 innodb_write_io_threads = 8 innodb_thread_concurrency = 0 # 连接设置 max_connections = 500 max_connect_errors = 100000 back_log = 512 max_allowed_packet = 64M interactive_timeout = 28800 wait_timeout = 28800 # 查询缓存 query_cache_type = 1 query_cache_size = 256M query_cache_limit = 4M # 其他设置 tmp_table_size = 256M max_heap_table_size = 256M sort_buffer_size = 4M read_buffer_size = 3M read_rnd_buffer_size = 4M join_buffer_size = 4M thread_cache_size = 16 table_open_cache = 2048 table_definition_cache = 2048 # 日志设置 slow_query_log = 1 slow_query_log_file = /var/log/mariadb/slow.log long_query_time = 2 log_queries_not_using_indexes = 1 # 启动MariaDB并设置开机自启 systemctl enable --now mariadb # 安全初始化 mysql_secure_installation
PostgreSQL优化
# 安装PostgreSQL dnf install -y postgresql-server postgresql-contrib # 初始化数据库 postgresql-setup --initdb # 编辑PostgreSQL配置文件 vi /var/lib/pgsql/data/postgresql.conf # 优化PostgreSQL配置 # 连接设置 max_connections = 200 superuser_reserved_connections = 10 # 内存设置 shared_buffers = 1GB # 设置为系统内存的25% effective_cache_size = 3GB # 设置为系统内存的50-75% work_mem = 16MB maintenance_work_mem = 256MB wal_buffers = 16MB checkpoint_segments = 32 checkpoint_completion_target = 0.9 checkpoint_timeout = 15min # 查询优化 random_page_cost = 2.0 effective_io_concurrency = 200 default_statistics_target = 100 # 日志设置 log_destination = 'stderr' logging_collector = on log_directory = 'pg_log' log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' log_statement = 'none' log_min_duration_statement = 1000 log_checkpoints = on log_connections = on log_disconnections = on log_lock_waits = on # 启动PostgreSQL并设置开机自启 systemctl enable --now postgresql
其他常见服务优化
Redis优化
# 安装Redis dnf install -y redis # 编辑Redis配置文件 vi /etc/redis.conf # 优化Redis配置 # 内存设置 maxmemory 1gb maxmemory-policy allkeys-lru # 持久化设置 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes # 日志设置 loglevel notice logfile /var/log/redis/redis.log # 网络设置 tcp-backlog 511 timeout 0 tcp-keepalive 300 # 启动Redis并设置开机自启 systemctl enable --now redis
Memcached优化
# 安装Memcached dnf install -y memcached # 编辑Memcached配置文件 vi /etc/sysconfig/memcached # 优化Memcached配置 PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="1024" OPTIONS="-l 127.0.0.1,::1 -t 8 -c 2048 -m 2048" # 启动Memcached并设置开机自启 systemctl enable --now memcached
安全性与性能平衡
在优化服务器性能的同时,不能忽视安全性。安全配置往往会对性能产生一定影响,需要在安全性和性能之间找到平衡点。
防火墙优化
# 安装Firewalld dnf install -y firewalld # 启动Firewalld并设置开机自启 systemctl enable --now firewalld # 配置防火墙规则 firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-service=https firewall-cmd --permanent --add-service=ssh firewall-cmd --permanent --add-port=3306/tcp # MySQL端口 firewall-cmd --permanent --add-port=6379/tcp # Redis端口 # 重新加载防火墙规则 firewall-cmd --reload # 优化防火墙性能 # 增加连接跟踪表大小 echo "net.netfilter.nf_conntrack_max = 1000000" >> /etc/sysctl.d/99-performance.conf echo "net.netfilter.nf_conntrack_tcp_timeout_established = 3600" >> /etc/sysctl.d/99-performance.conf sysctl -p /etc/sysctl.d/99-performance.conf
SELinux优化
SELinux提供了强大的安全保护,但可能对性能产生一定影响。可以根据实际需求调整SELinux策略。
# 检查SELinux状态 sestatus # 临时设置为宽松模式 setenforce 0 # 永久设置为宽松模式 vi /etc/selinux/config # 修改SELINUX=permissive # 优化SELinux性能 # 安装setroubleshoot工具 dnf install -y setroubleshoot-server # 查看SELinux拒绝日志 sealert -a /var/log/audit/audit.log # 为特定服务创建SELinux策略模块 # 例如,为Nginx创建策略 ausearch -c 'nginx' --raw | audit2allow -M my-nginx semodule -i my-nginx.pp
SSH安全与性能优化
# 编辑SSH配置文件 vi /etc/ssh/sshd_config # 优化SSH配置 # 禁用DNS反向解析 UseDNS no # 启用压缩 Compression yes # 使用更高性能的加密算法 Ciphers chacha20-poly1305@openssl.com,aes256-gcm@openssl.com,aes128-gcm@openssl.com,aes256-ctr,aes192-ctr,aes128-ctr MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256 # 禁用不安全的认证方式 PasswordAuthentication no ChallengeResponseAuthentication no UsePAM no # 重启SSH服务 systemctl restart sshd
性能监控与故障排除
持续的性能监控和及时的故障排除是保证服务器稳定运行的关键。
系统监控工具
使用Prometheus和Grafana监控系统
# 安装Prometheus dnf install -y prometheus node_exporter # 配置Prometheus vi /etc/prometheus/prometheus.yml # 添加以下配置 global: scrape_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'node' static_configs: - targets: ['localhost:9100'] # 启动Prometheus和Node Exporter systemctl enable --now prometheus systemctl enable --now node_exporter # 安装Grafana dnf install -y grafana # 启动Grafana systemctl enable --now grafana # 访问Grafana Web界面(http://your-server-ip:3000)并配置数据源
使用Zabbix监控系统
# 安装Zabbix仓库 rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/8/x86_64/zabbix-release-5.0-1.el8.noarch.rpm dnf clean all # 安装Zabbix Server dnf install -y zabbix-server-mysql zabbix-web-mysql zabbix-apache-conf zabbix-agent # 安装MariaDB并创建数据库 dnf install -y mariadb-server systemctl enable --now mariadb mysql -u root -p CREATE DATABASE zabbix CHARACTER SET utf8mb4 COLLATE utf8mb4_bin; CREATE USER 'zabbix'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix'@'localhost'; FLUSH PRIVILEGES; EXIT; # 导入Zabbix数据库架构 zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix # 配置Zabbix Server vi /etc/zabbix/zabbix_server.conf # 修改以下参数 DBPassword=password # 配置PHP时区 vi /etc/php-fpm.d/zabbix.conf # 修改以下参数 php_value[date.timezone] = Asia/Shanghai # 启动Zabbix Server和Agent systemctl enable --now zabbix-server zabbix-agent httpd php-fpm # 访问Zabbix Web界面(http://your-server-ip/zabbix)并完成安装
性能分析工具
使用perf进行性能分析
# 安装perf dnf install -y perf # 记录CPU性能数据 perf record -a -g # 分析性能数据 perf report # 生成火焰图 # 安装必要的工具 dnf install -y git perl cpan cpan App::cpanminus cpanm JSON::MaybeXS # 克隆FlameGraph仓库 git clone https://github.com/brendangregg/FlameGraph.git # 生成火焰图 perf script | ./FlameGraph/stackcollapse-perf.pl | ./FlameGraph/flamegraph.pl > flamegraph.svg
使用sysstat进行系统性能分析
# 安装sysstat dnf install -y sysstat # 启用sysstat数据收集 vi /etc/default/sysstat # 修改ENABLED="true" # 启动sysstat服务 systemctl enable --now sysstat # 使用sar命令查看CPU使用情况 sar -u 1 5 # 使用sar命令查看内存使用情况 sar -r 1 5 # 使用sar命令查看磁盘I/O情况 sar -b 1 5 # 使用sar命令查看网络使用情况 sar -n DEV 1 5
常见性能问题及解决方案
CPU使用率过高
# 查找CPU使用率高的进程 top -o %CPU # 查找CPU使用率高的线程 top -H -p <PID> # 使用perf分析CPU使用情况 perf top -p <PID> # 解决方案: # 1. 优化应用程序代码 # 2. 增加CPU资源 # 3. 调整进程优先级 renice -n 10 -p <PID> # 4. 限制进程CPU使用率 dnf install -y cpulimit cpulimit -p <PID> -l 50
内存使用率过高
# 查找内存使用率高的进程 top -o %MEM # 查看进程内存映射 cat /proc/<PID>/maps # 使用smem分析内存使用情况 dnf install -y smem smem -t -k -p # 解决方案: # 1. 优化应用程序内存使用 # 2. 增加物理内存 # 3. 调整内核参数减少swap使用 echo "vm.swappiness = 10" >> /etc/sysctl.d/99-performance.conf sysctl -p /etc/sysctl.d/99-performance.conf # 4. 清理页面缓存 echo 1 > /proc/sys/vm/drop_caches
磁盘I/O性能问题
# 监控磁盘I/O iostat -xz 1 # 查找I/O使用率高的进程 iotop -o # 解决方案: # 1. 优化应用程序I/O模式 # 2. 使用更快的存储设备 # 3. 调整I/O调度器 echo deadline > /sys/block/sda/queue/scheduler # 4. 增加文件系统缓存 echo "vm.dirty_ratio = 60" >> /etc/sysctl.d/99-performance.conf echo "vm.dirty_background_ratio = 2" >> /etc/sysctl.d/99-performance.conf sysctl -p /etc/sysctl.d/99-performance.conf
网络性能问题
# 监控网络使用情况 sar -n DEV 1 5 # 查看网络连接状态 ss -tuln # 查看网络连接数 ss -s # 解决方案: # 1. 优化网络配置 echo "net.core.rmem_max = 16777216" >> /etc/sysctl.d/99-performance.conf echo "net.core.wmem_max = 16777216" >> /etc/sysctl.d/99-performance.conf echo "net.ipv4.tcp_rmem = 4096 87380 16777216" >> /etc/sysctl.d/99-performance.conf echo "net.ipv4.tcp_wmem = 4096 65536 16777216" >> /etc/sysctl.d/99-performance.conf sysctl -p /etc/sysctl.d/99-performance.conf # 2. 使用负载均衡 # 3. 优化应用程序网络使用 # 4. 增加网络带宽
最佳实践与案例分析
Web服务器性能优化案例
某电子商务网站在促销活动期间面临高并发访问,导致网站响应缓慢,甚至出现服务不可用的情况。通过以下优化措施,成功提升了网站性能:
硬件升级:
- 将服务器内存从16GB增加到64GB
- 使用SSD替换传统HDD
- 增加CPU核心数
Nginx优化: “`nginx worker_processes auto; worker_cpu_affinity auto; worker_rlimit_nofile 100000;
events {
worker_connections 4096; multi_accept on; use epoll;
}
http {
sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 30; keepalive_requests 100000; reset_timedout_connection on; gzip on; gzip_comp_level 6; gzip_min_length 1000; open_file_cache max=200000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; # 启用缓存 proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m; server { listen 80; server_name example.com; location / { proxy_pass http://backend; proxy_cache my_cache; proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m; } }
}
3. **PHP-FPM优化**: ```ini [www] pm = dynamic pm.max_children = 100 pm.start_servers = 20 pm.min_spare_servers = 10 pm.max_spare_servers = 30 pm.max_requests = 1000
数据库优化:
[mysqld] innodb_buffer_pool_size = 32G innodb_buffer_pool_instances = 8 innodb_log_file_size = 2G innodb_flush_log_at_trx_commit = 2 innodb_flush_method = O_DIRECT max_connections = 500 query_cache_size = 512M
Redis缓存优化:
maxmemory 16gb maxmemory-policy allkeys-lru save 900 1 save 300 10 save 60 10000
通过以上优化措施,网站在促销活动期间能够稳定运行,页面加载时间从平均3秒减少到800毫秒,并发处理能力从每秒500请求提升到每秒5000请求。
数据库服务器性能优化案例
某大型企业的数据库服务器面临查询性能问题,特别是在高峰期,响应时间明显增加。通过以下优化措施,成功提升了数据库性能:
硬件升级:
- 将服务器内存从32GB增加到128GB
- 使用高性能SSD阵列
- 增加CPU核心数
MySQL/MariaDB优化:
[mysqld] innodb_buffer_pool_size = 96G innodb_buffer_pool_instances = 16 innodb_log_file_size = 4G innodb_log_buffer_size = 256M innodb_flush_log_at_trx_commit = 2 innodb_flush_method = O_DIRECT innodb_file_per_table = 1 innodb_read_io_threads = 16 innodb_write_io_threads = 16 innodb_thread_concurrency = 0 max_connections = 1000 max_connect_errors = 100000 back_log = 1024 max_allowed_packet = 128M query_cache_type = 1 query_cache_size = 1G query_cache_limit = 8M tmp_table_size = 512M max_heap_table_size = 512M sort_buffer_size = 8M read_buffer_size = 4M read_rnd_buffer_size = 8M join_buffer_size = 8M thread_cache_size = 32 table_open_cache = 4096 table_definition_cache = 4096
查询优化:
- 使用EXPLAIN分析慢查询
- 为常用查询字段添加索引
- 优化复杂查询,分解为多个简单查询
- 使用存储过程减少网络往返
主从复制优化: “`ini
主服务器配置
[mysqld] server-id = 1 log-bin = mysql-bin binlog_format = ROW binlog_row_image = MINIMAL sync_binlog = 0 innodb_flush_log_at_trx_commit = 2
# 从服务器配置 [mysqld] server-id = 2 relay-log = relay-bin read_only = 1 slave_parallel_workers = 8 slave_pending_jobs_size_max = 128M “`
- 分库分表策略:
- 按业务模块分库
- 按时间范围分表
- 使用中间件实现分库分表路由
通过以上优化措施,数据库查询响应时间从平均2秒减少到200毫秒,并发处理能力从每秒1000查询提升到每秒10000查询,系统稳定性显著提高。
总结
Rocky Linux企业级服务器性能优化是一个系统工程,涉及从硬件到软件的多个层面。本文详细介绍了系统基础配置优化、硬件资源监控与管理、服务与应用优化、安全性与性能平衡、性能监控与故障排除等方面的内容,并提供了实际案例分析。
在实际优化过程中,需要根据服务器的具体用途和工作负载特点,有针对性地进行优化。同时,性能优化是一个持续的过程,需要不断监控、分析和调整。通过合理的性能优化,可以充分发挥硬件潜力,提升服务器的运行效率和稳定性,为企业提供更优质的服务。
希望本文提供的Rocky Linux企业级服务器性能优化指南能够帮助系统管理员更好地管理和优化服务器,为企业创造更大的价值。