CyberPanel에서 MySQL의 쓰기 성능을 개선하고 MySQL 설치 후 성능을 최적화하는 방법

在CyberPanel 中 MySQL 写入性能优化指南

在CyberPanel 的使用中,会遇到MySQL写入缓慢的情况,正常时候数据库写入是没有异常的,但是当网站流量增大之后,数据库就会变得十分缓慢。

这时你会发现,CPU和内存还空闲很多,还没有占用超过30%,带宽也有剩余,为什么数据库写入会变得缓慢呢?

默认安装的数据库没有经过优化配置,下面看一下如何优化MySQL数据库。

MySQL 的写入性能优化涉及硬件、配置、查询设计和监控等多层面调整。根据实际负载(如高并发写入),优化可将性能提升 2-10 倍。

CyberPanel에서 MySQL의 쓰기 성능을 개선하고 MySQL 설치 후 성능을 최적화하는 방법

下面是在CyberPanel中如何优化MySQL

首先监控工具安装:sudo apt install mysql-tuner-perl,运行 perl mysql-tuner.pl 获取初步建议,例如增加 innodb_buffer_pool_size。

打开数据库配置文件

vi /etc/mysql/mariadb.conf.d/50-server.cnf

[mysqld]
bind-address = 127.0.0.1  # 绑定本地,避免远程滥用
default_storage_engine = InnoDB  # 默认 InnoDB(支持事务)
character-set-server = utf8mb4  # 支持 emoji
collation-server = utf8mb4_unicode_ci

配置文件中如果没有的话,添加在[mysqld]中。

InnoDB 缓冲池优化,写入瓶颈常在 InnoDB 日志、缓冲区和锁上。逐步调整,监控变化。

设置 innodb_buffer_pool_size = 70% 系统 RAM

innodb_buffer_pool_size = 44G
innodb_buffer_pool_instances = 8  # 多实例减少锁争用

在高性能 Web 应用中,MariaDB(MySQL 兼容)作为数据库引擎,其默认配置往往无法充分发挥服务器潜力。以 64GB 内存为例,默认 InnoDB 缓冲池仅为 128MB,远低于推荐的 70% RAM(约 45GB),这会导致频繁磁盘 I/O、查询延迟和写入瓶颈。根据 2025 年 MariaDB 官方基准测试和 Percona 报告,未优化配置下 TPS(每秒事务数)仅为峰值的 30%,而优化后可提升 4-8 倍,尤其在电商或 CMS 高并发场景。

在 CyberPanel 中优化 MySQL 不是一蹴而就,而是迭代过程。从基础配置入手,结合面板工具和监控,您能显著提升性能,避免常见瓶颈。记住,优化后测试负载(如用 JMeter 模拟),并定期运行 mysql-tuner。2025 年,随着 AI 驱动的数据库工具兴起,CyberPanel 的集成将更智能,但手动调优仍是基础。优化好后,您的网站将更高效,助力业务腾飞。

MySQL 临时表磁盘创建过多优化:从 46% 降至 25% 阈值的实用指南

在 MySQL 性能调优中,临时表(temporary tables)的创建位置直接影响查询效率和资源消耗。您的现状显示 Created_tmp_disk_tables / (Created_tmp_tables + Created_tmp_disk_tables) * 100 = 46%,远高于推荐的 25% 阈值,这意味着近一半临时表回落到磁盘而非内存,导致 I/O 开销激增、查询延迟翻倍,甚至在高并发下引发瓶颈。根据公式检验(value > 25),这触发了 mysql-tuner 等工具的警告。临时表常用于 GROUP BY、ORDER BY 或 DISTINCT 操作,当内存不足时,MySQL 会自动切换到磁盘(MyISAM 引擎),这在 64GB RAM 服务器上本不应发生,却暴露了配置不优或查询设计问题。

首先同步调整参数——设 max_heap_table_size = tmp_table_size = 1024M(占 RAM 1.6%),在 my.cnf [mysqld] 下添加,重启 MariaDB 测试。监控 SHOW GLOBAL STATUS LIKE 'Created_tmp%';,目标降至 <25%。若无效,重写查询:用 EXPLAIN 分析慢 SQL,拆分 BLOB 字段为外部存储,或启用查询缓存。结合 Percona Toolkit 的 pt-query-digest 分析日志,迭代优化。最终,不仅临时表内存命中率达 80%+,整体性能还将提升 2-4 倍,让您的数据库从“勉强支撑”转为“高效响应”。

tmp_table_size          = 1024M
max_heap_table_size     = 1024M

MySQL long_query_time 设置优化:从 10 秒调至 1-5 秒的指南

在 MySQL 性能监控中,long_query_time 参数定义了慢查询日志的阈值:超过此值的查询会被记录为“慢查询”,便于后续分析和优化。您的当前设置(10 秒或更长)意味着只有极慢的查询(如复杂 JOIN 或大表扫描)才会被捕获,这会遗漏许多潜在瓶颈,导致性能问题隐蔽。根据 MySQL 官方文档和 Percona 最佳实践,默认 10 秒阈值适合低负载测试环境,但对于生产服务器(每日查询量 >10 万),它会让慢查询日志过于稀疏,无法及时发现问题,如索引缺失或锁争用。结果是数据库响应时间悄然上升,用户体验下降,TPS(每秒事务数)降低 20-50%。

现状分析:long_query_time = 10 基于公式直接校验(value >= 10),这在 mysql-tuner 等工具中触发警告。10 秒阈值忽略了 1-5 秒的“中慢查询”,这些往往是优化高回报点(如用 EXPLAIN 分析)。在高访问量场景,建议调至 1-5 秒:1 秒适合实时应用(如电商),2-3 秒通用,5 秒用于报告型负载。调短后,日志会更丰富,但需监控日志大小(/var/log/mysql/mariadb-slow.log),避免磁盘压力。

long_query_time = 5  # 推荐 1-5 秒,根据负载测试
slow_query_log = 1   # 启用慢查询日志(如果未开)
slow_query_log_file = /var/log/mysql/mariadb-slow.log
log_slow_verbosity = query_plan,explain  # 记录执行计划
log_queries_not_using_indexes = 1  # 记录未用索引查询
log_slow_admin_statements = 1  # 记录管理语句

 

调优后,慢查询日志将捕获更多细节,帮助快速定位问题,整体查询速度提升 30%+。但日志文件会增大(每日 10-100MB),定期轮转:logrotate /var/log/mysql/mariadb-slow.log。高负载下结合 EXPLAIN 和索引优化,避免过度日志。定期运行 mysql-tuner 复查,确保阈值匹配实际(如从 2 秒调至 3 秒)。

通过这些调整,您的 MySQL 将更敏捷,助力网站高效运行。优化是持续过程,结合监控工具如 Grafana,效果更佳。

下面是一个完整的MySQL配置,可以用于生产环境

#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
[mysqld]

#
# * Basic Settings
#

#user                    = mysql
pid-file                = /run/mysqld/mysqld.pid
basedir                 = /usr
#datadir                 = /var/lib/mysql
#tmpdir                  = /tmp

# Broken reverse DNS slows down connections considerably and name resolve is
# safe to skip if there are no "host by domain name" access grants
#skip-name-resolve

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address            = 127.0.0.1

#
# * Fine Tuning
#

key_buffer_size         = 512M
max_allowed_packet      = 2G
thread_stack            = 192K
thread_cache_size       = 1024
tmp_table_size          = 1024M
max_heap_table_size     = 1024M
open_files_limit        = 0

#key_buffer_size        = 128M
#max_allowed_packet     = 1G
#thread_stack           = 192K
#thread_cache_size      = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
#myisam_recover_options = BACKUP
#max_connections        = 100
#table_cache            = 64

myisam_recover_options  = BACKUP
max_connections         = 9999
table_open_cache        = 40000

#
# * Logging and Replication
#

# Note: The configured log file or its directory need to be created
# and be writable by the mysql user, e.g.:
# $ sudo mkdir -m 2750 /var/log/mysql
# $ sudo chown mysql /var/log/mysql



# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# Recommend only changing this at runtime for short testing periods if needed!
#general_log_file       = /var/log/mysql/mysql.log
#general_log            = 1

# When running under systemd, error logging goes via stdout/stderr to journald
# and when running legacy init error logging goes to syslog due to
# /etc/mysql/conf.d/mariadb.conf.d/50-mysqld_safe.cnf
# Enable this if you want to have error logging into a separate file
#log_error = /var/log/mysql/error.log
# Enable the slow query log to see queries with especially long duration
#log_slow_query_file    = /var/log/mysql/mariadb-slow.log
#log_slow_query_time    = 10
#log_slow_verbosity     = query_plan,explain
#log-queries-not-using-indexes
#log_slow_min_examined_row_limit = 1000

long_query_time =5  # 推荐 1-5 秒,根据负载测试
slow_query_log = 1   # 启用慢查询日志(如果未开)
slow_query_log_file = /var/log/mysql/mariadb-slow.log
log_slow_verbosity = query_plan,explain  # 记录执行计划
log_queries_not_using_indexes = 1  # 记录未用索引查询
log_slow_admin_statements = 1  # 记录管理语句


# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replica, see README.Debian about other
#       settings you may need to change.
#server-id              = 1
#log_bin                = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10
#max_binlog_size        = 100M

#
# * SSL/TLS
#

# For documentation, please read
# https://mariadb.com/kb/en/securing-connections-for-client-and-server/
#ssl-ca = /etc/mysql/cacert.pem
#ssl-cert = /etc/mysql/server-cert.pem
#ssl-key = /etc/mysql/server-key.pem
#require-secure-transport = on

#
# * Character sets
#

# MySQL/MariaDB default is Latin1, but in Debian we rather default to the full
# utf8 4-byte character set. See also client.cnf
character-set-server  = utf8mb4
collation-server      = utf8mb4_unicode_ci

#
# * InnoDB
#

# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
# Most important is to give InnoDB 80 % of the system RAM for buffer use:
# https://mariadb.com/kb/en/innodb-system-variables/#innodb_buffer_pool_size
#innodb_buffer_pool_size = 8G

innodb_buffer_pool_size = 48G
innodb_buffer_pool_instances = 16
innodb_log_file_size = 12G
innodb_flush_log_at_trx_commit = 2
innodb_flush_method = O_DIRECT
innodb_io_capacity = 2000
innodb_read_io_threads = 8
innodb_write_io_threads = 8

# this is only for embedded server
[embedded]

# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]

# This group is only read by MariaDB-10.11 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.11]

 

점수

댓글남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다