I created a web service that generates huge requests every seconds.
Sometimes the MYSQL service seem down few secondes and work again well.
The main table contain more than 4 420 115 entries since one month.
Storage engine : InnoDB
The server configuation :
CPU : Intel(R) Xeon(R) CPU D-1540 # 2.00GHz
Coeurs : 16
Cache : 12288KB
RAM : 4x 32Go
Disques : 2 x 480 Go
The my.cnf :
skip-external-locking
key_buffer = 16M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 8
myisam-recover = BACKUP
query_cache_limit = 1M
query_cache_size = 16M
Can I have advices to avoid this problem and increase the mysql performances.
Thank's
Sounds like it may be time to iterate on the database structure and application logic to reduce the amount of DB interactions. Another option is adding a memcached or redis layer between the application and the SQL database to provide near millisecond response time for read actions.
Related
Our current infra works fine with Apache + PHP and MySQL managed elsewhere. I have not done any particular optimization on this server. These are the initial configurations. Except for memory_limit and execution_time.
I am in the process of changing our infra to something more modern with bigger server and Nginx + PHP-FPM.
While working on this problem, I realize that we have big performance losses using this new infra. It's a shame to have more powerful servers and it's slower.
I watched the server load and nothing. Memory and CPU are barely 2%
I pulled out my Inspector badge and began to eliminate guesswork. Obviously, my tests use exactly the same case, same framework, same version of our app, ...
Here are my tests:
I installed on newer Apache + PHP servers and in fact Apache is even slower than with Nginx (no surprise :)).
I created a simple "hello wok" PHP file to test if it could come from Nginx. So I am not using our framework which could be involved.
Everything is normal TTFB times are almost equivalent (~ 24ms). Nginx on the new infra is 2ms faster.
While going with our framework, I added a piece of basic code which amounts to doing a "Hello wok" but using our framework.
Ah there, we have a problem. Finally ! Old infra 61ms, New infra: 184ms.
The new infra is therefore 3x slower to load the framework.
Obviously, I did a lot of optimizing the existing code but seeing that, I'll never be able to optimize it and make it 3x faster.
In all cases: Why the same code takes 3x longer to run on different environments.
The only thing that occurs to me right now is a PHP and PHP-FPM configuration problem.
In these configurations, do you know of any reasons that could have such a slowdown?
Obviously I looked for and tested things to optimize but it's always the same.
Here is my php.ini file:
date.timezone = Europe/Paris
opcache.enable = 0
apc.enabled = 1
apc.enable_cli = 1
upload_max_filesize = 128M
post_max_size = 128M
realpath_cache_size=4096k
realpath_cache_ttl=7200
display_errors = Off
display_startup_errors = Off
access.log = /proc/self/fd/2
error_log = /proc/self/fd/2
error_reporting = E_ALL ^ E_STRICT
; output_buffering=4096
pm = dynamic
pm.max_children = 5
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 1000
max_execution_time=600
memory_limit = 2048M
opcache.enable = 1
opcache.enable_cli = 1
opcache.memory_consumption = 2048
; opcache.interned_strings_buffer = 32
; opcache.max_accelerated_files = 100000
; opcache.max_wasted_percentage = 15
opcache.revalidate_freq = 0
; opcache.validate_timestamps = 1
Do you have any advice ? recommendations ? ideas ?
thanks in advance
Are you doing well
EDIT #1
Server Information:
Old server:
VM with 1 processor / 2GB RAM
No database, just Apache + PHP
Framework specific cache system: file
$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 15
model : 6
model name : Common KVM processor
cpu MHz : 3504.002
cache size : 4096 KB
[...]
New server:
Docker container on a 3 processor / 4GB RAM server
I do not have the speeds of each processor. This is vCPU.
No database
Container PHP-FPM + Container Nginx
Framework specific cache system: APC
processor : 0
vendor_id : AuthenticAMD
cpu family : 23
model : 1
model name : AMD EPYC 7281 16-Core Processor
stepping : 2
cpu MHz : 2096.062
cache size : 512 KB
[...]
processor : 1
vendor_id : AuthenticAMD
cpu family : 23
model : 1
model name : AMD EPYC 7281 16-Core Processor
stepping : 2
cpu MHz : 2096.062
cache size : 512 KB
[...]
processor : 2
vendor_id : AuthenticAMD
cpu family : 23
model : 1
model name : AMD EPYC 7281 16-Core Processor
stepping : 2
cpu MHz : 2096.062
cache size : 512 KB
[...]
Others :
The database is managed in a provider
The sessions are stored in the database
Despite what you will read all over the internet, under low loads, Apache+mod_php is significantly faster than nginx+php-fpm. The latter architecture offers better capacity and scaling. That does not explain the difference in performance reported here.
The only thing you have told us about the two infrastructures is that one is old and one is new. If all you know about them is that you clicked a button in a cloud control panel then it can be difficult to find out more (if this is a Linux VM, check /proc/cpuinfo and mpstat paying particular attention to the steal time). The metrics you measured by testing will be highly dependent on single thread performance which is not a good indicator of throughput on a multi-cpu machine or any machine under heavy load.
But the first place I would be looking is at the storage latency / increasing opcache.revalidate_freq.
I couldn't think what else to title this strange problem.
We have a "Worker" Compute Engine which is a MySQL SLAVE. Its primary role is to process a large set of data and then place it back on the Master. All handled via a PHP Script.
Now the processing of data takes roughly 4 hours to complete. During this time we noticed the following CPU pattern.
What you can see above is the 50% solid CPU starts after a server reboot. Then after about 2 hours its starts to produce a ECG style pattern on the CPu. Around every 5/6 minutes CPU spikes to ~48% then drops over the 5 minutes.
My question is, why. Can anyoen please explain why. We ideally want this server to be Maxing out ots cpu at 100% (50% as there are 2 cores)
The spec of the server: 2 VCPU's with 7.5GB Memory.
As mentioned, if we can have this running full throttle it would be great. Below is the my.cnf
symbolic-links=0
max_connections=256
innodb_thread_concurrency = 0
innodb_additional_mem_pool_size = 1G
innodb_buffer_pool_size = 6G
innodb_flush_log_at_trx_commit = 1
innodb_io_capacity = 800
innodb_flush_method = O_DIRECT
innodb_log_file_size = 24M
query_cache_size = 1G
query_cache_limit = 512M
thread_cache_size = 32
key_buffer_size = 128M
max_allowed_packet = 64M
table_open_cache = 8000
table_definition_cache = 8000
sort_buffer_size = 128M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 128M
tmp_table_size = 256M
query_cache_type = 1
join_buffer_size = 256M
wait_timeout = 300
server-id = 2
relay-log = /var/log/mysql/mysql-relay-bin.log
log_bin = /var/log/mysql/mysql-bin.log
log-error=/var/log/mysqld.log
read-only = 1
innodb_flush_log_at_trx_commit=2
I have cleaned up the above to remove any configs with private information which are not relevant to performance.
UPDATE
I have noticed when the VPU starts dropping during the heartbeat section of the graph the PHP script is no longer running. This is impossible, as the script I know takes 4 hours. No errors, and after another 4 hours the data is where I expected it.
Changing innodb_io_capacity = 800 to 1500 will likely reduce your 4 hour elapsed time to process by raising the limit to what you know you can achieve with your slave processing.
For your 7.5G indicated environment, configuration has
innodb_additional_mem_pool_size=1G
innodb_buffer_pool_size=6G
query_cache_size=1G
so before you start, you are overcommitted.
Another angle to consider, with
max_connections=256
max_allowed_packet=64M
could on a fully busy 256 connections need 16GB + just for this function to survive.
It is unlikely max_allowed_packet at 64M is reasonable.
Changing read_rnd_buffer_size = 4M to SET GLOBAL read_rnd_buffer_size=16384; could be significant on your slave then 24 hours later on master. They can be different but if it is significant in reducing your 4 hours on the slave, implement on both instances. Let us know what this single change does for you, please.
The 50% cpu utilization you are seeing is the script maxing out the --- single core that it is capable of utilizing --- . As indicated by PressingOnAlways recently. You can not tune around limit in your running script.
For a more thorough analysis, provide from SLAVE AND MASTER
RAM size (nnG)
SHOW GLOBAL STATUS
SHOW GLOBAL VARIABLES
SHOW INNODB STATUS
CPU % is measured by all the cores - so 100% cpu usage == both cores maxing out. PHP by default runs in a single thread and does not utilize multi-cores. The 50% cpu utilization you are seeing is the script maxing out the single core that it is capable of utilizing.
In order to utilize 100% cpu, consider spawning 2 PHP scripts that work on 2 separate datasets - e.g. script 1 processes records 1-1000000, while script 2 processes 1000001-2000000.
Other option is to rewrite the script to utilize threads. You may want to consider changing the language altogether for something that is more conducive to threads, like Golang? Though this might not be necessary if the main work is done within mysql.
The other issue you're seeing when the graph is below 50% may be due to IO wait. It's hard to tell from a graph though, you may be having a data flow transfer bottleneck where your CPU isn't working and waiting while large bits of data is transferred.
Optimizing CPU utilization is an exercise in finding the bottlenecks and removing them - good luck.
'Monitoring Service' could enabled to periodically capture a 'health check' of your system since it appears to be on a 6 minute cycle when you see spikes.
SHOW GLOBAL STATUS LIKE 'Com_show_%status' may confirm activity of this nature.
Divide your com_show_%status counters by (uptime/3600) to get rate-per-hour.
10 times an hour would be every 6 minutes.
I have problems to optimize my VPS MySQL to use. I have a plan in RamNode with the following specs:
- Intel® Xeon® CPU E3-1240 V2 # 3.40GHz (4 Cores)
- 4GB de Ram
- 135 GB SSD Raid 10
I have problems in one of the applications that I have hosted, slow, sometimes gives up error "max user connections".
Below the test conducted in MySQLTunner :
Storage Engine Statistics
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MRG_MYISAM
[--] Data in MyISAM tables: 136M (Tables: 300)
[--] Data in InnoDB tables: 44M (Tables: 202)
[--] Data in PERFORMANCE_SCHEMA tables: 0B (Tables: 17)
[!!] Total fragmented tables: 220
Performance Metrics
[--] Up for: 1d 20h 25m 13s (3M q [23.681 qps], 251K conn, TX: 9B, RX: 605M)
[--] Reads / Writes: 57% / 43%
[--] Total buffers: 528.0M global + 3.6M per thread (400 max threads)
[!!] Query cache prunes per day: 7322
[!!] Sorts requiring temporary tables: 69% (144K temp sorts / 208K sorts)
[!!] Joins performed without indexes: 21719
-------- Recommendations -----------------------------------------------------
General recommendations:
Run OPTIMIZE TABLE to defragment tables for better performance. Enable the slow query log to troubleshoot bad queries. Adjust your join queries to always utilize indexes
Variables to adjust:
query_cache_size (> 64M)
sort_buffer_size (> 2M)
read_rnd_buffer_size (> 236K)
join_buffer_size (> 128.0K, or always use indexes with joins)
Below My.CNF
[mysqld]
max_connections = 400
max_user_connections=40
key_buffer_size = 256M
myisam_sort_buffer_size = 16M
read_buffer_size = 1M
table_open_cache = 2048
thread_cache_size = 128
wait_timeout = 20
connect_timeout = 10
tmp_table_size = 128M
max_heap_table_size = 64M
max_allowed_packet=268435456
net_buffer_length = 5500
max_connect_errors = 10
concurrent_insert = 2
read_rnd_buffer_size = 242144
bulk_insert_buffer_size = 2M
query_cache_limit = 2M
query_cache_size = 64M
query_cache_type = 1
query_prealloc_size = 87382
query_alloc_block_size = 21845
transaction_alloc_block_size = 2730
transaction_prealloc_size = 1364
max_write_lock_count = 2
log-error
external-locking=FALSE
open_files_limit=15000
default-storage-engine=MyISAM
innodb_file_per_table=1
[mysqld_safe]
[mysqldump]
quick
max_allowed_packet = 8M
[isamchk]
key_buffer = 128M
sort_buffer = 128M
read_buffer = 64M
write_buffer = 64M
[myisamchk]
key_buffer = 128M
sort_buffer = 128M
read_buffer = 64M
write_buffer = 64M
#### Per connection configuration ####
sort_buffer_size = 2M
join_buffer_size = 2M
thread_stack = 192K
log-slow-queries
If you can help me thank you :)
You seem to be using a mixture of MyISAM and InnoDB tables. It would be best to settle on one or the other - almost certainly InnoDB is going to be better for optimisation purposes.
You should convert all tables in your solution to InnoDB. If your solution can create new tables, change the default-storage-engine type to InnoDB as well.
default-storage-engine=InnoDB
Then, since you have plenty of RAM, tune to ensure that all your data fits in RAM, so MySQL doesn't have to keep doing expensive disk reads all the time. Set innodb_buffer_pool_size to be at least 1G, maybe more if your database is going to grow rapidly. You can probably safely go up to 2G, so long as you don't have anything else that's really ram intensive running on this VPS.
innodb_buffer_pool_size=2G
Currently you are running with the default InnoDB config options, which looks OK (144MB pool size, data size of only 44Mb), but you may as well configure for growth and take advantage of the RAM you have at your disposal. Also if you convert the 140Mb of MyISAM tables to InnoDB (recommended), then you really need this figure to be higher. This setting is probably the most important, and likely to make the biggest difference to performance.
Also, as you are hitting the max number of connections, you need to increase that value too. Your VPS should be able to handle more than the default of 40 - try anywhere between 50-100, depending on how many concurrent users/connections you expect.
max_user_connections=100
The stats also show that you have a large number of queries being executed without indexes
[!!] Joins performed without indexes: 21719
This needs attention. If it's not obvious to work out which fields need indexing (normally any fields used in join statements, and some fields used for searching and filtering, especially if they are numeric and have only a limited selection of values), you can try running queries in your favourite mysql client, with the EXPLAIN statement. This will give you detailed information about which parts of the query are performing poorly.
See the Mysql manual https://dev.mysql.com/doc/refman/5.6/en/explain.html for more information.
It is always worth trying the addition of indexes to see whether they improve query performance. If they don't, remove them again, as indexes can make insert/update operations more expensive in terms of server resource (as the indexes need to be updated as well as the underlying data).
I highly recommend a MySQL GUI such as sqlyog for playing around with indexing. The MySQL client tools are also OK.
For more details see posts such as https://www.percona.com/blog/2007/11/01/innodb-performance-optimization-basics/ (a bit old but still talks sense).
If you have a compelling reason why you'd want to use MyISAM in preference to InnoDB (and I really can't think of one), then you would need different advice as the recommendations for innodb pool size are of no use to you.
Run OPTIMIZE TABLE to defragment tables for better performance.
That's bogus advice. It is almost never useful, especially for InnoDB.
2G for the buffer pool is dangerously large for a tiny 4GB system, especially while you have MyISAM tables in use.
While you have a mixture:
key_buffer_size = 200M
innodb_buffer_pool_size = 500M
After you switch to InnoDB:
key_buffer_size = 30M
innodb_buffer_pool_size = 1000M
Swapping is much worse than lowering a value.
See my tips on converting from MyISAM to InnoDB.
The Query cache is doing a lot of prunes. Every write to a table forces a prune of all entries for that table. In general, the QC should be turned off for production servers. Raising the size above 64M would make things worse.
max_user_connections=40
Is one "user" connecting more than 40 times at once? Or do you have Apache with MaxClients > 40?
Show us a couple of your slow queries, together with SHOW CREATE TABLE; we may be able to speed them up.
Some data first:
wordpress instalation (~ 110k posts, ~20k tags, ~250 categories, latest version)
W3 Total cache included (disk caching: database, object, page, browser)
server is VPS (4 GB RAM / up to 8 GB Dynamic, 2 vCores, Ubuntu 64bit)
Running Apache2, php 5.3.10, mysql 5.5.37
What is running on the server:
a few very small domains (traffic and resources close to 0)
5 cron jobs that import data to WP
The problem: viewing a post takes around 10 s. Refreshing is in no time as the caching plugin does a good job, however if the post hasn't been visited before the time for viewing it is around 10-12 s.
After some tracing in order to find out what is going on wrong I found out that mysql query for each page is taking some 90 - 95 % of the total time (9-10 seconds for each page).
I have tried to figure out if there is something wrong with mysql configuration and modified the fallowing from default in my.cnf:
key_buffer_size = 64M
innodb_buffer_pool_size = 2G
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_thread_concurrency = 16
innodb_flush_method = O_DIRECT
innodb_additional_mem_pool_size = 20M
query_cache_limit = 8M
query_cache_size = 64M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 32
After restarting there has not been any improvement at all since I guess I am missing something. I have started tracing with newrelic and their system points out that there are wueries that take upt to 34 s to execute.
Here is an SQL query from newrelic's log:
SELECT SQL_CALC_FOUND_ROWS qmV?_posts.ID FROM qmV?_posts INNER JOIN qmV?_postmeta ON ( qmV?_posts.ID = qmV?_postmeta.post_id ) WHERE ?=? AND (
MONTH( qmV?_posts.post_date ) = ?
) AND qmV?_posts.post_type = ? AND (qmV?_posts.post_status = ?) AND (
qmV?_postmeta.meta_key = ?
) GROUP BY qmV?_posts.ID ORDER BY qmV?_postmeta.meta_value+? DESC LIMIT ?, ?
I have tried also to verify wether the tables are indexed correctly and it appears to be so as (obviously) WP is doing a great job in optimizing search across the DB.
In subsidiary a monitoring tools reports "MySQL CPU usage" to something around 95% to 105% while this query is running.
Question: is there anything else I can tweak on this server or I should have a look at some other options for hosting this application? If you propose the 2'nd option please argument your option.
Update 1:
ps aux | grep 'mysqld' returns:
mysql 522 13.5 1.0 1227664 85128 ? Ssl 2014 3513:12 /usr/sbin/mysqld
My client has got a pretty large Joomla-based website hosted on Amazon EC2 with 1.5GB of RAM. The server hosts both Apache and MySQL. Right now the database size is around 250MB and the website gets daily traffic of about 5000. It looks like there is a severe memory leak on the website as sometimes MySQL uses about 99% of CPU memory and then crashes. I have tried optimizing database tables, modifying my.cnf, but still there is no improvement.
There are finder tables used by Joomla smart search which occupy over 100MB of db size. I have disabled smart search, but still the problem occurs.
Friends, please throw some suggestions in fixing this.
Thanks.
Below is the my.cnf file
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
bind-address = 127.0.0.1
default-storage-engine=innodb
transaction-isolation = REPEATABLE-READ
character-set-server = UTF8
collation-server = UTF8_general_ci
max_connections = 5000
wait_timeout = 30
connect_timeout = 60
#interactive_timeout = 600
#max_connect_errors = 1000000
#max_allowed_packet = 10M
skip-external-locking
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8
slow_query_log
long_query_time = 2
[mysqld_safe]
log-error=/var/log/mysqld.log
myisam_sort_buffer_size = 64M
My bet would be that you are being hit by a rogue robot - one of the many SEO spiders out there, or tools like 80legs that let people program a network of bots to carry out tasks - often with errors in their programming that result in a heavy bombardment.
I can never remember which of the MySQL settings take memory once and which are per connection - but as you are set to allow up to 5000 simultaneous connections and some of the buffers are 2 and 8 MB I'd bet that the total memory usage under heavy load could easily be in excess of the total ram available.
Your current settings would allow all of your daily traffic to hit simultaneously. I'd knock that down to a setting of a hundred or less and see if that gives more stability.
There are various MySQL tuner scripts out there that can help you spot where too much memory is allocated.
If you have access logs from around the time of the crashes / high load I'd check for malicious bots though - we've had a constant battle to reign them in on some sites we monitor/control.
You might also check the thread_concurrency value - depending upon how many CPUs you have available.