MySQL 优化 - 需要建议

标签 mysql optimization

我有一台 CentOS 7 虚拟机,配备 32 GB RAM(4 核/8 线程)3.4 GHz+ 我运行了 MySQLTuner 脚本,并按照他们给出的建议进行操作,但它仍然很慢

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

[mysqld]
local-infile=0
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

symbolic-links=0

key_buffer        = 32M
max_allowed_packet    = 268435456
thread_stack        = 192K
thread_cache_size       = 384
key_buffer_size=32G
max_connections = 1200
max_user_connections=1000
table_open_cache=3000
table_open_cache=5000
table_definition_cache=2048
sort_buffer_size=32M
join_buffer_size = 32M
read_buffer_size=32M
wait_timeout=20
read_rnd_buffer_size=786432
bulk_insert_buffer_size = 8M

myisam_sort_buffer_size=64M
query_cache_size=128M
query_cache_limit=8M
query_cache_type = 1
query_prealloc_size = 262144
query_alloc_block_size = 65535
transaction_alloc_block_size = 8192
transaction_prealloc_size = 4096
max_write_lock_count = 8
tmp_table_size=320M
thread_concurrency=32


innodb_lock_wait_timeout = 600
innodb_additional_mem_pool_size=60M
innodb_buffer_pool_size=20G
innodb_buffer_pool_instances=20
innodb_table_locks=0
max_heap_table_size=128M
net_buffer_length = 16384
max_connect_errors = 10
open_files_limit=50000
default-storage-engine=InnoDB
low-priority-updates=1
innodb_file_per_table=1
concurrent_insert=ALWAYS


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

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d










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


[mysqldump]
quick
quote-names
max_allowed_packet    = 16M



[isamchk]
key_buffer = 384M
sort_buffer = 384M
read_buffer = 256M
write_buffer = 256M

我不久前重新启动了 MySQL 以应用我最近的更改。

当我运行脚本时,这是我得到的:

 >>  MySQLTuner 1.4.4 - Major Hayden <major@mhtx.net>
 >>  Bug reports, feature requests, and downloads at http://mysqltuner.com/
 >>  Run with '--help' for additional options and output filtering
[[0;32mOK[0m] Currently running supported MySQL version 5.5.41-MariaDB
[[0;32mOK[0m] Operating on 64-bit architecture

-------- Storage Engine Statistics -------------------------------------------
[[0;34m--[0m] Status: [0;32m+ARCHIVE [0m[0;32m+Aria [0m[0;32m+BLACKHOLE [0m[0;32m+CSV [0m[0;32m+FEDERATED [0m[0;32m+InnoDB [0m[0;32m+MRG_MYISAM [0m
[[0;34m--[0m] Data in MyISAM tables: 23M (Tables: 75)
[[0;34m--[0m] Data in InnoDB tables: 65M (Tables: 643)
[[0;34m--[0m] Data in PERFORMANCE_SCHEMA tables: 0B (Tables: 17)
[[0;34m--[0m] Data in MEMORY tables: 0B (Tables: 4)
[[0;31m!![0m] Total fragmented tables: 516

-------- Security Recommendations  -------------------------------------------
[[0;32mOK[0m] There is no anonymous account in all database users
[[0;32mOK[0m] All database users have passwords assigned
[[0;31m!![0m] User 'pixoneo@%' hasn't specific host restriction.
[[0;34m--[0m] There is 605 basic passwords in the list.

-------- Performance Metrics -------------------------------------------------
[[0;34m--[0m] Up for: 20m 58s (877K q [697.220 qps], 52K conn, TX: 16B, RX: 195M)
[[0;34m--[0m] Reads / Writes: 96% / 4%
[[0;34m--[0m] Total buffers: 22.3G global + 4.9M per thread (1200 max threads)
[[0;31m!![0m] Maximum possible memory usage: 28.1G (89% of installed RAM)
[[0;32mOK[0m] Slow queries: 0% (9/877K)
[[0;32mOK[0m] Highest usage of available connections: 40% (483/1200)
[[0;32mOK[0m] Key buffer size / total MyISAM indexes: 2.0G/773.0K
[[0;31m!![0m] Key buffer hit rate: 94.3% (53 cached / 3 reads)
[[0;32mOK[0m] Query cache efficiency: 45.6% (497K cached / 1M selects)
[[0;32mOK[0m] Query cache prunes per day: 0
[[0;32mOK[0m] Sorts requiring temporary tables: 0% (0 temp sorts / 22K sorts)
[[0;31m!![0m] Joins performed without indexes: 15817
[[0;32mOK[0m] Temporary tables created on disk: 11% (23K on disk / 204K total)
[[0;32mOK[0m] Thread cache hit rate: 99% (483 created / 52K connections)
[[0;32mOK[0m] Table cache hit rate: 102% (1K open / 1K opened)
[[0;32mOK[0m] Open file limit used: 23% (239/1K)
[[0;32mOK[0m] Table locks acquired immediately: 100% (199K immediate / 199K locks)

-------- InnoDB Metrics -----------------------------------------------------
[[0;34m--[0m] InnoDB is enabled.
[[0;34m--[0m] InnoDB BufferPool Size :20.0G
[[0;34m--[0m] InnoDB BufferPool Inst :1
[[0;32mOK[0m] InnoDB buffer pool / data size: 20.0G/65.4M
[[0;31m!![0m] InnoDB buffer pool instances: 1
[[0;32mOK[0m] InnoDB log waits: 0

-------- Recommendations -----------------------------------------------------
General recommendations:
    Run OPTIMIZE TABLE to defragment tables for better performance
    Restrict Host for user@% to user@SpecificDNSorIp
    MySQL started within last 24 hours - recommendations may be inaccurate
    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
Variables to adjust:
    join_buffer_size (> 1.0M, or always use indexes with joins)
    innodb_buffer_pool_instances(=20)

有人对现在该做什么有什么建议吗?谢谢

最佳答案

key_buffer_size=32G

不!你没有给其他任何东西留下余地。你在使用 MyISAM 吗?如果是这样,请为您的 32GB 服务器将其设置为 4G。如果不是,请将其设置为 20M。

Key buffer size / total MyISAM indexes: 2.0G/773.0K

这是矛盾的!是2G还是32G?执行 SHOW VARIABLES LIKE 'key_buffer_size';。如果显示 2G,则说明您没有向我们显示正在使用的 my.cnf。

Total fragmented tables: 516

忽略;大多数表格大部分时间都是“碎片化”的。这不值得担心。

20 分钟内有 877K 个查询?那是比较忙。如果打开了慢日志,让我们看看其中显示了什么。

483/1200 连接。他们忘记断开连接了吗?还有什么可能导致 Max_used_connections = 483

Thread cache hit rate: 99% (483 created / 52K connections)
table_open_cache=5000

5000 是矫枉过正

无论如何,真正要看“慢”的是 SlowLog:

  • 设置long_query_time=1
  • 开启慢日志
  • 等一天
  • 使用pt-query-digest总结slowlog
  • 向我们展示该摘要中的前几个查询
  • 为这些表和查询包含 SHOW CREATE TABLEEXPLAIN SELECT ...

关于MySQL 优化 - 需要建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31287491/

相关文章:

java - 如何进一步优化这个色差函数?

performance - 使用 OpenGL 加速 2D 图形

MySQL 计算月份之间的差异

python - 我如何使用 python 在 Tkinter treeview 中显示从 Mysql 表获取的数据

php - 将日期和时间转换为日期时间格式时出现问题

mysql - 如何在 VSCode 中断开与 MySQL 服务器的连接?

php - 检索结果时如何解决不明确的列名?

c++ - 将 39567865 个无符号字符加载到 RAM : Valid approach?

c++ - c/c++中endian的最佳和可移植转换

c++ - 并行与线程 - 性能