0

我的服务器中的 mysql 占用了大量的 CPU 和内存。

我的配置如下:

  • 英特尔至强 E5-2640 v4 @ 2.40GHz(4 核)
  • 8GB 内存 DDR4 ECC
  • 120GB SSD RAID-10
  • 未计量流量/100Mbit

我已经尝试优化一些请求并降低一些 cron 作业的频率,但服务器仍然不时变慢。这是我的 my.cnf 代码:

  [mysqld]
    performance-schema=0
    #
    # Remove leading # and set to the amount of RAM for the most important data
    # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    innodb_buffer_pool_size = 128M
    #
    # Remove leading # to turn on a very important data integrity option: logging
    # changes to the binary log between backups.
    # log_bin
    #
    # Remove leading # to set options mainly useful for reporting servers.
    # The server defaults are faster for transactions and fast SELECTs.
    # Adjust sizes as needed, experiment to find the optimal values.
    skip-innodb
    join_buffer_size = 1G
    sort_buffer_size = 256M
    key_buffer_size = 20M
    read_rnd_buffer_size = 2M
    max_connections=250
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    slow_query_log=1
    slow_query_log_file="/var/log/mysql/mysql-slow.log"
    long_query_time=1

    performance-schema=0
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0

    performance-schema=ON
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid
    innodb_file_per_table=1
    default-storage-engine=MyISAM
    max_allowed_packet=524288000
    open_files_limit=20000
    wait_timeout=300
    interactive_timeout = 300
    skip-external-locking
    skip-name-resolve
    query_cache_type = OFF
    query_cache_size = 0


    innodb_log_file_size = 16M
    tmp_table_size= 256M
    max_heap_table_size= 512M

I tried launching the command ./mysqltuner.pl on SSH. It gave me :

>>  MySQLTuner 1.7.15 - 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
[OK] Currently running supported MySQL version 5.7.27-log
[OK] Operating on 64-bit architecture

-------- Log file Recommendations ------------------------------------------------------------------
[--] Log file: /var/log/mysqld.log(42K)
[OK] Log file /var/log/mysqld.log exists
[OK] Log file /var/log/mysqld.log is readable.
[OK] Log file /var/log/mysqld.log is not empty
[OK] Log file /var/log/mysqld.log is smaller than 32 Mb
[!!] /var/log/mysqld.log contains 106 warning(s).
[!!] /var/log/mysqld.log contains 167 error(s).
[--] 4 start(s) detected in /var/log/mysqld.log
[--] 1) 2019-08-16T01:09:52.183997Z 0 [Note] /usr/sbin/mysqld: ready for connections.
[--] 2) 2019-08-15T23:21:04.491625Z 0 [Note] /usr/sbin/mysqld: ready for connections.
[--] 3) 2019-08-15T23:20:45.330911Z 0 [Note] /usr/sbin/mysqld: ready for connections.
[--] 4) 2019-08-15T13:43:15.808431Z 0 [Note] /usr/sbin/mysqld: ready for connections.
[--] 1 shutdown(s) detected in /var/log/mysqld.log
[--] 1) 2019-08-15T13:40:45.097276Z 0 [Note] /usr/sbin/mysqld: Shutdown complete

-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MEMORY +MRG_MYISAM +MyISAM +PERFORMANCE_SCHEMA
[--] Data in MyISAM tables: 400.7M (Tables: 146)
[--] Data in InnoDB tables: 16.0K (Tables: 1)
[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
[!!] failed to execute: SELECT CONCAT(user, '@', host) FROM mysql.user WHERE (IF(plugin='mysql_native_password', authentication_string, password) = '' OR IF(plugin='mysql_native_password', authentication_string, password) IS NULL) AND plugin NOT IN ('unix_socket', 'win_socket', 'auth_pam_compat')
[!!] FAIL Execute SQL / return code: 256
[OK] All database users have passwords assigned
[!!] failed to execute: SELECT CONCAT(user, '@', host) FROM mysql.user WHERE CAST(IF(plugin='mysql_native_password', authentication_string, password) as Binary) = PASSWORD(user) OR CAST(IF(plugin='mysql_native_password', authentication_string, password) as Binary) = PASSWORD(UPPER(user)) OR CAST(IF(plugin='mysql_native_password', authentication_string, password) as Binary) = PASSWORD(CONCAT(UPPER(LEFT(User, 1)), SUBSTRING(User, 2, LENGTH(User))))
[!!] FAIL Execute SQL / return code: 256
[!!] There is no basic password file list!

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

..

-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 6h 39m 4s (2M q [84.270 qps], 48K conn, TX: 31G, RX: 357M)
[--] Reads / Writes: 99% / 1%
[--] Binary logging is disabled
[--] Physical Memory     : 7.6G
[--] Max MySQL memory    : 313.5G
[--] Other process memory: 0B
[--] Total buffers: 420.0M global + 1.3G per thread (250 max threads)
[--] P_S Max memory usage: 72B
[--] Galera GCache Max memory usage: 0B
[!!] Maximum reached memory usage: 169.5G (2222.31% of installed RAM)
[!!] Maximum possible memory usage: 313.5G (4110.82% of installed RAM)
[!!] Overall possible memory usage with other process exceeded memory
[OK] Slow queries: 0% (0/2M)
[OK] Highest usage of available connections: 54% (135/250)
[OK] Aborted connections: 0.15%  (71/48029)
[OK] Query cache is disabled by default due to mutex contention on multiprocessor machines.
[OK] Sorts requiring temporary tables: 1% (5K temp sorts / 547K sorts)
[!!] Joins performed without indexes: 991
[!!] Temporary tables created on disk: 73% (131K on disk / 180K total)
[OK] Thread cache hit rate: 95% (1K created / 48K connections)
[!!] Table cache hit rate: 7% (2K open / 28K opened)
[OK] Open file limit used: 8% (1K/20K)
[OK] Table locks acquired immediately: 99% (2M immediate / 2M locks)

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

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

-------- MyISAM Metrics ----------------------------------------------------------------------------
[OK] Key buffer used: 100.0% (20M used / 20M cache)
[OK] Key buffer size / total MyISAM indexes: 20.0M/93.7M
[OK] Read Key buffer hit rate: 99.5% (1B cached / 6M reads)
[OK] Write Key buffer hit rate: 99.8% (6K cached / 6K writes)

-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[--] InnoDB Thread Concurrency: 0
[OK] InnoDB File per table is activated
[OK] InnoDB buffer pool / data size: 128.0M/16.0K
[OK] Ratio InnoDB log file size / InnoDB Buffer pool size: 16.0M * 2/128.0M should be equal to 25%
[OK] InnoDB buffer pool instances: 1
[--] Number of InnoDB Buffer Pool Chunk : 1 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% (26366073 hits/ 26366453 total)
[!!] InnoDB Write Log efficiency: 0% (2 hits/ 0 total)
[OK] InnoDB log waits: 0.00% (0 waits / 2 writes)

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

-------- 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: ROW
[--] XA support enabled: ON
[--] Semi synchronous replication Master: Not Activated
[--] Semi synchronous replication Slave: Not Activated
[--] This is a standalone server

-------- Recommendations ---------------------------------------------------------------------------
General recommendations:
    Control warning line(s) into /var/log/mysqld.log file
    Control error line(s) into /var/log/mysqld.log file
    MySQL was started within the last 24 hours - recommendations may be inaccurate
    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
    Temporary table size is already large - reduce result set size
    Reduce your SELECT DISTINCT queries without LIMIT clauses
    Increase table_open_cache gradually to avoid file descriptor limits
    Read this before increasing table_open_cache over 64: 
    Read this before increasing for MariaDB 
    This is MyISAM only table_cache scalability problem, InnoDB not affected.
    This bug already fixed in MySQL 5.7.9 and newer MySQL versions.
    Beware that open_files_limit (20000) variable
    should be greater than table_open_cache (2000)
Variables to adjust:
  *** MySQL's maximum memory usage is dangerously high ***
  *** Add RAM before increasing MySQL buffer variables ***
    join_buffer_size (> 1.0G, or always use indexes with JOINs)
    table_open_cache (> 2000)

Anyone to help me about the optimization I can do to my.cnf file or any other tweaks to lower memory and CPU consumption.

谢谢。

4

2 回答 2

0

建议您的 8GB 主机服务器 my.cnf [mysqld] 部分,PUT 前导 # 和前面的空格

join_buffer_size = 1G
sort_buffer_size = 256M
read_rnd_buffer_size = 2M

这三行允许默认值(这很好)为每个连接提供服务。这三个值导致了非常极端的 CPU 利用率。

停止/启动您的实例,然后在一个完整的工作日后,将 mysqltuner.pl 报告的完整结果发布到 pastebin.com,然后将您的问题中的链接发布给我们,拜托。

于 2019-08-17T17:02:33.140 回答
0

从 MyISAM 切换到 InnoDB,然后调整key_buffer_sizeinnodb_buffer_pool_size

开启慢日志;然后报告“最差”

于 2019-08-17T01:10:10.697 回答