This article is a mirror article of machine translation, please click here to jump to the original article.

View: 33231|Reply: 6

[Source] MySQL analyzes high CPU usage in high concurrency situations

[Copy link]
Posted on 9/5/2017 8:23:59 PM | | | |


Recently, for some reasons, the number of website visits has increased sharply, and from the figure, it can be seen that the number of concurrent connections is more than 1,200

Because the server configuration is very low, 1 core and 2G memory, and then the website is very slow to access when there are many people


We use the top command to check, mysql takes up a lot of CPU and memory, and other processes are php-fpm taking up CPU and memory, which is not very high

If mysql is running normally, it is that some SQL statements in PHP cause problems to be discovered, and the root user enters mysql management
mysql -u root -p
Enter the password, then enter the following command




With this command, we can see that the database is constantly executing delete operations with where conditions.



pre_k_spider this table is a table that records spider accesses, presumably caused by a plugin installed

We can see that pre_k_spider table has more than 100,000 records, and the query takes 33 seconds



Decisively empty this table, and then, go to the management background to close the relevant plug-ins of the table and restore it to normal!









Previous:Alibaba Cloud Cloud Marketplace Verification Code Recognition Service
Next:mysql 5.6.21 to view the my.cnf configuration file
 Landlord| Posted on 9/6/2017 9:32:56 AM |


Today the website is stuck again, and a large number of sleep is found

Causes of sleep:

1 The client program did not call mysql_close() before exiting.

2 The client had been sleeping more than wait_timeout or interactive_timeout seconds without issuing any requests to the server.

3 The client program ended abruptly in the middle of a data transfer

If the number of sleep processes in the same period is too high, and the total number of connections in other states exceeds the max_connection value, then MySQL can no longer process any requests except the root user, cannot establish a connection with any request, or hangs directly

Solution:

1. First, check whether your program uses the pconnect method, and secondly, check whether the mysql_close() is called in time before the page is executed. Try not to use the pconnect method, i.e. use mysql_connect. When the program is executed, the mysql_close should be explicitly invoked

2. Add wait_timeout and interactive_timeout to my.cnf, set the value smaller, by default the wait_timeout value is 8 hours, you can change it to 1 hour, or half an hour. This way mysql will kill dead connections faster. Prevent the total number of connections from exceeding the max_connection value.

wait_timeout too large will cause a large number of SLEEP processes in MySQL to be released in time, dragging down system performance, but if the settings are too small, you may encounter problems such as "MySQL has gone away"

Set the max_connection value to a higher value, but this is obviously not appropriate, the more connections you have, the more stress it puts on your server. In fact, those connections are redundant, and killing them as soon as possible is the best policy.

3. Analyze the system's SQL query step by step, find the SQL that is too slow, and optimize it



1)interactive_timeout:
Parameter meaning: The number of seconds the server waits for an activity before turning off the interactive connection. An interactive client is defined as a client that uses CLIENT_INTERACTIVE option in mysql_real_connect().
Parameter default: 28800 seconds (8 hours)

(2)wait_timeout:
Parameter meaning: The number of seconds that the server waits for activity before closing the non-interactive connection.
When the thread starts, the session wait_timeout value is initialized based on the global wait_timeout or global interactive_timeout value, depending on the client type (defined CLIENT_INTERACTIVE the connection options of mysql_real_connect()).
Parameter default: 28800 seconds (8 hours)

There is an upper limit to the maximum number of connections that MySQL Server can support, as each connection is established and consumes memory, so we expect clients to disconnect and free up the occupied memory after connecting to MySQL Server to handle the corresponding operation. If your MySQL Server has a large number of idle connections, not only will they consume memory in vain, but if the connections keep accumulating and opening, they will eventually reach the maximum number of connections in MySQL Server, which will report the error 'too many connections'. The value setting of the wait_timeout should be judged according to the operation of the system. After the system has been running for a period of time, you can check the connection status of the current system through the show processlist command, if you find a large number of connected processes in the sleep state, it means that the parameter setting is too large, and you can make appropriate adjustments to make smaller adjustments.


sql command:



This modification method is inAfter restarting the mysql service, it will be invalid, so it's best to configure these two properties in the mysql configuration file.

Modify the my.cnf file under Linux:




Posted on 9/6/2017 11:15:15 AM |
I have to get a master-slave server
 Landlord| Posted on 9/6/2017 11:15:49 AM |
vrchary posted on 2017-9-6 11:15
I have to get a master-slave server

Server configuration reason The configuration is low   
 Landlord| Posted on 9/6/2017 1:47:56 PM |
Solved the issue of "mysql-bin.000001" taking up a large amount of space
http://www.itsvse.com/thread-2694-1-1.html
(Source: Architect)

After installing MySQL with ports, after a while I found that the /var space was insufficient, I checked and found that mysql-bin.000001, mysql-bin.000002 and other files took up space, so what are these files for? This is the operation log of the database, such as UPDATE a table, or DELETE some data, even if the statement does not match the data, this command will be stored in the log file, including the execution time of each statement, which will also be recorded.

This is mainly done for the following two purposes:
1: Data recovery
If your database has a problem and you have a backup before, you can look at the log file to find out which command caused your database problem and find a way to recover the loss.
2: Synchronize data between master-slave servers
All operations on the primary server are recorded in a log that the slave server can use to ensure that both are synchronized.

Just close the log


 Landlord| Posted on 10/9/2017 2:33:30 PM |
mysqld is too CPU occupying Windows below



Solution: Optimize the SQL statement
Posted on 12/30/2021 11:15:36 PM |
Support it!!!!!!!!!!!!!!!!!
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com