优化 my.cnf 配置,实现 1 GB 内存,100+ 用户在线



我的mysql不断崩溃。我认为这是因为my.cnf的配置不正确。这是我的 my.cnf 当前配置。请帮助我优化它。我使用的是 1gb 内存和 30gb ssd。我想优化分配的内存和内存。

我通过percona创建了这个配置。

[mysql]
# CLIENT #
port                           = 3306
socket                         = /var/lib/mysql/mysql.sock
[mysqld]
# GENERAL #
user                           = mysql
default-storage-engine         = InnoDB
socket                         = /var/lib/mysql/mysql.sock
pid-file                       = /var/lib/mysql/mysql.pid
performance_schema             = off
# MyISAM #
key-buffer-size                = 32M
myisam-recover                 = FORCE,BACKUP
# SAFETY #
max-allowed-packet             = 16M
max-connect-errors             = 1000000
skip-name-resolve
sql-mode                       = STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ONLY_FULL_GROUP_BY
sysdate-is-now                 = 1
innodb                         = FORCE
# DATA STORAGE #
datadir                        = /var/lib/mysql/
# BINARY LOGGING #
log-bin                        = /var/lib/mysql/mysql-bin
expire-logs-days               = 7
sync-binlog                    = 1
# CACHES AND LIMITS #
tmp-table-size                 = 32M
max-heap-table-size            = 32M
query-cache-type               = 1
query-cache-size               = 32M
max-connections                = 200
thread-cache-size              = 50
open-files-limit               = 65535
table-definition-cache         = 4096
table-open-cache               = 4096
# INNODB #
innodb-flush-method            = O_DIRECT
innodb-log-files-in-group      = 2
innodb-log-file-size           = 64M
innodb-flush-log-at-trx-commit = 1
innodb-file-per-table          = 1
innodb-buffer-pool-size        = 600M
# LOGGING #
log-error                      = /var/lib/mysql/mysql-error.log
log-queries-not-using-indexes  = 1
slow-query-log                 = 1
slow-query-log-file            = /var/lib/mysql/mysql-slow.log

这是mysqltuner的结果

 >>  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
Please enter your MySQL administrative login: root
Please enter your MySQL administrative password:
[OK] Currently running supported MySQL version 5.5.42-log
[OK] Operating on 64-bit architecture
-------- Storage Engine Statistics -------------------------------------------
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MRG_MYISAM
[--] Data in MyISAM tables: 51M (Tables: 3)
[--] Data in InnoDB tables: 653M (Tables: 233)
[--] Data in PERFORMANCE_SCHEMA tables: 0B (Tables: 17)
[--] Data in MEMORY tables: 372K (Tables: 5)
[!!] Total fragmented tables: 34
-------- Security Recommendations  -------------------------------------------
[OK] All database users have passwords assigned
-------- Performance Metrics -------------------------------------------------
[--] Up for: 34m 50s (46K q [22.025 qps], 3K conn, TX: 3B, RX: 18M)
[--] Reads / Writes: 86% / 14%
[--] Total buffers: 672.0M global + 2.8M per thread (500 max threads)
[!!] Maximum possible memory usage: 2.0G (205% of installed RAM)
[OK] Slow queries: 4% (1K/46K)
[OK] Highest usage of available connections: 1% (6/500)
[OK] Key buffer size / total MyISAM indexes: 32.0M/51.3M
[OK] Key buffer hit rate: 98.8% (55K cached / 675 reads)
[!!] Query cache is disabled
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 1K sorts)
[!!] Joins performed without indexes: 8
[OK] Temporary tables created on disk: 22% (103 on disk / 455 total)
[OK] Thread cache hit rate: 99% (6 created / 3K connections)
[OK] Table cache hit rate: 33% (142 open / 420 opened)
[OK] Open file limit used: 0% (56/65K)
[OK] Table locks acquired immediately: 99% (70K immediate / 70K locks)
[!!] InnoDB  buffer pool / data size: 592.0M/653.4M
[OK] InnoDB log waits: 0
-------- Recommendations -----------------------------------------------------
General recommendations:
    Run OPTIMIZE TABLE to defragment tables for better performance
    MySQL started within last 24 hours - recommendations may be inaccurate
    Reduce your overall MySQL memory footprint for system stability
    Adjust your join queries to always utilize indexes
Variables to adjust:
  *** MySQL's maximum memory usage is dangerously high ***
  *** Add RAM before increasing MySQL buffer variables ***
    query_cache_size (>= 8M)
    join_buffer_size (> 128.0K, or always use indexes with joins)
    innodb_buffer_pool_size (>= 653M)

我希望有人能帮助我。

更新:遵循下面的混搭建议后,我从mysqltuner获得了这个新结果。

[OK] Currently running supported MySQL version 5.5.42-log
[OK] Operating on 64-bit architecture
-------- Storage Engine Statistics -------------------------------------------
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MRG_MYISAM
[--] Data in MyISAM tables: 53M (Tables: 3)
[--] Data in InnoDB tables: 650M (Tables: 233)
[--] Data in PERFORMANCE_SCHEMA tables: 0B (Tables: 17)
[--] Data in MEMORY tables: 372K (Tables: 5)
[!!] Total fragmented tables: 35
-------- Security Recommendations  -------------------------------------------
[OK] All database users have passwords assigned
-------- Performance Metrics -------------------------------------------------
[--] Up for: 3h 27m 19s (386K q [31.091 qps], 26K conn, TX: 31B, RX: 154M)
[--] Reads / Writes: 55% / 45%
[--] Total buffers: 1.5G global + 2.8M per thread (50 max threads)
[!!] Maximum possible memory usage: 1.6G (167% of installed RAM)
[OK] Slow queries: 0% (2K/386K)
[OK] Highest usage of available connections: 30% (15/50)
[OK] Key buffer size / total MyISAM indexes: 64.0M/51.9M
[!!] Key buffer hit rate: 87.6% (450K cached / 55K reads)
[OK] Query cache efficiency: 82.3% (221K cached / 269K selects)
[OK] Query cache prunes per day: 0
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 5K sorts)
[!!] Temporary tables created on disk: 57% (633 on disk / 1K total)
[OK] Thread cache hit rate: 99% (15 created / 26K connections)
[OK] Table cache hit rate: 85% (213 open / 249 opened)
[OK] Open file limit used: 0% (29/8K)
[OK] Table locks acquired immediately: 99% (159K immediate / 159K locks)
[OK] InnoDB buffer pool / data size: 700.0M/650.9M
[OK] InnoDB log waits: 0
-------- Recommendations -----------------------------------------------------
General recommendations:
    Run OPTIMIZE TABLE to defragment tables for better performance
    MySQL started within last 24 hours - recommendations may be inaccurate
    Reduce your overall MySQL memory footprint for system stability
    Temporary table size is already large - reduce result set size
    Reduce your SELECT DISTINCT queries without LIMIT clauses

答案在你的日志中:

1. Mysql 使用的内存比你多 => 2GB2.您的缓存显然已禁用

将其添加到您的 my.cnf 以减少空闲连接占用您的 RAM:

wait_timeout=20

增加以下内容:

tmp-table-size                 = 500M
max-heap-table-size            = 500M

减少以下内容:

max-connections                = 100

使用1GB RAM,您无法处理200个连接,这可能会使您的服务器崩溃,但更重要的是,您需要检查为什么query_cache似乎被禁用。修复后,它将开始正确缓存,您将能够处理更多连接

更新:

试试这个,我相信你的语法可能是错误的(如果我没记错的话,MySQL 5.5.42 使用下划线,而不是破折号),这就是为什么 mysqltuner 抱怨查询缓存被禁用的原因:

query_cache_type=1
query_cache_limit=2M
query_cache_size=256M

最终,运行

pico /var/lib/mysql/mysql-slow.log

调试所有慢查询。可能删除站点插件并优化索引等

还要检查是否有任何 sql 错误:

pico /var/lib/mysql/mysql-error.log

禁用一堆功能可以将内存消耗减少一半。

只有一个 MySql 配置文件:

sudo vi /etc/mysql/my.cnf

然后添加以下值

[mysqld]
performance_schema = OFF
symbolic-links=0
skip-external-locking
key_buffer_size = 32K
max_allowed_packet = 4M
table_open_cache = 8
sort_buffer_size = 128K
read_buffer_size = 512K
read_rnd_buffer_size = 512K
net_buffer_length = 4K
thread_stack = 480K
innodb_file_per_table
max_connections=100
max_user_connections=50
wait_timeout=50
interactive_timeout=50
long_query_time=5

然后停止/启动 MySql

sudo service mysql stop
sudo service mysql start

最新更新