Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL CPU increase when I have Sleeping connection that stay open

I have a MySQL 5.6.27-0ubuntu0.14.04.1 that run on a Google Compute instance with 4 CPU.

I noticed that if I have a connection that Sleep for a long time, then the CPU of the server will increase in a linear way. I don't understand why? If I kill the Sleep connection then CPU just restore to a correct usage.

So to summary I have the following: I notice the CPU of my instance is increasing: enter image description here

Then I check the processlist on my server

mysql> show processlist
-> ;
+-------+--------+-------------------+----------------+---------+------+-------+------------------+
| Id    | User   | Host              | db             | Command | Time | State | Info             |
+-------+--------+-------------------+----------------+---------+------+-------+------------------+
| 85949 | nafora | paper-eee-2:58461 | state_recorder | Sleep   | 1300 |       | NULL             |
| 85956 | nafora | paper-eee-2:58568 | state_recorder | Sleep   |   64 |       | NULL             |
| 85959 | root   | localhost         | NULL           | Query   |    0 | init  | show processlist |
+-------+--------+-------------------+----------------+---------+------+-------+------------------+

You can see I have just 2 connection that Sleep and one is here from 1300 seconds (because I have a process that is stuck with the connection open)

So I kill the connection 85949, and the CPU just fall down. enter image description here

Can someone explain me why a single connection that is sleeping can impact my database like this.

Thanks.

like image 471
Julien Bachmann Avatar asked Nov 27 '15 15:11

Julien Bachmann


People also ask

Why is MySQL taking too much CPU?

Increases in CPU utilization can be caused by several factors, such as user-initiated heavy workloads, multiple concurrent queries, or long-running transactions. To identify the source of the CPU usage in your Amazon RDS for MySQL instance, review the following approaches: Enhanced Monitoring. Performance Insights.

What does sleep do in MySQL?

A sleeping MySQL query is an inactive open connection. When too many exist at the same time, the MySQL server will run out of connections, causing the server to be unable to process queries. However, blindly killing sleeping queries can have bad results, such as database corruption or data loss.

Should I close MySQL connection after every query?

Connections can die. You need a database connection wrapper that can determine whether connection is established and if not - establish one. The same wrapper should take care of disconnect/reconnect. In other words, do not close the connection.

How do I stop sleep queries in MySQL?

To overcome this issue of SQL sleep command, MySQL uses two parameters: interactive_ timeout and wait_ timeout. These require certain values to be set to help query run-up to that set time. By default, both the parameters have set the value as 28800 seconds (i.e. 8 hours).


1 Answers

Some non-closed connections or long running slow queries might cause this behavior. You could limit the non-closed connections by configuring the global variable wait_timeout as reasonable value and also set another related variable interactive_timeout as high as per best practice.

Stateful applications that use a connection pool (Java, .NET, etc.) will need to adjust wait_timeout to match their connection pool settings. The default 8 hours (wait_timeout = 28800) works well with properly configured connection pools.

Configure the wait_timeout to be slightly longer than the application connection pool’s expected connection lifetime. This is a good safety check. Also profile the queries accordingly to observe the performance of MySQL instance can help you to avoid I/O bottlenecks.

like image 138
S.K. Venkat Avatar answered Oct 18 '22 04:10

S.K. Venkat