{"id":389,"date":"2021-07-23T12:28:57","date_gmt":"2021-07-23T12:28:57","guid":{"rendered":"https:\/\/ssdsunucum.com\/blog\/how-to-enable-the-slow-query-log-in-mysql-or-mariadb\/"},"modified":"2021-07-23T12:28:57","modified_gmt":"2021-07-23T12:28:57","slug":"how-to-enable-the-slow-query-log-in-mysql-or-mariadb","status":"publish","type":"post","link":"https:\/\/ssdsunucum.com\/blog\/how-to-enable-the-slow-query-log-in-mysql-or-mariadb\/","title":{"rendered":"How to Enable the Slow Query Log in MySQL\u00ae or MariaDB"},"content":{"rendered":"<\/p>\n<div class=\"col-md-9\">\n<div class=\"flex-column flex-md-row article-header\"><\/div>\n<hr>\n<h2 id=\"overview\">Overview<\/h2>\n<p>Enabling the Slow Query Log for MySQL\u00ae or MariaDB can be a useful tool to diagnose performance and efficiency issues affecting your server. By identifying queries that are particularly slow in their execution, you can address them by restructuring the application that triggers your queries. You can also rebuild the queries themselves to ensure that they are constructed as efficiently as possible.<\/p>\n<p>For more information about the MySQL slow query log, read the MySQL 5.7 Reference Manual: The Slow Query Log documentation.<\/p>\n<h2 id=\"enable-the-slow-query-log\">Enable the Slow Query Log<\/h2>\n<p>To enable the Slow Query Log for MySQL or MariaDB:<\/p>\n<ol>\n<li>\n<p>Log in to your server as the <code>root<\/code> user via SSH.<\/p>\n<\/li>\n<li>\n<p>Open the <code>my.cnf<\/code> file with a text editor and add the following block of code under the <code>mysqld<\/code> section:\n<\/p>\n<div class=\"highlight\">\n<pre style=\"color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4\"><code class=\"language-BASH\" data-lang=\"BASH\">slow_query_log <span style=\"color:#f92672\">=<\/span> <span style=\"color:#ae81ff\">1<\/span>\nslow-query_log_file <span style=\"color:#f92672\">=<\/span> \/var\/log\/mysql-slow.log\nlong_query_time <span style=\"color:#f92672\">=<\/span> <span style=\"color:#ae81ff\">2<\/span><\/code><\/pre>\n<\/div>\n<div class=\"callout callout-info\">\n<div class=\"callout-heading\">Note:<\/div>\n<div class=\"callout-content\">\n<p>In MySQL 5.6 and older, use the <code>log-slow-queries<\/code> variable instead of the <code>slow-query_log_file<\/code> variable.<\/p>\n<\/p><\/div>\n<\/div>\n<\/li>\n<li>\n<p>Create the <code>\/var\/log\/mysql-slow.log<\/code> file and set its user as the <code>mysql<\/code> user. To do this, run the following commands:\n<\/p>\n<div class=\"highlight\">\n<pre style=\"color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4\"><code class=\"language-BASH\" data-lang=\"BASH\">touch \/var\/log\/mysql-slow.log\nchown mysql:mysql \/var\/log\/mysql-slow.log<\/code><\/pre>\n<\/div>\n<\/li>\n<li>\n<p>Restart MySQL or MariaDB. To do this, run the following command:\n<\/p>\n<div class=\"highlight\">\n<pre style=\"color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4\"><code class=\"language-BASH\" data-lang=\"BASH\">\/usr\/local\/cpanel\/scripts\/restartsrv_mysql<\/code><\/pre>\n<\/div>\n<\/li>\n<li>\n<p>Start monitoring the slow query logfile. To analyze and print the file\u2019s summary, run the <code>mysqldumpslow<\/code> command. For example, to print all slow queries that the system previously recorded, run the following command:\n<\/p>\n<div class=\"highlight\">\n<pre style=\"color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4\"><code class=\"language-BASH\" data-lang=\"BASH\">mysqldumpslow -a \/var\/log\/mysql-slow.log<\/code><\/pre>\n<\/div>\n<\/li>\n<\/ol>\n<p>For a complete list of options to use with the <code>mysqldumpslow<\/code> command, read MySQL\u2019s mysqldumpslow article.<\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Overview Enabling the Slow Query Log for MySQL\u00ae or MariaDB can be a useful tool to diagnose performance and efficiency issues affecting your server. By identifying queries that are particularly slow in their execution, you can address them by restructuring the application that triggers your queries. You can also rebuild the queries themselves to ensure &hellip;<\/p>\n","protected":false},"author":1,"featured_media":390,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ssdsunucum.com\/blog\/wp-json\/wp\/v2\/posts\/389"}],"collection":[{"href":"https:\/\/ssdsunucum.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ssdsunucum.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ssdsunucum.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ssdsunucum.com\/blog\/wp-json\/wp\/v2\/comments?post=389"}],"version-history":[{"count":0,"href":"https:\/\/ssdsunucum.com\/blog\/wp-json\/wp\/v2\/posts\/389\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ssdsunucum.com\/blog\/wp-json\/wp\/v2\/media\/390"}],"wp:attachment":[{"href":"https:\/\/ssdsunucum.com\/blog\/wp-json\/wp\/v2\/media?parent=389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ssdsunucum.com\/blog\/wp-json\/wp\/v2\/categories?post=389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ssdsunucum.com\/blog\/wp-json\/wp\/v2\/tags?post=389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}