【问题标题】:Reduce MySQL RAM usage减少 MySQL RAM 使用量
【发布时间】:2021-09-03 17:05:51
【问题描述】:

我目前在我的 16gb 服务器上为我的大约 15 个网站(主要是 WordPress 和 PHP 框架)使用以下配置:

key_buffer_size                = 256M
max_allowed_packet             = 256M
thread_stack                   = 256K
thread_cache_size              = 256K
table_open_cache               = 10000
table_definition_cache         = 8192

innodb_file_per_table          = 1
innodb_flush_log_at_trx_commit = 2
innodb_flush_method            = O_DIRECT
innodb_buffer_pool_size        = 10G
innodb_buffer_pool_instances   = 10
innodb_log_file_size           = 1G
innodb_read_io_threads         = 8
innodb_write_io_threads        = 8
innodb_io_capacity             = 2000

tmp_table_size                 = 256M
max_heap_table_size            = 256M

sort_buffer_size               = 4M
read_buffer_size               = 256K
join_buffer_size               = 256K
read_rnd_buffer_size           = 256K

query_cache_size               = 0
query_cache_type               = 0
query_cache_limit              = 128M

myisam-recover-options         = BACKUP
max_connections                = 500

几个月来一切都很顺利,但我发现优化和内存使用之间的完美平衡(有些网站真的是 SQL 密集型的)。但是几天前我看到 MySQL 被杀死了,因为 ram 使用量超过了可用 RAM(如果我记得的话,在 16 上超过 10 GB)。

所以我的问题是,当这种情况发生时,您应该首先在 MySQL 配置中减少什么?我在所有 *_buffer_size 和 inno_pool_size 之间犹豫不决,因此为了在更短的时间内释放更多 RAM,我将这些变量减少为:

innodb_buffer_pool_size        = 8G
innodb_buffer_pool_instances   = 8

目前一切都很好,我现在是 9gb/16gb,但我不知道这是否可行。

谢谢。

编辑:添加来自网站的示例查询

"SELECT c.cover, c.type, cl.slug, cl.label, GROUP_CONCAT(p.id) AS links
 FROM card c
 INNER JOIN card_language cl ON cl.card_id = c.id AND cl.language_id = " . $language->id . "
 INNER JOIN card_has_card_category chcc ON chcc.card_id = c.id
 INNER JOIN card_category_language ccl ON ccl.card_category_id = chcc.card_category_id AND ccl.language_id = " . $language->id . "
 LEFT JOIN card_has_links cp ON cp.card_id = c.id 
 LEFT JOIN link p ON p.id = cp.link_id
 WHERE c.type = '" . $type . "' 
 AND ccl.slug = '" . $slug . "'
 GROUP BY cl.slug
 ORDER BY c.id DESC
 LIMIT " . $limit . "
 OFFSET " . $offset .  "
 ;"

【问题讨论】:

    标签: mysql optimization ram


    【解决方案1】:

    一般情况下,最好使用 MySQL 提供的默认值。

    我确实看到您可能将某些设置增加到危险的数量。请记住,如果 MySQL 使用了太多需要交换的 RAM,性能可能会一落千丈。

    key_buffer_size                = 256M  --> 20M (Assuming no tables are MyISAM)
    max_allowed_packet             = 256M  -- no more than 1% of RAM
    thread_stack                   = 256K  -- remove (to get default)
    thread_cache_size              = 256K  -- This is NOT bytes!!; change to 20
    table_open_cache               = 10000 -- Do you really have thousands of tables?
    table_definition_cache         = 8192  -- Do you really have thousands of tables?
    
    innodb_file_per_table          = 1
    innodb_flush_log_at_trx_commit = 2
    innodb_flush_method            = O_DIRECT
    innodb_buffer_pool_size        = 10G  -- the main tunable; 10G is good
    innodb_buffer_pool_instances   = 10
    innodb_log_file_size           = 1G
    innodb_read_io_threads         = 8
    innodb_write_io_threads        = 8
    innodb_io_capacity             = 2000  -- Good, IF you have SSD, not HDD
    
    tmp_table_size                 = 256M  -- no more than 1% of RAM
    max_heap_table_size            = 256M  -- no more than 1% of RAM
    
    sort_buffer_size               = 4M
    read_buffer_size               = 256K
    join_buffer_size               = 256K
    read_rnd_buffer_size           = 256K
    
    query_cache_size               = 0
    query_cache_type               = 0
    query_cache_limit              = 128M
    
    myisam-recover-options         = BACKUP
    max_connections                = 500  -- see below
    

    SHOW GLOBAL STATUS LIKE 'Max_used_connections';max_connections 提供“高水位标记”。您可能不需要比“使用”的数字大得多的 `max_connections。

    为了加速 WP,因此整个系统,O.Jones 和我制作了一个 WordPress 插件来改进某些 INDEXes: https://wordpress.org/plugins/index-wp-mysql-for-speed 我们检测各种东西,比如 InnoDB 存储的 Barrucuda 版本的存在引擎和其他 MySQL 奥秘,并做正确的事。

    【讨论】:

    • 您好!谢谢回复。我在每个 WP 上都安装了你的插件,似乎其中一些还在使用 MyISAM!之后我将一些变量更新为:``` key_buffer_size = 32M max_allowed_pa​​cket = 160M table_open_cache = 2048 table_definition_cache = 2048 tmp_table_size = 160M max_heap_table_size = 160M max_connections = 200 ``` 并删除了thread_stack和thread_cache_size。我认为现在应该更好,因为一些变量被划分了。
    • @DK01 - 很高兴听到事情变得更好。请注意,应根据可用 RAM 的大小设置一些变量;一些基于其他事物。此外,当从 MyISAM 切换到 InnoDB 时,大幅缩小 key_buffer_size 并将 innodb_buffer_pool_size 增加到可用 RAM 的 70% 左右。
    • 您好,服务器运行几天没有任何问题,但我有点担心 MySQL 进程每天都在吃更多的内存。我现在的内存是它使用的 45.7%,所以我是 10.8G/15.6G。如果它稳定,那么我想这是完美的平衡,否则如果它继续增长,我将不得不减少 buffer_pool_size?
    • 你有多少数据?可能不到10GB。也就是说,buffer_pool 可能还不是一个完整的大小。因此它可能会继续增长。
    • 我有 16 个数据库,但真正使用的只有 10 个。其中一个数据库为 830mb(相当大),总大小约为 1,37gb。所以我不知道它是否仍然“正常”,但此时我的内存为 11.7gb/15.6gb,mysql 正在使用它的 52.4%
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2020-04-07
    • 2011-08-13
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    相关资源
    最近更新 更多