MariaDB 优化

MariaDB 优化

我在 DigitalOcean 上有一个单核、1GB RAM 服务器。操作系统:CentOS Linux 版本 7.4.1708(核心)mysql Ver 15.1 Distrib 5.5.56-MariaDB,适用于 Linux(x86_64),使用 readline 5.1 此服务器上没有其他服务。

配置文件:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
slow-query-log=1
long-query-time=1
innodb_buffer_pool_size=512M

innodb_file_per_table
skip-name-resolve

query_cache_type=1
query_cache_size=32M
query_cache_limit=16M
thread_cache_size=64M
max_connections=100
join_buffer_size=32M
key_buffer_size=128M
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

MySQLTuner 输出

[root@db1 ~]# ./mysqltuner.pl 
>>  MySQLTuner 1.7.1 - Major Hayden <[email protected]>
>>  Bug reports, feature requests, and downloads at 
>>  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.5.56-MariaDB
[OK] Operating on 64-bit architecture

-------- Log file Recommendations ------------------------------------------------------------------
[--] Log file: /var/log/mariadb/mariadb.log(29K)
[OK] Log file /var/log/mariadb/mariadb.log exists
[OK] Log file /var/log/mariadb/mariadb.log is readable.
[OK] Log file /var/log/mariadb/mariadb.log is not empty
[OK] Log file /var/log/mariadb/mariadb.log is smaller than 32 Mb
[!!] /var/log/mariadb/mariadb.log contains 50 warning(s).
[!!] /var/log/mariadb/mariadb.log contains 1 error(s).
[--] 16 start(s) detected in /var/log/mariadb/mariadb.log
[--] 1) 180521 13:31:59 [Note] /usr/libexec/mysqld: ready for connections.
[--] 2) 180521 12:47:05 [Note] /usr/libexec/mysqld: ready for connections.
[--] 3) 180521  8:11:43 [Note] /usr/libexec/mysqld: ready for connections.
[--] 4) 180421 22:09:18 [Note] /usr/libexec/mysqld: ready for connections.
[--] 5) 180218  3:37:14 [Note] /usr/libexec/mysqld: ready for connections.
[--] 6) 180218  2:57:02 [Note] /usr/libexec/mysqld: ready for connections.
[--] 7) 180213 20:53:56 [Note] /usr/libexec/mysqld: ready for connections.
[--] 8) 180207 10:59:05 [Note] /usr/libexec/mysqld: ready for connections.
[--] 9) 180207 10:39:52 [Note] /usr/libexec/mysqld: ready for connections.
[--] 10) 180207 10:31:00 [Note] /usr/libexec/mysqld: ready for connections.
[--] 13 shutdown(s) detected in /var/log/mariadb/mariadb.log
[--] 1) 180521 13:31:56 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 2) 180521 12:47:02 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 3) 180521  8:11:40 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 4) 180421 22:09:15 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 5) 180213 20:39:30 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 6) 180207 10:59:03 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 7) 180207 10:39:50 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 8) 180207 10:30:57 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 9) 180207 10:28:46 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 10) 180207 10:01:14 [Note] /usr/libexec/mysqld: Shutdown complete

-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Status: +ARCHIVE +Aria +BLACKHOLE +CSV +FEDERATED +InnoDB +MEMORY +MRG_MYISAM +MyISAM +PERFORMANCE_SCHEMA 
[--] Data in MyISAM tables: 69M (Tables: 2)
[--] Data in InnoDB tables: 106M (Tables: 1057)
[OK] Total fragmented tables: 0

-------- Security Recommendations ------------------------------------------------------------------
[OK] There are no anonymous accounts for any database users
[OK] All database users have passwords assigned
[!!] There is no basic password file list!

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

-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 2d 9h 58m 16s (113K q [0.542 qps], 13K conn, TX: 933M, RX: 9M)
[--] Reads / Writes: 85% / 15%
[--] Binary logging is disabled
[--] Physical Memory     : 992.4M
[--] Max MySQL memory    : 4.2G
[--] Other process memory: 98.2M
[--] Total buffers: 832.0M global + 34.7M per thread (100 max threads)
[--] P_S Max memory usage: 0B
[--] Galera GCache Max memory usage: 0B
[!!] Maximum reached memory usage: 3.1G (317.81% of installed RAM)
[!!] Maximum possible memory usage: 4.2G (433.05% of installed RAM)
[!!] Overall possible memory usage with other process exceeded memory
[OK] Slow queries: 0% (981/113K)
[OK] Highest usage of available connections: 67% (67/100)
[OK] Aborted connections: 0.03%  (4/13450)
[OK] Query cache is disabled by default due to mutex contention on multiprocessor machines.
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 8K sorts)
[!!] Joins performed without indexes: 682
[!!] Temporary tables created on disk: 63% (13K on disk / 21K total)
[OK] Thread cache hit rate: 99% (67 created / 13K connections)
[!!] Table cache hit rate: 2% (400 open / 17K opened)
[OK] Open file limit used: 7% (80/1K)
[OK] Table locks acquired immediately: 99% (42K immediate / 42K locks)

-------- Performance schema ------------------------------------------------------------------------
[--] Performance schema is disabled.
[--] Memory used by P_S: 0B
[--] Sys schema isn't installed.

-------- ThreadPool Metrics ------------------------------------------------------------------------
[--] ThreadPool stat is enabled.
[--] Thread Pool Size: 1 thread(s).
[--] Using default value is good enough for your version (5.5.56-MariaDB)

-------- MyISAM Metrics ----------------------------------------------------------------------------
[!!] Key buffer used: 18.3% (24M used / 134M cache)
[OK] Key buffer size / total MyISAM indexes: 128.0M/4.8M
[OK] Read Key buffer hit rate: 98.5% (50K cached / 756 reads)
[!!] Write Key buffer hit rate: 33.4% (36K cached / 12K writes)

-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[--] InnoDB Thread Concurrency: 0
[OK] InnoDB File per table is activated
[OK] InnoDB buffer pool / data size: 512.0M/106.2M
[!!] Ratio InnoDB log file size / InnoDB Buffer pool size (1.953125 %): 5.0M * 2/512.0M should be equal 25%
[OK] InnoDB buffer pool instances: 1
[--] InnoDB Buffer Pool Chunk Size not used or defined in your version
[OK] InnoDB Read buffer efficiency: 100.00% (843826738 hits/ 843833107 total)
[!!] InnoDB Write Log efficiency: 9.23% (168 hits/ 1820 total)
[OK] InnoDB log waits: 0.00% (0 waits / 1652 writes)

-------- AriaDB Metrics ----------------------------------------------------------------------------
[--] AriaDB is enabled.
[OK] Aria pagecache size / total Aria indexes: 128.0M/1B
[!!] Aria pagecache hit rate: 91.9% (80K cached / 6K reads)

-------- 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:
Control warning line(s) into /var/log/mariadb/mariadb.log file
Control error line(s) into /var/log/mariadb/mariadb.log file
Reduce your overall MySQL memory footprint for system stability
Dedicate this server to your database for highest performance.
Adjust your join queries to always utilize indexes
When making adjustments, make tmp_table_size/max_heap_table_size equal
Reduce your SELECT DISTINCT queries which have no LIMIT clause
Increase table_open_cache gradually to avoid file descriptor limits
Read this before increasing table_open_cache over 64:
Beware that open_files_limit (1024) variable 
should be greater than table_open_cache (400)
Performance shouldn't be activated for MySQL and MariaDB 5.5 and lower version
Consider installing Sys schema from https://github.com/mysql/mysql-sys
Variables to adjust:
*** MySQL's maximum memory usage is dangerously high ***
*** Add RAM before increasing MySQL buffer variables ***
join_buffer_size (> 32.0M, or always use indexes with joins)
tmp_table_size (> 16M)
max_heap_table_size (> 16M)
table_open_cache (> 400)
performance_schema = OFF disable PFS
innodb_log_file_size * innodb_log_files_in_group should be equals to 1/4 of buffer pool size (=256M) if possible.
[root@db1 ~]# 

我不是 MySQL 专家。有人能帮我调整参数来提高数据库效率吗?

谢谢

=======================================================================

这是最新报告。我已将我不理解且需要帮助的建议加上前缀 **。

[root@db1 ~]# ./mysqltuner.pl 
 >>  MySQLTuner 1.7.1 - Major Hayden <[email protected]>
 >>  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.5.56-MariaDB
[OK] Operating on 64-bit architecture

-------- Log file Recommendations ------------------------------------------------------------------
[--] Log file: /var/log/mariadb/mariadb.log(30K)
[OK] Log file /var/log/mariadb/mariadb.log exists
[OK] Log file /var/log/mariadb/mariadb.log is readable.
[OK] Log file /var/log/mariadb/mariadb.log is not empty
[OK] Log file /var/log/mariadb/mariadb.log is smaller than 32 Mb
[!!] /var/log/mariadb/mariadb.log contains 51 warning(s).
[!!] /var/log/mariadb/mariadb.log contains 1 error(s).
[--] 17 start(s) detected in /var/log/mariadb/mariadb.log
[--] 1) 180527 12:09:12 [Note] /usr/libexec/mysqld: ready for connections.
[--] 2) 180521 13:31:59 [Note] /usr/libexec/mysqld: ready for connections.
[--] 3) 180521 12:47:05 [Note] /usr/libexec/mysqld: ready for connections.
[--] 4) 180521  8:11:43 [Note] /usr/libexec/mysqld: ready for connections.
[--] 5) 180421 22:09:18 [Note] /usr/libexec/mysqld: ready for connections.
[--] 6) 180218  3:37:14 [Note] /usr/libexec/mysqld: ready for connections.
[--] 7) 180218  2:57:02 [Note] /usr/libexec/mysqld: ready for connections.
[--] 8) 180213 20:53:56 [Note] /usr/libexec/mysqld: ready for connections.
[--] 9) 180207 10:59:05 [Note] /usr/libexec/mysqld: ready for connections.
[--] 10) 180207 10:39:52 [Note] /usr/libexec/mysqld: ready for connections.
[--] 14 shutdown(s) detected in /var/log/mariadb/mariadb.log
[--] 1) 180527 12:09:09 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 2) 180521 13:31:56 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 3) 180521 12:47:02 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 4) 180521  8:11:40 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 5) 180421 22:09:15 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 6) 180213 20:39:30 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 7) 180207 10:59:03 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 8) 180207 10:39:50 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 9) 180207 10:30:57 [Note] /usr/libexec/mysqld: Shutdown complete
[--] 10) 180207 10:28:46 [Note] /usr/libexec/mysqld: Shutdown complete

-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Status: +ARCHIVE +Aria +BLACKHOLE +CSV +FEDERATED +InnoDB +MEMORY +MRG_MYISAM +MyISAM +PERFORMANCE_SCHEMA 
[--] Data in MyISAM tables: 70M (Tables: 2)
[--] Data in InnoDB tables: 107M (Tables: 1057)
[OK] Total fragmented tables: 0

-------- Security Recommendations ------------------------------------------------------------------
[OK] There are no anonymous accounts for any database users
[OK] All database users have passwords assigned
[!!] There is no basic password file list!

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

-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 1d 0h 13m 18s (37K q [0.434 qps], 4K conn, TX: 534M, RX: 2M)
[--] Reads / Writes: 87% / 13%
[--] Binary logging is disabled
[--] Physical Memory     : 992.4M
[--] Max MySQL memory    : 1.0G
[--] Other process memory: 99.0M
[--] Total buffers: 736.0M global + 2.9M per thread (100 max threads)
[--] P_S Max memory usage: 0B
[--] Galera GCache Max memory usage: 0B
[OK] Maximum reached memory usage: 820.3M (82.66% of installed RAM)
[!!] Maximum possible memory usage: 1.0G (103.45% of installed RAM)
[!!] Overall possible memory usage with other process exceeded memory
[OK] Slow queries: 0% (336/37K)
[OK] Highest usage of available connections: 28% (29/100)
[OK] Aborted connections: 0.06%  (3/4987)
[OK] Query cache is disabled by default due to mutex contention on multiprocessor machines.
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 3K sorts)
[OK] No joins without indexes
[!!] Temporary tables created on disk: 67% (4K on disk / 6K total)
[OK] Thread cache hit rate: 99% (29 created / 4K connections)
[!!] Table cache hit rate: 5% (400 open / 7K opened)
[OK] Open file limit used: 7% (80/1K)
[OK] Table locks acquired immediately: 99% (13K immediate / 13K locks)

-------- Performance schema ------------------------------------------------------------------------
[--] Performance schema is disabled.
[--] Memory used by P_S: 0B
[--] Sys schema isn't installed.

-------- ThreadPool Metrics ------------------------------------------------------------------------
[--] ThreadPool stat is enabled.
[--] Thread Pool Size: 1 thread(s).
[--] Using default value is good enough for your version (5.5.56-MariaDB)

-------- MyISAM Metrics ----------------------------------------------------------------------------
[!!] Key buffer used: 18.4% (12M used / 67M cache)
[OK] Key buffer size / total MyISAM indexes: 64.0M/4.9M
[!!] Read Key buffer hit rate: 89.7% (1K cached / 143 reads)
[!!] Write Key buffer hit rate: 79.5% (898 cached / 714 writes)

-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[--] InnoDB Thread Concurrency: 0
[OK] InnoDB File per table is activated
[OK] InnoDB buffer pool / data size: 512.0M/107.2M
[!!] Ratio InnoDB log file size / InnoDB Buffer pool size (1.953125 %): 5.0M * 2/512.0M should be equal 25%
[OK] InnoDB buffer pool instances: 1
[--] InnoDB Buffer Pool Chunk Size not used or defined in your version
[OK] InnoDB Read buffer efficiency: 100.00% (352323951 hits/ 352330431 total)
[!!] InnoDB Write Log efficiency: 7.78% (21 hits/ 270 total)
[OK] InnoDB log waits: 0.00% (0 waits / 249 writes)

-------- AriaDB Metrics ----------------------------------------------------------------------------
[--] AriaDB is enabled.
[OK] Aria pagecache size / total Aria indexes: 128.0M/1B
[!!] Aria pagecache hit rate: 91.7% (26K cached / 2K reads)

-------- 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:
    Control warning line(s) into /var/log/mariadb/mariadb.log file
    Control error line(s) into /var/log/mariadb/mariadb.log file
    ** Reduce your overall MySQL memory footprint for system stability
    Dedicate this server to your database for highest performance.
    ** When making adjustments, make tmp_table_size/max_heap_table_size equal
    Reduce your SELECT DISTINCT queries which have no LIMIT clause
    ** Increase table_open_cache gradually to avoid file descriptor limits
    ** Read this before increasing table_open_cache over 64: 
    ** Beware that open_files_limit (1024) variable should be greater than table_open_cache (400)
    ** Performance shouldn't be activated for MySQL and MariaDB 5.5 and lower version
    Consider installing Sys schema from https://github.com/mysql/mysql-sys
Variables to adjust:
  *** MySQL's maximum memory usage is dangerously high ***
  *** Add RAM before increasing MySQL buffer variables ***
    tmp_table_size (> 16M)
    max_heap_table_size (> 16M)
    table_open_cache (> 400)
    performance_schema = OFF disable PFS
    innodb_log_file_size * innodb_log_files_in_group should be equals to 1/4 of buffer pool size (=256M) if possible.
[root@db1 ~]# 

答案1

针对 my.cnf-ini [mysqld] 部分的建议

query_cache_type=0  # from 1 for NO Query Cache
query_cache_size=0  # from 32M to avoid wasting RAM - only 1G available
query_cache_limit=0  # from 16K QC will not be used
thread_cache_size=100  # from 64M an unreasonable value
join_buffer_size=256K  # from 32M to minimize RAM required PER connection
key_buffer_size=64M  # from 128M  your MyISAM involvement is minor

如需更多帮助,请参阅我的个人资料以获取联系信息。

相关内容