Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL High CPU usage and persistent links

Tags:

php

mysql

I am having very high CPU spikes on mysqld process (greater than 100%, and even saw a 300% at one point). My load average is around: .25, .34, .28.

I read this great post about this issue: MySQL high CPU usage

One of the main things to do is disable persistent connections. So I checked my php.ini and mysql.allow_persistent = on and mysql.max_persistent = -1 -- which means no limit.

This raises a few questions for me before changing anything just to be sure:

  1. If my mysqld process is spiking over 100% every couple seconds shouldn't my load average be higher then they are?
  2. What will disabling persistent links do - will my scripts continue to function as is?
  3. If I turn this off and reload php what does this mean for my current users as there will be many active users.

EDIT:

CPU Info: Core2Quad q9400 2.6 Ghz

like image 329
bMon Avatar asked Aug 18 '11 15:08

bMon


People also ask

Why is my MySQL server using so much CPU?

Servers tend to have more than one core. Percent CPU usage is calculated relative to one core, anotherwords a process using up two cores completely will have a CPU usage of 200%. Here, MySQL is using up 100% of one core and 60% of another core. High CPU almost always means inefficient queries.

What to do when MySQL server load is high?

If the server load is indeed related to high CPU usage, we’ve found the following fixes to be useful: Enable InnoDB to handle a high number of concurrent connections – Check MySQL “PROCESSLIST”, and if you see a lot of queries in “LOCK” status, it means a lot of queries are put on hold because MyISAM tables are handling other transactions.

How to optimize your MySQL database to reduce CPU load?

Use the “PROCESSLIST” in MySQL to identify the top users, and block access to the abusive connections. Optimize database queries – Some web applications use complex queries to display site information. These queries can take a long time to execute, and cause CPU load.

Why is MySQL running so high on Linux?

Check for “leap second bug” – On July 1st, 2015, there was an addition of a leap second to standard UTC time. In servers running old Linux kernel versions, and which uses time servers, this is seen to cause MySQL high load.


1 Answers

Persistent connections won't use any CPU by themselves - if nothing's using a connection, it's just sitting idle and only consumes a bit of memory and occupies a socket.

Load averages are just that - averages. If you have a process that alternates between 0% and 100% 10 times a second, you'd get a load average of 0.5. They're good for figuring out long-term persistent high cpu, but by their nature hide/obliterate signs of spikes.

Persistent connections with mysql are usually not needed. MySQL has a relatively fast connection protocol and any time savings from using persistent connections is fairly minimal. The downside is that once a connection goes persistent, it can be left in an inconsistent state. e.g. If an app using the connection dies unexpectedly, MySQL will not see that and start cleaning up. This means that any server-side variables created by the app, any locks, any transactions, etc... will be left at the state they were in when the app crashed.

When the connection gets re-used by another app, you'll start out with what amounts to dirty dishes in the sink and an unflushed toilet. It can quite easily cause deadlocks because of the dangling transactions/locks - the new app won't know about them, and the old app is no longer around to relinquish those.

like image 101
Marc B Avatar answered Oct 16 '22 23:10

Marc B