
我有一個大約 6GB 的資料庫,一個表幾乎佔據了所有空間,當伺服器受到高負載時,表不斷崩潰,因此我必須停止 MySQL 伺服器並修復它,然後再次啟動它。任何想法為什麼以及什麼會導致這種情況以及我如何減輕這種情況?只有當伺服器訪問量突然大幅增加時才會發生這種情況。請參閱此範例影像,顯示上次崩潰發生在峰值期間:
當我要求我的託管提供者進行調查時,他們說了以下內容:
經過檢查,我們發現資料庫大小較大,當網站負載較高時,會存在大量資料庫查詢,因此資料庫崩潰的幾率會很高。
自由-M
total used free shared buff/cache available
Mem: 8342 1451 586 451 6304 6180
表格資訊
Table - Rows - Engine - encoding - Size
Stats: 22 020 753 MyISAM utf8_unicode_ci 6,0 GB
伺服器
Server: Localhost via UNIX socket
Servertyp: MariaDB
Server connection: SSL is not being used Dokumentation
Serverversion: 10.3.27-MariaDB - MariaDB Server
Protokollversion: 10
我的CNF
[mysql]
# CLIENT #
port = 3306
socket = /var/lib/mysql/mysql.sock
[mysqld]
log-error=/var/lib/mysql/server.err
performance-schema=0
table_open_cache=2000
innodb_strict_mode="ON"
sql_mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
open_files_limit=40000
# GENERAL #
user = mysql
default-storage-engine = InnoDB
socket = /var/lib/mysql/mysql.sock
pid-file = /var/lib/mysql/mysql.pid
# MyISAM #
key-buffer-size = 64M
myisam-sort-buffer-size = 64M
myisam-recover-options = FORCE
# SAFETY #
skip-external-locking
max-allowed-packet = 128M
max-connect-errors = 1000000
innodb = FORCE
# DATA STORAGE #
datadir = /var/lib/mysql/
# CACHES AND LIMITS #
tmp-table-size = 32M
max-heap-table-size = 32M
query-cache-type = 0
query-cache-size = 32
max-connections = 500
thread-cache-size = 286
open-files-limit = 65535
table-definition-cache = 1024
table-open-cache = 512
group-concat-max-len = 1048576
# INNODB #
#innodb-flush-method = O_DIRECT
innodb-log-files-in-group = 2
innodb-log-file-size = 512M
innodb-log-buffer-size = 16M
innodb-flush-log-at-trx-commit = 2
innodb-file-per-table = 1
innodb-buffer-pool-size = 4G
# LOGGING #
# log-queries-not-using-indexes = 1
# slow-query-log = 1
# slow-query-log-file = /var/lib/mysql/mysql-slow.log
max_allowed_packet=268435456
innodb_file_per_table=1
[mysqldump]
quick
max_allowed_packet = 128M
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
答案1
現在,將 key_buffer_size 增加到 1G。
從長遠來看,從 MyISAM 遷移到 InnoDB(並更改快取大小)。這至少可以消除「修復」的需要。
問題發生時正在運行的頑皮查詢是什麼?還是你認為是「用戶太多」互相絆倒?我見過這樣的。快速解決方法是減少 max_connections
比方說,200 個,同時也減少 Web 伺服器維護的「子級」數量——這將首先防止建立過多的連線。
如果這些沒有幫助,請按照此處的說明提供更多資訊: http://mysql.rjweb.org/doc.php/mysql_analysis
在 下進行更改(或新增)[mysqld]
。這就是重要的伺服器名稱。 myisamchk
是一個單獨的實用程式。
這改變了一張表:
ALTER TABLE t ENGINE=InnoDB;
請參閱此內容以取得調整 key_buffer_size 和 innodb_buffer_pool_size 的一些說明: http://mysql.rjweb.org/doc.php/memory