1

我按照互联网上的一些提示调整了 mysql。

但是网站加载速度很慢,而且第一个字节的时间非常长。因此我开始调查,据我所知,这是由mysql和高内存使用引起的

每次我根据我从 ./mysqltuner.pl./tuning-primer.sh获得的建议对 my.cnf 进行更改时, 它都会给出另一个建议。实际上,某些值应该相互平衡。我希望有人知道如何获得 mysql 的高性能使用,同时照顾服务器的健康

MariaDB 服务器版本:10.0.20-MariaDB-log MariaDB 服务器 服务器信息:Intel Core i7-3770 2x HDD 3,0 TB SATA 4x RAM 8192 MB DDR3 Cloudlinux + Cpanel 已安装 Apache/2.4.16 + eAccelerator + mod_pagespeed

SLOW QUERIES
Error: slow_query_log_file=/var/log/mysql/log-slow-queries.log
Current long_query_time = 10.000000 sec.
You have 1944 out of 25401054 that take longer than 10.000000 sec. to complete
**Your long_query_time seems to be fine**

BINARY UPDATE LOG
The binary update log is NOT enabled.
**You will not be able to do point in time recovery**
See http://dev.mysql.com/doc/refman/10.0/en/point-in-time-recovery.html

WORKER THREADS
Current thread_cache_size = 8
Current threads_cached = 6
Current threads_per_sec = 0
Historic threads_per_sec = 0
**Your thread_cache_size is fine**

MAX CONNECTIONS
Current max_connections = 151
Current threads_connected = 6
Historic max_used_connections = 43
The number of used connections is 28% of the configured maximum.
**Your max_connections variable seems to be fine.**

No InnoDB Support Enabled!

MEMORY USAGE
Max Memory Ever Allocated : 20.96 G
Configured Max Per-thread Buffers : 24.81 G
Configured Max Global Buffers : 13.89 G
Configured Max Memory Limit : 38.70 G
Physical Memory : 31.12 G

**Max memory limit exceeds 90% of physical memory**

KEY BUFFER
Current MyISAM index space = 29 M
Current key_buffer_size = 384 M
Key cache miss rate is 1 : 870
Key buffer free ratio = 78 %
**Your key_buffer_size seems to be fine**

QUERY CACHE
Query cache is enabled
Current query_cache_size = 512 M
Current query_cache_used = 222 M
Current query_cache_limit = 1.00 G
Current Query cache Memory fill ratio = 43.41 %
Current query_cache_min_res_unit = 4 K
**MySQL won't cache query results that are larger than query_cache_limit in size**

SORT OPERATIONS
Current sort_buffer_size = 16 M
Current read_rnd_buffer_size = 8 M
**Sort buffer seems to be fine**

JOINS
./tuning-primer.sh: line 402: export: `2097152': not a valid identifier
Current join_buffer_size = 128.00 M
You have had 10199 queries where a join could not use an index properly
join_buffer_size >= 4 M
**This is not advised
You should enable "log-queries-not-using-indexes"
Then look for non indexed joins in the slow query log.**

OPEN FILES LIMIT
Current open_files_limit = 16162 files
The open_files_limit should typically be set to at least 2x-3x
that of table_cache if you have heavy MyISAM usage.
**Your open_files_limit value seems to be fine**

TABLE CACHE
Current table_open_cache = 8000 tables
Current table_definition_cache = 8000 tables
You have a total of 7347 tables
You have 8000 open tables.
**Current table_cache hit rate is 21%
, while 100% of your table cache is in use
You should probably increase your table_cache**

TEMP TABLES
Current max_heap_table_size = 16 M
Current tmp_table_size = 16 M
Of 592173 temp tables, 36% were created on disk
**Perhaps you should increase your tmp_table_size and/or max_heap_table_size
to reduce the number of disk-based temporary tables
Note! BLOB and TEXT columns are not allow in memory tables.
If you are using these columns raising these values might not impact your
ratio of on disk temp tables.**

TABLE SCANS
Current read_buffer_size = 16 M
Current table scan ratio = 74 : 1
**read_buffer_size is over 8 MB there is probably no need for such a large read_buffer**

TABLE LOCKING
Current Lock Wait ratio = 1 : 4366
**You may benefit from selective use of InnoDB.
If you have long running SELECT's against MyISAM tables and perform
frequent updates consider setting 'low_priority_updates=1'
If you have a high concurrency of inserts on Dynamic row-length tables
consider setting 'concurrent_insert=ALWAYS'.**

root@my [~]# ./mysqltuner.pl

 >>  MySQLTuner 1.4.0 - Major Hayden <major@mhtx.net>
 >>  Bug reports, feature requests, and downloads at http://mysqltuner.com/
 >>  Run with '--help' for additional options and output filtering
[!!] Currently running unsupported MySQL version 10.0.20-MariaDB-log
[OK] Operating on 64-bit architecture

-------- Storage Engine Statistics -------------------------------------------
[--] Status: +ARCHIVE +Aria +BLACKHOLE +CSV +FEDERATED +InnoDB +MRG_MyISAM
[--] Data in MyISAM tables: 82M (Tables: 925)
[--] Data in InnoDB tables: 7G (Tables: 6334)
[--] Data in PERFORMANCE_SCHEMA tables: 0B (Tables: 52)
[--] Data in MEMORY tables: 0B (Tables: 2)
[!!] Total fragmented tables: 159

-------- Security Recommendations  -------------------------------------------
[OK] All database users have passwords assigned

-------- Performance Metrics -------------------------------------------------
[--] Up for: 14h 42m 18s (25M q [480.374 qps], 81K conn, TX: 71B, RX: 6B)
[--] Reads / Writes: 98% / 2%
[--] Total buffers: 13.9G global + 168.3M per thread (151 max threads)
**[!!] Maximum possible memory usage: 38.7G (124% of installed RAM)**
[OK] Slow queries: 0% (1K/25M)
[OK] Highest usage of available connections: 28% (43/151)
[OK] Key buffer size / total MyISAM indexes: 384.0M/29.8M
[OK] Key buffer hit rate: 99.9% (12M cached / 14K reads)
[OK] Query cache efficiency: 44.9% (20M cached / 44M selects)
**[!!] Query cache prunes per day: 2013573**
[OK] Sorts requiring temporary tables: 0% (1K temp sorts / 1M sorts)
**[!!] Joins performed without indexes: 10207
[!!] Temporary tables created on disk: 58% (345K on disk / 592K total)**
[OK] Thread cache hit rate: 98% (1K created / 81K connections)
[OK] Table cache hit rate: 21% (8K open / 38K opened)
[OK] Open file limit used: 12% (1K/16K)
[OK] Table locks acquired immediately: 99% (10M immediate / 10M locks)
[OK] InnoDB buffer pool / data size: 10.0G/7.7G
[OK] InnoDB log waits: 0
-------- Recommendations -----------------------------------------------------
General recommendations:
    **Run OPTIMIZE TABLE to defragment tables for better performance
    Reduce your overall MySQL memory footprint for system stability
    Increasing the query_cache size over 128M may reduce 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 without LIMIT clauses**
Variables to adjust:
  *** MySQL's maximum memory usage is dangerously high ***
  *** Add RAM before increasing MySQL buffer variables ***
    query_cache_size (> 512M) [see warning above]
    join_buffer_size (> 128.0M, or always use indexes with joins)
    tmp_table_size (> 16M)
    max_heap_table_size (> 16M)

这是我设置的 my.cnf 设置

[mysqld]
#http://blog.secaserver.com/2011/08/mysql-recommended-my-cnf-settings-innodb-engine/
# GENERAL #
default-storage-engine=InnoDB
tmpdir=/tmp_mysql
group_concat_max_len=10000000
local-infile=1

# LOGGING #
slow_query_log = 1
slow_query_log_file=/var/log/mysql/log-slow-queries.log
long_query_time = 10
log-error = /var/log/error.log
log-queries-not-using-indexes

# CACHES AND LIMITS AND SAFETY #
max_allowed_packet = 512M #16
query_cache_size = 512M
query_cache_limit = 1024M
thread_cache_size = 8
table_definition_cache = 8000
table_open_cache = 8000
sort_buffer_size = 16M
read_buffer_size = 16M   #2
read_rnd_buffer_size = 8M
join_buffer_size = 128M
thread_concurrency = 0 # Try number of CPU's*2 for thread_concurrency
key_buffer = 256M


# INNODB #
innodb_file_per_table=1
innodb_file_format = Barracuda
innodb_sort_buffer_size = 128M
innodb_data_home_dir = /var/lib/mysql
innodb_log_group_home_dir = /var/lib/mysql
innodb_thread_concurrency=0
innodb_flush_method=O_DIRECT 
innodb_lock_wait_timeout = 120
innodb_buffer_pool_size=10G
innodb_log_file_size = 1536M   # Set .. innodb_log_file_size to 25 % of innodb_buffer_pool_size -You can set .. innodb_buffer_pool_size up to 50 - 80 %
innodb_log_buffer_size = 3072M
innodb_additional_mem_pool_size = 20M
#innodb_read_io_threads=16
#innodb_write_io_threads=16
#innodb_io_capacity=500
#innodb_flush_log_at_trx_commit=1
#sync_binlog=1
#innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend


# MyISAM #
key_buffer_size = 384M
myisam_sort_buffer_size = 64M


[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
4

2 回答 2

1

概括

  • 不用担心内存“问题”
  • 看看慢日志 - 你有一些严重的优化要处理
  • 为什么有数千张桌子?这可能是一个设计缺陷。
  • 忽略关于碎片的评论。
  • 将其余表转换为 InnoDB。

细节

您的 long_query_time 似乎很好

不,long_query_time将速度调低到 2 并密切关注缓慢的查询。

没有启用 InnoDB 支持!
[--] InnoDB 表中的数据:7G(表:6334)

这些说法相互矛盾!InnoDB 是首选引擎。其他输出似乎说 InnoDB 正在使用中。(脚本中的错误?)

最大内存限制超过物理内存的 90%
[!!] 最大可能的内存使用量:38.7G(已安装 RAM 的 124%)

没有没有缺陷的论坛。你可能没问题。

您的 key_buffer_size 似乎很好

key_buffer应该是 MyISAM_only 服务器中最大的内存使用量。但是你有一个很小的数据库,所以没问题。innodb_buffer_pool_size应该是使用 InnoDB 时最大的内存用户。您当前的值很好:

innodb_buffer_pool_size = 10G
key_buffer_size = 384M

当前 query_cache_size = 512 M
[OK] 查询缓存效率:44.9%(20M 缓存 / 44M 选择)
[!!] 每天查询缓存修剪次数:2013573

512M 会影响性能;将其限制为 50M。另外两行说 QC 没那么有用,并且有很多修剪开销。
结论:谨慎尝试

query_cache_size = 0
query_cache_type = OFF

./tuning-primer.sh:第 402 行:导出:`2097152':不是有效的标识符

脚本中的错误?

您应该启用“log-queries-not-using-indexes”

不,它只会使慢日志变得混乱。没有索引的单行表,甚至没有索引的 100 行表,都可能“足够快”。
相反,请查看现有的慢查询来决定应该做什么。

您总共有 7347 个表
(表:6334)

这是您的数据库中的设计缺陷。

在 592173 个临时表中,36% 是在磁盘上创建的

同样,慢日志可以帮助识别最差的查询。然后我们可以通过添加索引或重新编写查询来修复它们。

[!!] 碎片表总数:159
运行 OPTIMIZE TABLE 对表进行碎片整理

忽略——几乎所有的表几乎总是碎片化的。这里没有行动项目。

[480.374 qps]

去 InnoDB 的一个很好的理由。

[!!] 在没有索引的情况下执行的连接:10207
[!!] 在磁盘上创建的临时表:58%(磁盘上 345K / 总共 592K)**

必须查看慢日志条目。让我们从你已经抓到的 10 秒开始。

#innodb_flush_log_at_trx_commit=1

建议设置为 2。

(这是我第一次看到tuning-primer.sh;我没有留下深刻的印象。)

这两个博客可能有用:

于 2015-08-09T16:58:28.950 回答
0

它似乎更有可能是一个不完整的索引问题。

  1. 验证慢查询的 WHERE 子句中的所有字段是否被正确索引

  2. 如果您有 Nfields-in-one 索引,请尝试分别展开它们(如果它们成为单独条件的主题),因为只有第一个索引会被真正索引,或者所有 N 一起一个 WHERE 子句

  3. 避免冗余索引(枚举类案例或高级位)

  4. db 结构也可能需要一点点接触 - 但这实际上取决于您的 db 配置文件,当然还有数据更新和查看的方式(您可以在每种情况下使用混合类型的引擎进行改进 - 但您必须知道如何调整 my.cnf适当地)

于 2015-12-18T15:22:42.417 回答