Terminating Inactive MySQL Connections
It is common to observe numerous MySQL connections remaining idle over extended periods. This can prompt questions regarding how to terminate or close such connections from the server, without resorting to restarting the service.
Manual Termination
One approach is to manually terminate idle connections using the KILL command. To identify the process ID for the idle connection, execute the following query:
mysql> show full processlist;
Once the process ID is known, it can be terminated with:
mysql> kill [process_id];
However, this method has limitations:
Automatic Connection Management
Alternatively, configuring MySQL's timeout settings can automatically handle the termination of idle connections. By reducing the wait_timeout and interactive_timeout values, MySQL will automatically close connections that remain inactive for a specified duration. To adjust these settings, run the following commands:
mysql> show variables like "%timeout%"; mysql> set global wait_timeout=[new timeout value]; mysql> set global interactive_timeout=[new timeout value];
It is important to note that while this approach addresses the symptom of idle connections, it does not resolve the underlying cause. It is recommended to investigate why connections remain open and consider whether connection pooling is being used by the web server.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3