我的mysql一直在崩溃。而且我认为这是因为my.cnf的配置不当。这是我的my.cnf当前配置。请帮我优化一下。我使用1gb ram和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)
我希望有人可以帮助我。
更新:按照下面的mashup建议后,我从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
答案 0 :(得分:2)
答案在你的日志中:
1。 Mysql使用的内存比你拥有的多得多=&gt; 2GB 2。您的缓存显然已停用
将此添加到my.cnf以减少闲置连接占用内存:
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