Optimize my.cnf configuration for 1 gb ram with 100+ users online

user3416962 picture user3416962 · Apr 14, 2015 · Viewed 13.3k times · Source

My mysql keeps crashing. And I think it's because of improper configuration of my.cnf. Here's my my.cnf current configuration. Please help me to optimize it. I'm using a 1gb ram and 30gb ssd. I wanted to optimize my allocated ram and memory.

I created this config through 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

And here's the result of mysqltuner

 >>  MySQLTuner 1.4.0 - Major Hayden <[email protected]>
 >>  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)

I hope someone could help me.

UPDATE: After following the recommendation of mashup below, I get this new result from 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

Answer

mashup picture mashup · Apr 15, 2015

The answer is in your log:

1. Mysql is using more RAM than you have => 2GB 2. Your cache is apparently disabled

Add this to your my.cnf to reduce idle connections eating up your RAM:

wait_timeout=20

Increase the following:

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

Decrease the following:

max-connections                = 100

With 1GB RAM you can't handle 200 connections, which may crash your server, but more importantly you need to inspect why the query_cache seems to be disabled. Once you fix that it will start caching properly and you will be able to handle more connections

UPDATE:

Try this, I believe your syntax may be wrong (MySQL 5.5.42 uses underscores if I am not mistaken, not dashes) and thats why mysqltuner is complaining that query cache is disabled:

query_cache_type=1
query_cache_limit=2M
query_cache_size=256M

Eventually, run

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

Debug all slow queries. Possibly remove site plugins and optimize indexes etc

Also check for any sql errors:

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