MySQL database uses too much CPU and run very slow, help configuration [closed]

Posted on

Question :

i need really your help, for set up best config for my website. I have a MySQL database which uses and it use too much CPU. 30-40% and then go to 100% up and down. If are connected few users or more than 100, it do the same. I check all, and the apache server work normally.

I am actually performing benchmarking to the web servers which queries the database. Right now the database is being the bottleneck, it uses too much CPU, and the website run very slow with 10 or 100 users, is the same. The server have 2.4 GHz The VM has 4 vCPU and 12GB of memory. System centos 7
The mysql configuration is this:

datadir = /var/lib/mysql
socket = /var/lib/mysql/mysql.sock


max_connections = 3072

key_buffer_size = 6G
key_buffer = 1M
query_cache_size = 32M
join_buffer_size = 1M
bulk_insert_buffer_size = 1G
tmp_table_size = 1G
max_heap_table_size = 1G
bulk_insert_buffer_size = 1G



thread_cache_size = 1
thread-pool-idle-timeout = 60

#innodb_buffer_pool_size = 5G
innodb_additional_mem_pool_size = 4M
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 128M
innodb_lock_wait_timeout = 60
innodb_thread_concurrency = 6
innodb_commit_concurrency = 4

innodb_support_xa = 0

expire_logs_days = 3

sort_buffer_size = 1M
read_buffer_size =  1M
read_rnd_buffer_size = 1M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 256M

## Binlog sync settings
## XA transactions = 1, otherwise set to 0 for best performance
sync_binlog         = 0

## TX Isolation
transaction-isolation       = REPEATABLE-READ #REPEATABLE-READ req for ACID, SERIALIZABLE req XA

## Per-Thread Buffer memory utilization equation:
#(read_buffer_size + read_rnd_buffer_size + sort_buffer_size + thread_stack + join_buffer_size + binlog_cache_size) * max_connections

## Global Buffer memory utilization equation:
# innodb_buffer_pool_size + innodb_additional_mem_pool_size + innodb_log_buffer_size + key_buffer_size + query_cache_size

!includedir /etc/my.cnf.d

and the results of 1 hour of low usage.

 >>  MySQLTuner 1.7.9 - Major Hayden <>
 >>  Bug reports, feature requests, and downloads at
 >>  Run with '--help' for additional options and output filtering

[--] Skipped version check for MySQLTuner script
Please enter your MySQL administrative login: root
Please enter your MySQL administrative password: [OK] Currently running supported MySQL version 5.5.56-MariaDB
[OK] Operating on 64-bit architecture

-------- Log file Recommendations ------------------------------------------------------------------
[--] Log file: /var/log/mariadb/mariadb.log(31K)
[OK] Log file /var/log/mariadb/mariadb.log exists
[OK] Log file /var/log/mariadb/mariadb.log is readable.
[OK] Log file /var/log/mariadb/mariadb.log is not empty
[OK] Log file /var/log/mariadb/mariadb.log is smaller than 32 Mb
[!!] /var/log/mariadb/mariadb.log contains 1 warning(s).
[!!] /var/log/mariadb/mariadb.log contains 114 error(s).
[--] 1 start(s) detected in /var/log/mariadb/mariadb.log
[--] 1) 180619 16:42:54 [Note] /usr/libexec/mysqld: ready for connections.
[--] 0 shutdown(s) detected in /var/log/mariadb/mariadb.log

-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Data in MyISAM tables: 240M (Tables: 347)
[--] Data in InnoDB tables: 32K (Tables: 2)
[OK] Total fragmented tables: 0

-------- Security Recommendations ------------------------------------------------------------------
[OK] There are no anonymous accounts for any database users
[!!] failed to execute: SELECT CONCAT(user, '@', host) FROM mysql.user WHERE (password = '' OR password IS NULL) AND                           plugin NOT IN ('unix_socket', 'win_socket', 'auth_pam_compat')
[!!] FAIL Execute SQL / return code: 256
[OK] All database users have passwords assigned
[!!] User 'admin@%' does not specify hostname restrictions.
[!!] User 'root@%' does not specify hostname restrictions.
[!!] There is no basic password file list!

-------- CVE Security Recommendations --------------------------------------------------------------
[--] Skipped due to --cvefile option undefined

-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 25m 12s (275K q [181.983 qps], 24K conn, TX: 179M, RX: 29M)
[--] Reads / Writes: 97% / 3%
[--] Binary logging is disabled
[--] Physical Memory     : 11.6G
[--] Max MySQL memory    : 2.3G
[--] Other process memory: 95.4M
[--] Total buffers: 1.4G global + 4.3M per thread (214 max threads)
[--] P_S Max memory usage: 0B
[--] Galera GCache Max memory usage: 0B
[OK] Maximum reached memory usage: 1.8G (15.95% of installed RAM)
[OK] Maximum possible memory usage: 2.3G (19.92% of installed RAM)
[OK] Overall possible memory usage with other process is compatible with memory available
[OK] Slow queries: 0% (7/275K)
[OK] Highest usage of available connections: 48% (104/214)
[OK] Aborted connections: 0.28%  (68/24397)
[!!] name resolution is active : a reverse name resolution is made for each new connection and can reduce performanc                          e
[!!] Query cache may be disabled by default due to mutex contention.
[OK] Query cache efficiency: 42.9% (158K cached / 369K selects)
[OK] Query cache prunes per day: 0
[!!] Sorts requiring temporary tables: 15% (616 temp sorts / 4K sorts)
[!!] Joins performed without indexes: 1012
[!!] Temporary tables created on disk: 42% (1K on disk / 3K total)
[!!] Thread cache is disabled
[OK] Table cache hit rate: 109% (351 open / 322 opened)
[OK] Open file limit used: 38% (398/1K)
[!!] Table locks acquired immediately: 93%

-------- Performance schema ------------------------------------------------------------------------
[--] Performance schema is disabled.
[--] Memory used by P_S: 0B
[--] Sys schema isn't installed.

-------- ThreadPool Metrics ------------------------------------------------------------------------
[--] ThreadPool stat is enabled.
[--] Thread Pool Size: 4 thread(s).
[--] Using default value is good enough for your version (5.5.56-MariaDB)

-------- MyISAM Metrics ----------------------------------------------------------------------------
[OK] Key buffer used: 100.0% (1M used / 1M cache)
[OK] Key buffer size / total MyISAM indexes: 1.0M/65.9M
[OK] Read Key buffer hit rate: 97.3% (59M cached / 1M reads)
[!!] Write Key buffer hit rate: 94.3% (7K cached / 6K writes)

-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[--] InnoDB Thread Concurrency: 6
[!!] InnoDB File per table is not activated
[OK] InnoDB buffer pool / data size: 128.0M/32.0K
[!!] Ratio InnoDB log file size / InnoDB Buffer pool size (7.8125 %): 5.0M * 2/128.0M should be equal 25%
[OK] InnoDB buffer pool instances: 1
[--] InnoDB Buffer Pool Chunk Size not used or defined in your version
[OK] InnoDB Read buffer efficiency: 99.16% (48632 hits/ 49042 total)
[!!] InnoDB Write Log efficiency: 246.15% (128 hits/ 52 total)
[OK] InnoDB log waits: 0.00% (0 waits / 180 writes)

-------- AriaDB Metrics ----------------------------------------------------------------------------
[--] AriaDB is enabled.
[OK] Aria pagecache size / total Aria indexes: 128.0M/1B
[OK] Aria pagecache hit rate: 100.0% (97M cached / 1K reads)

-------- TokuDB Metrics ----------------------------------------------------------------------------
[--] TokuDB is disabled.

-------- XtraDB Metrics ----------------------------------------------------------------------------
[--] XtraDB is disabled.

-------- RocksDB Metrics ---------------------------------------------------------------------------
[--] RocksDB is disabled.

-------- Spider Metrics ----------------------------------------------------------------------------
[--] Spider is disabled.

-------- Connect Metrics ---------------------------------------------------------------------------
[--] Connect is disabled.

-------- Galera Metrics ----------------------------------------------------------------------------
[--] Galera is disabled.

-------- Replication Metrics -----------------------------------------------------------------------
[--] Galera Synchronous replication: NO
[--] No replication slave(s) for this server.
[--] Binlog format: STATEMENT
[--] XA support enabled: OFF
[--] Semi synchronous replication Master: Not Activated
[--] Semi synchronous replication Slave: Not Activated
[--] This is a standalone server

-------- Recommendations ---------------------------------------------------------------------------
General recommendations:
    Control warning line(s) into /var/log/mariadb/mariadb.log file
    Control error line(s) into /var/log/mariadb/mariadb.log file
    Restrict Host for user@% to user@SpecificDNSorIp
    MySQL was started within the last 24 hours - recommendations may be inaccurate
    Enable the slow query log to troubleshoot bad queries
    Configure your accounts with ip or subnets only, then update your configuration with skip-name-resolve=1
    Adjust your join queries to always utilize indexes
    Temporary table size is already large - reduce result set size
    Reduce your SELECT DISTINCT queries without LIMIT clauses
    Set thread_cache_size to 4 as a starting value
    Optimize queries and/or use InnoDB to reduce lock wait
    Consider installing Sys schema from
    Before changing innodb_log_file_size and/or innodb_log_files_in_group read 
Variables to adjust:
    query_cache_size (=0)
    query_cache_type (=0)
    sort_buffer_size (> 1M)
    read_rnd_buffer_size (> 1M)
    join_buffer_size (> 1.0M, or always use indexes with JOINs)
    thread_cache_size (start at 4)
    innodb_log_file_size should be (=16M) if possible, so InnoDB total log files size equals to 25% of buffer pool size.

Please help me, i change some time the configuration, but always the same, better or worse.

Answer :

With 12 GB RAM and your my.cnf-ini listing 15GB requested at startup, this would seem to be a minunderstanding of how MySQL uses RAM. Rethink your requests and use more reasonable values, please.

Leave a Reply

Your email address will not be published. Required fields are marked *