{"id":7060,"date":"2023-02-03T16:26:05","date_gmt":"2023-02-03T10:56:05","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=7060"},"modified":"2023-02-03T16:27:22","modified_gmt":"2023-02-03T10:57:22","slug":"how-to-enable-the-slow-query-log-mysql-in-linux","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-enable-the-slow-query-log-mysql-in-linux\/","title":{"rendered":"How to enable the slow query log &#8211; Mysql in Linux"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Introduction:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><br>MySQL&#8217;s slow query log makes it easy to track SQL queries that take more than a specific time for execution. This allows you to find inefficient SQL queries that can be optimized to improve database performance.<br><br><strong>Prerequisite:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>A user with sudo privileges<\/li>\n\n\n\n<li>Mysql Database must be installed&nbsp;<\/li>\n\n\n\n<li>Mysql credentials (if secured)<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Implementation:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Step 1: SSH the server with the user having sudo privileges<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>$ ssh user@IP<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Step 2: Open the mysqld.cnf file from the path \/etc\/mysql\/mysql.conf.d\/ with vi editor or nano editor<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>$ cd \/etc\/mysql\/mysql.conf.d\/$ vi mysqld.cnf<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/FdzkvpCry7-exBtVQH1Wg2CcMTdiEECNCQ7mxYa7OFZ6T2FBsorVOTTxyh7pBPbyoNLivfwlkV87TE1RfVqj-C5ADNw4gR51tib2Vi3BvZqBOajiXrjrKQZMRjmRUZLHC3xlUvhPf_bBGZoPj6ZUG3Y\" alt=\"\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Step 3: Search for the parameter slow_query_log and edit the existing setup as mentioned&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>slow_query_log = 1slow-query_log_file = \/var\/log\/mysql\/mysql-slow.loglong_query_time = 2<br><br>======<br>Change the long query time as per the requirement in seconds.&nbsp;<br>======<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/48HOe-qhYjQU_MXIvnkyK1AxMx0ttUlg3Uk4DPX9HPs3if0IkuZ8A23mIa3epa4M_H3CIkXFvWEfwB-kLOe6xN8ZywjEM3ot9yXQcKS7VYspbdXvjMphoAaNMjhQ5G2wUPBKIi9noMH6i8CAAdTqcJE\" alt=\"\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Step 4: Create the mysql-slow.log file in the path as mentioned in the parameter slow-query_log_file in step 3&nbsp; and set its user as the mysql user<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>$touch \/var\/log\/mysql\/mysql-slow.log$chown mysql:mysql \/var\/log\/mysql\/mysql-slow.log<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Step 5: Restart MySQL service&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>$ systemctl restart mysql<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/_ifCAA_36j3FtUJKGZPg5lSQDSFPxwVTkHMEONaopwKcOsvvvH5_OdJJUhviwbH7ng2W5Pl0lprtAf3u4YJ9Dtf_XAUGlDByGVKxH2ckNNyO-cGQq7x3qchblNuTcbf29wU18AYpZU5SwVEbOdxMmTo\" alt=\"\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Step 6: To save all slow queries that the system previously recorded in the mysql-slow.log file&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>$ mysqldumpslow -a \/var\/log\/mysql\/mysql-slow.log<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/zkLRtCvahqBNLbJmKP0jVkwwm3-sxzLOlGzEoyORb9DWZRsV7GbXstuSZVdh1Q6KWQNUIQs6kf-d7rIVAnsGUWzwZX--Z9YmEbFRfvn7qjIwOmjjkbYMXz0Ok__MT8Z5nWx-Ci3m1fPb6FgcATnikLE\" alt=\"\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Step 7: To verify this setup log in to MySQL database server<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>$ mysql -u root -p<br>====<br>Enter the root user password<br>====<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Step 8: Run the below query to verify whether the slow query log has been enabled or not<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SHOW GLOBAL VARIABLES LIKE &#8216;slow_query_log%&#8217;;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/XCAZ4S_wSVv_A5PXpI94rkQAtOTARXcYmk3BPWZ7zPiP-O_VvC2UH2Dd-poA0Xv_Nx6XOmyWwf5D44hJyagaqcdZUvOXd-vB59hMqP5duARlbbH8yqpIEy0b7jEniEz3WbxdRlrW6af20AuS5JQ5mqo\" alt=\"\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><br>If the slow_query_log value is ON, we can confirm that the slow query log has been enabled successfully<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Conclusion:<\/strong><br><br>By enabling the slow query log for MySQL we can diagnose the performance and efficiency issues that affect the server.<br>By identifying queries that are particularly slow in their execution, we can address them while restructuring the application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: MySQL&#8217;s slow query log makes it easy to track SQL queries that take more than a specific time for [&hellip;]<\/p>\n","protected":false},"author":495,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1019],"tags":[179],"class_list":["post-7060","post","type-post","status-publish","format-standard","hentry","category-cloud-aws","tag-mysql-2","psol-cat-cloud-aws"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1PS","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/495"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=7060"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7060\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=7060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=7060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=7060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}