Skip to main content

แนวทางการเพิ่มประสิทธิภาพ MySQL และ MariaDB ด้วยการรัน MySQLTuner

Kongvut Sangkla

Intro

สวัสดีครับ บทความนี้จะแนะแนวทางการรันสคริป MySQLTuner ที่เขียนด้วยภาษา Perl (รองรับ MySQL/MariaDB) โดยสคริปจะตรวจสอบแนะแนวทางการปรับปรุงค่า Configs ต่าง ๆ ของ Database Server หลังจากนั้นเราต้องแก้ไขค่าต่าง ๆ ตามคำแนะนำที่ไฟล์ config-file.cnfเพื่อเพิ่มประสิทธิภาพของ Database server 🚀

เริ่มกันเลย

Download MySQLTuner
mkdir mysqltuner && cd mysqltuner \
&& wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/mysqltuner.pl -O mysqltuner.pl \
&& wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/basic_passwords.txt -O basic_passwords.txt \
&& wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/vulnerabilities.csv -O vulnerabilities.csv
ตัวอย่างการสั่งรันด้วย Docker
docker run -it -v $PWD:/etc/mysqltuner \
-w /etc/mysqltuner \
--rm mariadb:10.5 \
perl mysqltuner.pl --forcemem 8192 --forceswap 2048 --host 192.168.32.30 --user root --pass root --port 3308
note

--forcemem คือ ขนาด Ram ของ Server

--forceswap คือ ขนาด Memory Swap size

--host ระบุ IP ของ Database Server

--user ระบุ Username ของ Database Server

--pass ระบุ Password ของ Database Server

--port ระบุ Port ของ Database Server

Results of MySQLTuner
 >>  MySQLTuner 1.8.1 - Major Hayden <[email protected]>
>> Bug reports, feature requests, and downloads at http://mysqltuner.pl/
>> Run with '--help' for additional options and output filtering

[--] Skipped version check for MySQLTuner script
[--] Performing tests on 192.168.32.30:3308
[OK] Logged in using credentials passed on the command line
[--] Assuming 8192 MB of physical memory
[--] Assuming 2048 MB of swap space
[OK] Currently running supported MySQL version 10.5.9-MariaDB-1:10.5.9+maria~focal

-------- Log file Recommendations ------------------------------------------------------------------
[!!] Log file doesn\'t exist

-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Status: +Aria +CSV +InnoDB +MEMORY +MRG_MyISAM +MyISAM +PERFORMANCE_SCHEMA +SEQUENCE
[--] Data in InnoDB tables: 73.8M (Tables: 40)
[OK] Total fragmented tables: 0

-------- Analysis Performance Metrics --------------------------------------------------------------
[--] innodb_stats_on_metadata: OFF
[OK] No stat updates during querying INFORMATION_SCHEMA.

-------- Security Recommendations ------------------------------------------------------------------
[OK] There are no anonymous accounts for any database users
[OK] All database users have passwords assigned
[!!] User 'root'@'%' has user name as password.
[!!] User 'root'@'localhost' has user name as password.
[!!] User 'root'@% does not specify hostname restrictions.
[--] There are 620 basic passwords in the list.
[!!] User 'root@localhost' is using weak password: root in a lower, upper or capitalize derivative version.
[!!] User 'root@%' is using weak password: root in a lower, upper or capitalize derivative version.

-------- CVE Security Recommendations --------------------------------------------------------------
[OK] NO SECURITY CVE FOUND FOR YOUR VERSION

-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 8d 0h 43m 31s (14K q [0.022 qps], 2K conn, TX: 12M, RX: 1M)
[--] Reads / Writes: 49% / 51%
[--] Binary logging is disabled
[--] Physical Memory : 8.0G
[--] Max MySQL memory : 7.1G
[--] Other process memory: 0B
[--] Total buffers: 4.3G global + 18.9M per thread (151 max threads)
[--] P_S Max memory usage: 72B
[--] Galera GCache Max memory usage: 0B
[OK] Maximum reached memory usage: 4.4G (55.26% of installed RAM)
[!!] Maximum possible memory usage: 7.1G (88.96% of installed RAM)
[OK] Overall possible memory usage with other process is compatible with memory available
[OK] Slow queries: 0% (68/14K)
[OK] Highest usage of available connections: 3% (5/151)
[OK] Aborted connections: 0.00% (0/2448)
[OK] Query cache is disabled by default due to mutex contention on multiprocessor machines.
[OK] Sorts requiring temporary tables: 5% (60 temp sorts / 1K sorts)
[OK] No joins without indexes
[!!] Temporary tables created on disk: 31% (206 on disk / 660 total)
[OK] Thread cache hit rate: 98% (31 created / 2K connections)
[OK] Table cache hit rate: 80% (21K hits / 26K requests)
[OK] table_definition_cache(400) is upper than number of tables(245)
[OK] Open file limit used: 0% (30/32K)
[OK] Table locks acquired immediately: 100% (54 immediate / 54 locks)

-------- Performance schema ------------------------------------------------------------------------
[--] Memory used by P_S: 72B
[--] Sys schema isn\'t installed.

-------- ThreadPool Metrics ------------------------------------------------------------------------
[--] ThreadPool stat is enabled.
[--] Thread Pool Size: 2 thread(s).
[--] Using default value is good enough for your version (10.5.9-MariaDB-1:10.5.9+maria~focal)

-------- MyISAM Metrics ----------------------------------------------------------------------------
[!!] Key buffer used: 18.2% (24M used / 134M cache)
[OK] Key buffer size / total MyISAM indexes: 128.0M/0B

-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[--] InnoDB Thread Concurrency: 0
[OK] InnoDB File per table is activated
[OK] InnoDB buffer pool / data size: 4.0G/73.8M
[!!] Ratio InnoDB log file size / InnoDB Buffer pool size (12.5 %): 512.0M * 1/4.0G should be equal to 25%
[--] Number of InnoDB Buffer Pool Chunk : 32 for 1 Buffer Pool Instance(s)
[OK] Innodb_buffer_pool_size aligned with Innodb_buffer_pool_chunk_size & Innodb_buffer_pool_instances
[OK] InnoDB Read buffer efficiency: 100.00% (5782841951 hits/ 5782846513 total)
[!!] InnoDB Write Log efficiency: 989.79% (4266 hits/ 431 total)
[OK] InnoDB log waits: 0.00% (0 waits / 4697 writes)

-------- Aria Metrics ------------------------------------------------------------------------------
[--] Aria Storage Engine is enabled.
[OK] Aria pagecache size / total Aria indexes: 128.0M/2.5M
[OK] Aria pagecache hit rate: 97.3% (7K cached / 196 reads)

-------- TokuDB Metrics ----------------------------------------------------------------------------
[--] TokuDB is disabled.

-------- XtraDB Metrics ----------------------------------------------------------------------------
[--] XtraDB is disabled.

-------- Galera Metrics ----------------------------------------------------------------------------
[--] Galera is disabled.

-------- Replication Metrics -----------------------------------------------------------------------
[--] Galera Synchronous replication: NO
[--] No replication slave(s) for this server.
[--] Binlog format: MIXED
[--] XA support enabled: ON
[--] Semi synchronous replication Master: OFF
[--] Semi synchronous replication Slave: OFF
[--] This is a standalone server

-------- Recommendations ---------------------------------------------------------------------------
General recommendations:
Set up a Secure Password for 'root'@'%' user: SET PASSWORD FOR 'root'@'%' = PASSWORD('secure_password');
Set up a Secure Password for 'root'@'localhost' user: SET PASSWORD FOR 'root'@'localhost' = PASSWORD('secure_password');
Restrict Host for 'root'@'%' to 'root'@LimitedIPRangeOrLocalhost
RENAME USER 'root'@'%' TO 'root'@LimitedIPRangeOrLocalhost;
Set up a Secure Password for root@localhost user: SET PASSWORD FOR 'root'@'localhost' = PASSWORD('secure_password');
Set up a Secure Password for root@% user: SET PASSWORD FOR 'root'@'%' = PASSWORD('secure_password');
2 user(s) used basic or weak password from basic dictionary.
Reduce your overall MySQL memory footprint for system stability
When making adjustments, make tmp_table_size/max_heap_table_size equal
Reduce your SELECT DISTINCT queries which have no LIMIT clause
Consider installing Sys schema from https://github.com/mysql/mysql-sys for MySQL
Consider installing Sys schema from https://github.com/FromDual/mariadb-sys for MariaDB
Before changing innodb_log_file_size and/or innodb_log_files_in_group read this: https://bit.ly/2TcGgtU
Variables to adjust:
tmp_table_size (> 64M)
max_heap_table_size (> 64M)
innodb_log_file_size should be (=1G) if possible, so InnoDB total log files size equals to 25% of buffer pool size.

สรุป

เมื่อรัน Script สำเร็จจะแสดงผลลัพธ์และ Recommendations แบ่งออกเป็น 2 อย่างคือ

  • General recommendations ส่วนคำแนะนำสำหรับการตั้งค่าทั่วไป (ที่ควรทำ)
  • Variables to adjust คือ ส่วนคำแนะนำในการแก้ไขค่าต่าง ๆ ที่ไฟล์ config-file.cnf เราสามารถ Override ค่าต่าง ๆ ได้ (ปกติไฟล์จะอยู่ที่ /etc/mysql/conf.d) เมื่อแก้ไขค่าความคำแนะนำเรียบร้อยให้ Restart service ของ Database ก็เป็นอันเสร็จเรียบร้อย
ตัวอย่างไฟล์ config-file.cnf
[mysqld]
...
tmp_table_size = 256M
max_heap_table_size = 256M
innodb_log_file_size = 1G
...

อ้างอิงและรายละเอียดอื่น ๆ

Loading...