精品軟體與實用教程
在CyberPanel 中MySQL 寫入效能最佳化指南
在CyberPanel 的使用中,會遇到MySQL寫入緩慢的情況,正常時候資料庫寫入是沒有異常的,但是當網站流量增加之後,資料庫就會變得十分緩慢。
這時你會發現,CPU和記憶體還空閒很多,還沒佔用超過30%,頻寬也有剩餘,為什麼資料庫寫入會變得緩慢呢?
預設安裝的資料庫沒有經過最佳化配置,以下看如何優化MySQL資料庫。
MySQL 的寫入效能最佳化涉及硬體、配置、查詢設計和監控等多層次調整。根據實際負載(如高並發寫入),優化可將效能提升2-10 倍。
下面是在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 collpe-server = 代碼
設定檔中如果沒有的話,加入在[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 開銷甚至在高分時延遲表回落到磁碟區而非記憶體,導致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_log_file = /var/log/mysql/mariadb-slow.log log_slow_verbosity = querym-slow.記錄執行計劃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 emservered for serverssql daemon [mysqld] # # * Basic Settings # #user = mysql pid-file = /run/mysqld/mysqld.pid basedir = /usr #gdatadir = /var/lib/my DNS #mpin DNS 53smp 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 on left only n 。 1024M open_files_limit = 0 #key_buffer_size = 128M #max_allowed_packet = 1G #thread_stack = 192K #thread_cache_size = 8 # This replaces # the first time they are touched #myisam_recover_options = BACKUP #max_connections = 100 #table_cache = 64 myisam_recover_options = BACKUP max_connections = 64 myisam_recover_options = BACKUP max_connections = 64 myisam_recover_options = BACKUP max_connections = 199019000 * Logging and Replication # # Note: The configured log file or its directory need to be created # and be writable by the mysql user, eg: # $ sudo mkdir -m 2750 5/sql /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 pergeneds ifif /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 goror 1 /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.able 1 月 6 月 3 月 5 月 30 月 mysql15 月 15% 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-not15Tlog-quees-not1us_plans_ron_wvh 1000 long_query_time =5 # 建議1-5 秒,根據負載測試slow_query_log = 1 # 啟用慢查詢日誌(如果未開式) slow_query_log_file = /var/log/mysql/mariadb-slow.log log_slow_verbossql/mariadb-slow.log log_slow_verbossql/mariadb-slow.log log_slow_verbossql/mariadb-slow。記錄執行計劃log_queries_not_using_indexes = 1 # 記錄未使用索引查詢log_slow_admin_statements = 1 # 記錄管理語句# The following can be used as easy to replay backup logs or for repliting be used as easy to replay backup logs or for repliting: 15 月, youf 15 月 3up 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_bin.log expire_logs_days = 10 #max_binlog_size = 15151515155 STP # 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/cacert.pem #ssl-cert = /etc/mysql-cert.pem. /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 default is Latin1, but in Debian we rather default to the full 1. client.cnf character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci # # * InnoDB # # InnoDB is enabled by default with a 10MB datafile in / 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/#innfin_bize; 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_trx_commit = 2 innodb_flush_甲基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 read by MariaDB servers, not by MySQL. # If you read the same for Mariafm. options here [mariadb] # This group is only read by MariaDB-10.11 servers. # If you use the same .cnf file for MariaDB of different serverions, # use this dbst options DB of different versions, # use this