MySQLのパフォーマンスチューニングはmysqltuner.plが便利

MySQLのチューニング箇所を提案してくれるmysqltuner.pl

MySQLのパフォーマンスチューニングにはmysqltuner.plが便利です。現在のサーバーのMySQLの使用状況や/etc/my.cnfの設定を分析して、最適なチューニング法を提案してくれます。

まずMySQLTuner-perlをダウンロードしましょう。

$ git clone https://github.com/major/MySQLTuner-perl

次にmysqltuner.plを実行します。

$ cd MySQLTuner-perl
$ perl mysqltuner.pl

MySQLの管理権限ユーザーのユーザー名とパスワードを聞いてくるので入力します。その後、MySQLの設定に関する診断とレポートが行われて、最適なチューニング方法の提案が表示されます。

 >>  MySQLTuner 1.7.3 - Major Hayden <major@mhtx.net>
 >>  Bug reports, feature requests, and downloads at http://mysqltuner.com/
 >>  Run with '--help' for additional options and output filtering

[--] Skipped version check for MySQLTuner script
Please enter your MySQL administrative login: root
Please enter your MySQL administrative password: [OK] Currently running supported MySQL version 5.7.19
[OK] Operating on 64-bit architecture

-------- Log file Recommendations ------------------------------------------------------------------
[--] Log file: /var/log/mysqld.log(191K)
[OK] Log file /var/log/mysqld.log exists
[!!] Log file /var/log/mysqld.log isn't readable.

-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MEMORY +MRG_MYISAM +MyISAM +PERFORMANCE_SCHEMA
[--] Data in MyISAM tables: 9M (Tables: 4)
[--] Data in InnoDB tables: 20M (Tables: 91)
[OK] Total fragmented tables: 0

-------- Security Recommendations ------------------------------------------------------------------
[OK] There are no anonymous accounts for any database users

-------- CVE Security Recommendations --------------------------------------------------------------
[--] Skipped due to --cvefile option undefined

-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 16h 2m 52s (438K q [7.583 qps], 6K conn, TX: 5G, RX: 85M)
[--] Reads / Writes: 95% / 5%
[--] Binary logging is disabled
[--] Physical Memory     : 3.9G
[--] Max MySQL memory    : 1.6G
[--] Other process memory: 1.1G
[--] Total buffers: 1.1G global + 1.1M per thread (512 max threads)
[--] P_S Max memory usage: 72B
[--] Galera GCache Max memory usage: 0B
[OK] Maximum reached memory usage: 1.1G (28.27% of installed RAM)
[OK] Maximum possible memory usage: 1.6G (42.70% of installed RAM)
[OK] Overall possible memory usage with other process is compatible with memory available
[OK] Slow queries: 0% (0/438K)
[OK] Highest usage of available connections: 0% (5/512)
[OK] Aborted connections: 0.07%  (4/6127)
[!!] name resolution is active : a reverse name resolution is made for each new connection and can reduce performance
[OK] Query cache is disabled by default due to mutex contention on multiprocessor machines.
[OK] Sorts requiring temporary tables: 0% (1 temp sorts / 55K sorts)
[OK] No joins without indexes
[!!] Temporary tables created on disk: 73% (13K on disk / 17K total)
[OK] Thread cache hit rate: 99% (5 created / 6K connections)
[OK] Table cache hit rate: 56% (1K open / 2K opened)
[OK] Open file limit used: 2% (107/5K)
[OK] Table locks acquired immediately: 100% (84K immediate / 84K locks)

-------- Performance schema ------------------------------------------------------------------------
[--] Memory used by P_S: 72B
[--] Sys schema is installed.

-------- ThreadPool Metrics ------------------------------------------------------------------------
[--] ThreadPool stat is disabled.

-------- MyISAM Metrics ----------------------------------------------------------------------------
[!!] Key buffer used: 25.5% (2M used / 8M cache)
[OK] Key buffer size / total MyISAM indexes: 8.0M/3.6M
[OK] Read Key buffer hit rate: 99.9% (553K cached / 588 reads)
[!!] Write Key buffer hit rate: 14.8% (3K cached / 463 writes)

-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[--] InnoDB Thread Concurrency: 0
[OK] InnoDB File per table is activated
[OK] InnoDB buffer pool / data size: 1.0G/20.1M
[OK] Ratio InnoDB log file size / InnoDB Buffer pool size: 128.0M * 2/1.0G should be equal 25%
[OK] InnoDB buffer pool instances: 1
[--] Number of InnoDB Buffer Pool Chunk : 8 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: 99.99% (12806082 hits/ 12807199 total)
[!!] InnoDB Write Log efficiency: 88.34% (164903 hits/ 186664 total)
[OK] InnoDB log waits: 0.00% (0 waits / 21761 writes)

-------- AriaDB Metrics ----------------------------------------------------------------------------
[--] AriaDB is disabled.

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

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

-------- RocksDB Metrics ---------------------------------------------------------------------------
[--] RocksDB is disabled.

-------- Spider Metrics ----------------------------------------------------------------------------
[--] Spider is disabled.

-------- Connect Metrics ---------------------------------------------------------------------------
[--] Connect is disabled.

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

-------- Replication Metrics -----------------------------------------------------------------------
[--] Galera Synchronous replication: NO
[--] No replication slave(s) for this server.
[--] This is a standalone server.

-------- Recommendations ---------------------------------------------------------------------------
General recommendations:
    Set up a Password for user with the following SQL statement ( SET PASSWORD FOR 'user'@'SpecificDNSorIp' = PASSWORD('secure_password'); )
    MySQL started within last 24 hours - recommendations may be inaccurate
    Configure your accounts with ip or subnets only, then update your configuration with skip-name-resolve=1
    When making adjustments, make tmp_table_size/max_heap_table_size equal
    Reduce your SELECT DISTINCT queries which have no LIMIT clause
Variables to adjust:
    tmp_table_size (> 16M)
    max_heap_table_size (> 16M)

ここではパスワードの強化、tmp_table_sizeを16MB以上にすること、max_heap_table_sizeを16MB以上にすることが提案されました。

利用状況に依るので常に最適とは限らないですが、1つの提案として参考になることは間違いないかと思います。