1

我是具有多年构建小型网站经验的初学者。然而,最近我的一个网站流量很大,mysql已经成为增长的瓶颈。

我花了几个小时试图弄清楚它是如何工作的。

您能否建议我如何优化我的 mysql 数据库以在我的电子商务平台上处理 25,000 多个每日访问者。

当前 my.cnf 具有以下信息:

disable-log-bin=1
default-authentication-plugin=mysql_native_password
performance-schema = 0
datadir = /var/lib/mysql
socket = /var/lib/mysql/mysql.sock
#symbolic-links = 0
log-error = /var/log/mysqld.log
pid-file = /var/run/mysqld/mysqld.pid
#innodb_buffer_pool_size = 2122683392
max_allowed_packet = 26843545611
open_files_limit=80000
innodb_file_per_table = 1
#slow_query_log = 1
#join_buffer_size = 99M
#sort_buffer_size = 112M
bind-address=127.0.0.1
event_scheduler=DISABLED
innodb_table_locks=1
key_buffer_size = 1G
innodb_buffer_pool_size = 1G

如何修改我的 mysql 数据库以获得最佳性能。

Mysqltuner 提供以下结果:

 >>  MySQLTuner 1.7.20 - Major Hayden <major@mhtx.net>
 >>  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
[OK] Currently running supported MySQL version 8.0.23
[OK] Operating on 64-bit architecture

-------- Log file Recommendations ------------------------------------------------------------------
[OK] Log file /var/log/mysqld.log exists
[--] Log file: /var/log/mysqld.log(619K)
[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 456 warning(s).
[!!] /var/log/mysqld.log contains 233 error(s).
[--] 130 start(s) detected in /var/log/mysqld.log
[--] 1) 2021-02-12T06:16:31.704591Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.23'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server - GPL.
[--] 2) 2021-02-12T06:16:31.505171Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
[--] 3) 2021-02-12T06:09:04.818266Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.23'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server - GPL.
[--] 4) 2021-02-12T06:09:04.648814Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
[--] 5) 2021-02-12T04:37:19.807846Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.23'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server - GPL.
[--] 6) 2021-02-12T04:37:19.591494Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
[--] 7) 2021-02-10T07:30:11.946321Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.23'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server - GPL.
[--] 8) 2021-02-10T07:30:11.499995Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
[--] 9) 2021-02-09T15:57:42.436141Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.23'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server - GPL.
[--] 10) 2021-02-09T15:57:42.241010Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
[--] 100 shutdown(s) detected in /var/log/mysqld.log
[--] 1) 2021-02-12T06:16:24.530235Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.23)  MySQL Community Server - GPL.
[--] 2) 2021-02-12T04:37:09.697327Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.23)  MySQL Community Server - GPL.
[--] 3) 2021-02-10T07:28:56.162707Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.23)  MySQL Community Server - GPL.
[--] 4) 2021-02-09T15:57:37.130047Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.23)  MySQL Community Server - GPL.
[--] 5) 2021-02-04T15:59:02.976921Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.23)  MySQL Community Server - GPL.
[--] 6) 2021-01-31T17:40:30.009548Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.23)  MySQL Community Server - GPL.
[--] 7) 2021-01-31T13:34:30.775996Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.23)  MySQL Community Server - GPL.
[--] 8) 2021-01-31T10:26:41.335569Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.23)  MySQL Community Server - GPL.
[--] 9) 2021-01-31T09:53:50.614458Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.23)  MySQL Community Server - GPL.
[--] 10) 2021-01-31T09:46:05.146097Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.23)  MySQL Community Server - GPL.

-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MEMORY +MRG_MYISAM +MyISAM +PERFORMANCE_SCHEMA
[--] Data in MyISAM tables: 110.9K (Tables: 10)
[--] Data in InnoDB tables: 1.7G (Tables: 402)
[OK] Total fragmented tables: 0

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

-------- Security Recommendations ------------------------------------------------------------------
[--] Skipped due to unsupported feature for MySQL 8

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

-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 4h 18m 44s (15M q [1K qps], 30K conn, TX: 20G, RX: 8G)
[--] Reads / Writes: 99% / 1%
[--] Binary logging is disabled
[--] Physical Memory     : 15.5G
[--] Max MySQL memory    : 153.2G
[--] Other process memory: 0B
[--] Total buffers: 2.0G global + 1.0G per thread (151 max threads)
[--] P_S Max memory usage: 0B
[--] Galera GCache Max memory usage: 0B
[!!] Maximum reached memory usage: 154.2G (994.61% of installed RAM)
[!!] Maximum possible memory usage: 153.2G (988.16% of installed RAM)
[!!] Overall possible memory usage with other process exceeded memory
[OK] Slow queries: 0% (0/15M)
[!!] Highest connection usage: 100%  (152/151)
[!!] Aborted connections: 7.13%  (2205/30935)
[!!] name resolution is active : a reverse name resolution is made for each new connection and can reduce performance
[--] Query cache have been removed in MySQL 8
[OK] Sorts requiring temporary tables: 0% (28K temp sorts / 9M sorts)
[!!] Joins performed without indexes: 81575
[OK] Temporary tables created on disk: 0% (0 on disk / 2M total)
[OK] Thread cache hit rate: 94% (1K created / 30K connections)
[OK] Table cache hit rate: 51% (4K open / 7K opened)
[OK] table_definition_cache(2000) is upper than number of tables(736)
[OK] Open file limit used: 0% (0/40K)
[OK] Table locks acquired immediately: 100% (4 immediate / 4 locks)

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

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

-------- MyISAM Metrics ----------------------------------------------------------------------------
[--] MyISAM Metrics are disabled on last MySQL versions.

-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[--] InnoDB Thread Concurrency: 0
[OK] InnoDB File per table is activated
[!!] InnoDB buffer pool / data size: 1.0G/1.7G
[!!] Ratio InnoDB log file size / InnoDB Buffer pool size (9.375 %): 48.0M * 2/1.0G should be equal to 25%
[!!] InnoDB buffer pool <= 1G and Innodb_buffer_pool_instances(!=1).
[--] Number of InnoDB Buffer Pool Chunk : 8 for 8 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% (25775403246 hits/ 25775449742 total)
[OK] InnoDB Write log efficiency: 95.62% (3779883 hits/ 3953044 total)
[OK] InnoDB log waits: 0.00% (0 waits / 173161 writes)

-------- Aria Metrics ------------------------------------------------------------------------------
[--] Aria Storage Engine not available.

-------- 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.
    Reduce or eliminate persistent connections to reduce connection usage
    Reduce or eliminate unclosed connections and network issues
    Configure your accounts with ip or subnets only, then update your configuration with skip-name-resolve=1
    We will suggest raising the 'join_buffer_size' until JOINs not using indexes are found.
             See https://dev.mysql.com/doc/internals/en/join-buffer-size.html
             (specially the conclusions at the bottom of the page).
    Performance schema should be activated for better diagnostics
    Before changing innodb_log_file_size and/or innodb_log_files_in_group read this: 
Variables to adjust:
  *** MySQL's maximum memory usage is dangerously high ***
  *** Add RAM before increasing MySQL buffer variables ***
    max_connections (> 151)
    wait_timeout (< 28800)
    interactive_timeout (< 28800)
    join_buffer_size (> 256.0K, or always use indexes with JOINs)
    performance_schema = ON enable PFS
    innodb_buffer_pool_size (>= 1.7G) if possible.
    innodb_log_file_size should be (=128M) if possible, so InnoDB total log files size equals to 25% of buffer pool size.
    innodb_buffer_pool_instances (=1)

这是我在 stackoverflow 中的第一篇文章,希望社区成员可以帮助我。

先感谢您。

4

1 回答 1

0

每秒速率 = RPS

为您的 my.cnf [mysqld] 部分考虑的建议

innodb_buffer_pool_size= 4G  # from 1G to use RAM for ALL innodb table storage for your 1.7G of data and indexes with room to grow. 
innodb_buffer_pool_instances=4  # from 1 to reduce mutex contention
innodb_lru_scan_depth=100  # from 1024 to conserve 90% of CPY cycles used for function EVERY SECOND
thread_cache_size=300  # from 151 to support max_used_connections greater than 151 limit
max_connections=300  ; from 151 to accomodate more concurrent connections
connect_timeout=30  # from 10 seconds limit to reduce aborted_connections count
table_open_cache=8000  # from 4000 to reduce tables_opened thrashing 
innodb_open_files=8000  # to ALWAYS match to table_open_cache - BEST PRACTICE
table_open_cache_instances=16  # to reduce mutex contention with tables_opened frequency
wait_timeout=7200  # from 28800 for 2 hours of inactivity before timeout
interactive_timeout=7200  # from 28800 for 2 hours of MySQL Command Prompt inactivity before timeout

您的 MySQLTuner 报告显示在 4h 18m 内“无索引执行的连接”为 81,575。我们常见问题页面上的提示将帮助您找到需要索引的表/列,通过减少每天的 select_scan 频率来提高性能。

您会发现这些更改将有助于适应您的高使用率并减少 CPU 繁忙百分比。有关其他提示,请查看我们的个人资料、联系信息的网络个人资料和免费下载的实用程序脚本,以帮助您进行所需的性能调整。

于 2021-02-19T11:30:56.560 回答