如何优化我的 MySQL 安装?

如何优化我的 MySQL 安装?

(Debian 7 32 位 KVM,配备 512mb RAM - SSD 存储 - 1 个 CPU)

该服务器不是mysql专用的。

它上面有一个小型论坛。(Mysql -> 主要为读取)

有人能给我一些关于如何优化 MySQL 安装的建议吗?

服务器在高峰时段没有延迟。

谢谢

我的 my.cnf 文件中的当前配置是:

[mysqld]
#skip-locking
#skip-innodb
#skip-name-resolve
#skip-networking
#skip-bdb
local-infile=0

# Logs
log_error=/var/log/mysql/mysql_error.log
#log_slow_queries=/var/log/mysql/mysql_slow.log
#long_query_time=10
#log-queries-not-using-indexes

# Buffers
key_buffer_size=16M
sort_buffer_size=1M
join_buffer_size=1M
max_join_size=2M

#Threads
thread_concurrency=2
thread_cache_size=1

#Table_cache
table_cache=1024

#Temp tables
max_heap_table_size=128M
tmp_table_size=128M

#Query_cache
query_cache_limit=1M
query_cache_size=16M

#Timeouts
interactive_timeout=90
wait_timeout=90

#Connections
max_connections=50

#[innodb]
innodb_buffer_pool_size=128M
innodb_file_per_table=1
innodb_log_file_size=5M
innodb_thread_concurrency=1

当我运行 MySQLTuner 时,我得到的结果如下:

>>  MySQLTuner 1.3.0 - Major Hayden <[email protected]>
>>  Bug reports, feature requests, and downloads at http://mysqltuner.com/
>>  Run with '--help' for additional options and output filtering
[OK] Logged in using credentials from debian maintenance account.
[OK] Currently running supported MySQL version 5.5.36-34.2-648.wheezy
[OK] Operating on 32-bit architecture with less than 2GB RAM

-------- Storage Engine Statistics -------------------------------------------
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MRG_MYISAM
[--] Data in MyISAM tables: 5M (Tables: 64)
[--] Data in InnoDB tables: 82M (Tables: 195)
[--] Data in PERFORMANCE_SCHEMA tables: 0B (Tables: 17)
[--] Data in MEMORY tables: 252K (Tables: 3)
[!!] Total fragmented tables: 26

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

-------- Performance Metrics -------------------------------------------------
[--] Up for: 8d 4h 46m 25s (2M q [3.459 qps], 181K conn, TX: 7B, RX: 521M)
[--] Reads / Writes: 54% / 46%
[--] Total buffers: 304.0M global + 2.6M per thread (50 max threads)
[!!] Maximum possible memory usage: 432.1M (86% of installed RAM)
[OK] Slow queries: 0% (0/2M)
[OK] Highest usage of available connections: 62% (31/50)
[OK] Key buffer size / total MyISAM indexes: 16.0M/5.1M
[OK] Key buffer hit rate: 99.8% (1M cached / 2K reads)
[OK] Query cache efficiency: 74.4% (1M cached / 1M selects)
[!!] Query cache prunes per day: 3959
[OK] Sorts requiring temporary tables: 0% (21 temp sorts / 81K sorts)
[!!] Joins performed without indexes: 30762
[!!] Temporary tables created on disk: 46% (74K on disk / 160K total)
[OK] Thread cache hit rate: 86% (25K created / 181K connections)
[!!] Table cache hit rate: 10% (347 open / 3K opened)
[OK] Open file limit used: 8% (178/2K)
[OK] Table locks acquired immediately: 99% (1M immediate / 1M locks)
[OK] InnoDB buffer pool / data size: 128.0M/82.8M
[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
    Enable the slow query log to troubleshoot bad queries
    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
    Increase table_cache gradually to avoid file descriptor limits
    Read this before increasing table_cache over 64: http://bit.ly/1mi7c4C
Variables to adjust:
    query_cache_size (> 16M)
    join_buffer_size (> 1.0M, or always use indexes with joins)
    tmp_table_size (> 128M)
    max_heap_table_size (> 128M)
    table_cache (> 1024)

当我运行 TUNING PRIMER 时,我得到了以下结果:

 -- MYSQL PERFORMANCE TUNING PRIMER --
             - By: Matthew Montgomery -

MySQL Version 5.5.36-34.2-648.wheezy i686

Uptime = 8 days 4 hrs 47 min 29 sec
Avg. qps = 3
Total Questions = 2450393
Threads Connected = 1

Server has been running for over 48hrs.
It should be safe to follow these recommendations

To find out more information on how each of these
runtime variables effects performance visit:
http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html
Visit http://www.mysql.com/products/enterprise/advisors.html
for info about MySQL's Enterprise Monitoring and Advisory Service

SLOW QUERIES
The slow query log is NOT enabled.
Current long_query_time = 10.000000 sec.
You have 0 out of 2450414 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/5.5/en/point-in-time-recovery.html

WORKER THREADS
Current thread_cache_size = 1
Current threads_cached = 0
Current threads_per_sec = 0
Historic threads_per_sec = 0
Your thread_cache_size is fine

MAX CONNECTIONS
Current max_connections = 50
Current threads_connected = 1
Historic max_used_connections = 31
The number of used connections is 62% of the configured maximum.
Your max_connections variable seems to be fine.

INNODB STATUS
Current InnoDB index space = 25 M
Current InnoDB data space = 82 M
Current InnoDB buffer pool free = 13 %
Current innodb_buffer_pool_size = 128 M
Depending on how much space your innodb indexes take up it may be safe
to increase this value to up to 2 / 3 of total system memory

MEMORY USAGE
Max Memory Ever Allocated : 255 M
Configured Max Per-thread Buffers : 128 M
Configured Max Global Buffers : 176 M
Configured Max Memory Limit : 304 M
Physical Memory : 502 M
Max memory limit seem to be within acceptable norms

KEY BUFFER
Current MyISAM index space = 5 M
Current key_buffer_size = 16 M
Key cache miss rate is 1 : 513
Key buffer free ratio = 67 %
Your key_buffer_size seems to be fine

QUERY CACHE
Query cache is enabled
Current query_cache_size = 16 M
Current query_cache_used = 9 M
Current query_cache_limit = 1 M
Current Query cache Memory fill ratio = 60.50 %
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 = 1 M
Current read_rnd_buffer_size = 256 K
Sort buffer seems to be fine

JOINS
Current join_buffer_size = 1.00 M
You have had 30765 queries where a join could not use an index properly
You should enable "log-queries-not-using-indexes"
Then look for non indexed joins in the slow query log.
If you are unable to optimize your queries you may want to increase your
join_buffer_size to accommodate larger joins in one pass.

Note! This script will still suggest raising the join_buffer_size when
ANY joins not using indexes are found.

OPEN FILES LIMIT
Current open_files_limit = 2109 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 = 1024 tables
Current table_definition_cache = 400 tables
You have a total of 303 tables
You have 348 open tables.
The table_cache value seems to be fine

TEMP TABLES
Current max_heap_table_size = 128 M
Current tmp_table_size = 128 M
Of 86516 temp tables, 46% 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 = 128 K
Current table scan ratio = 489 : 1
read_buffer_size seems to be fine

TABLE LOCKING
Current Lock Wait ratio = 1 : 178950
Your table locking seems to be fine

答案1

几点注意事项:

  • 你注意到数据库在高峰期没有出现问题。为什么要修复没有损坏的东西?
  • 性能调优从您拥有的模式/数据库开始。在开始调优之前,数据类型、约束、索引都会对您的性能产​​生影响。
  • 你的读/写百分比是多少?
  • 您运行的某些性能检查可能会显示偏差数据..例如磁盘上的临时表可能是由于您的模式具有 blob 数据类型等。
  • 硬件是另一回事。你说你使用 ssd 进行存储。对于你的规模/负载的数据库服务器,这会让任何配置调整都变得毫无意义。数据库喜欢内存 :) 尤其是像 innodb 这样的引擎。
  • 将存储引擎与您在​​数据库中存储的数据类型相匹配。
  • 根据系统内存调整缓冲区。例如:您最多有 50 个连接,每个连接获得 2.6m,那么您的服务器上已经有 +100m,这已经是总 RAM 的 25%。
  • 关闭未使用的引擎和功能。
  • 查询缓存可能非常好,也可能非常糟糕。如果您有很多唯一查询,您不妨将其关闭。修剪量也可能表明缓存太小。不要将其设置得太大,因为这同样容易造成开销。
  • 使用慢速查询日志查看哪些查询可能会给您的服务器带来麻烦并分析这些查询。
  • 阅读那些真正懂行的人的文章;)例如这里
  • 如果您只有一个地方可以“测试”,请务必小心!

你问的问题对于其他人来说确实很难回答。你运行的脚本是一个很好的开始。使用几个不同的脚本来测试你得到的数据是否符合你的预期并且相互一致。

我现在的建议是确保不会导致服务器其余部分不稳定,因为服务器当前可以使用机器中的大部分内存。当然,512m 并不是很多,但仍然...所以从降低全局缓冲区开始,而不会损失性能。

来自 mysqltuner 脚本的是磁盘上的 tmp 表。这些通常是较大环境中的性能杀手。

看到您正在使用 SSD,我认为最小配置和超级调整的 cnf 之间的差异不会产生明显的区别。当然,如果没有任何设计不良的模式/数据库的话。

当你认为性能很好时,就停止性能调优。这是一项永远无法完成的任务。这是一个硬核课题,可能性是无穷无尽的。你可能会浪费数小时来调优数据库的一小部分。而人类永远不会注意到你这个规模的环境中的区别。

我绝不是专业人士,所以我可能会错。但我去过那里,只是想分享我的经历。我希望它们能有所帮助

相关内容