{"id":1198,"date":"2023-10-04T16:39:18","date_gmt":"2023-10-04T11:09:18","guid":{"rendered":"https:\/\/www.veeble.org\/kb\/?p=1198"},"modified":"2025-03-14T12:06:52","modified_gmt":"2025-03-14T06:36:52","slug":"optimizing-mysql-mariadb-ram-usage-resource-efficiency-guide","status":"publish","type":"post","link":"https:\/\/www.veeble.com\/kb\/optimizing-mysql-mariadb-ram-usage-resource-efficiency-guide\/","title":{"rendered":"Optimizing MySQL\/MariaDB RAM Usage: Resource Efficiency Guide"},"content":{"rendered":"\n<p>Your server&#8217;s performance is sluggish, and your websites are crawling like a snail. The main issue? MySQL is consuming too much of your server&#8217;s memory, as shown by commands like <a href=\"https:\/\/www.veeble.org\/kb\/how-to-monitor-cpu-usage-in-linux\/\" target=\"_blank\" rel=\"noopener\">top and free<\/a>. How can you improve MySQL performance and enhance your server&#8217;s efficiency?<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full is-style-default\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-04-143751-1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"759\" height=\"395\" data-id=\"1200\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-04-143751-1.png\" alt=\"High mysqld usage in linux\" class=\"wp-image-1200\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-04-143751-1.png 759w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-04-143751-1-300x156.png 300w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-04-143751-1-60x31.png 60w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-04-143751-1-150x78.png 150w\" sizes=\"auto, (max-width: 759px) 100vw, 759px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-04-143801.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"783\" height=\"144\" data-id=\"1201\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-04-143801.png\" alt=\"High buffer\/cache usage in linux\" class=\"wp-image-1201\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-04-143801.png 783w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-04-143801-300x55.png 300w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-04-143801-768x141.png 768w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-04-143801-60x11.png 60w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-04-143801-150x28.png 150w\" sizes=\"auto, (max-width: 783px) 100vw, 783px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<p>Notice that <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/mysqld.html\" target=\"_blank\" rel=\"noopener\">mysql<\/a> is at the top of the list, indicating a high memory occupancy by MySQL. You can address this issue by implementing limits to reduce MySQL&#8217;s memory utilization. The data in the &#8216;buff\/cache&#8217; section represents cached memory pages containing data used by MySQL and other server services. The system caches data in memory to minimize the need for read\/write operations to slower hard disk devices, significantly improving overall system performance and reducing latency in responses from disk devices.<br><\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"flushing-the-memory-ram-cache\">Flushing the Memory (RAM) cache<\/h3>\n\n\n<p>To flush the Memory (RAM) cache by following the instructions:<br><br>To release cached memory effectively, you can leverage the &#8216;<a href=\"https:\/\/linux-mm.org\/Drop_Caches\" target=\"_blank\" rel=\"noopener\">drop_caches<\/a>&#8216; feature embedded in the Linux kernel. But before you proceed, it&#8217;s crucial to ensure all &#8216;dirty&#8217; pages are written to disk. Execute the &#8216;<a href=\"https:\/\/www.computerhope.com\/unix\/sync.htm#:~:text=The%20sync%20command%20forces%20an,filesystem%20containing%20the%20specified%20files.\" target=\"_blank\" rel=\"noopener\">sync<\/a>&#8216; command to accomplish this, reducing the number of dirty pages within the caches.<\/p>\n\n\n\n<p>Accessing the &#8216;drop_caches&#8217; capability involves passing a specific numeric control to the &#8216;\/proc\/sys\/vm\/drop_caches&#8217; path through the &#8216;echo&#8217; command. For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">echo 3 &gt; \/proc\/sys\/vm\/drop_caches<\/pre>\n\n\n\n<p>More details regarding the above command will be available in the blog: <a href=\"https:\/\/linux-mm.org\/Drop_Caches\" target=\"_blank\" rel=\"noopener\">https:\/\/linux-mm.org\/Drop_Caches<\/a> . Refer to the article before doing the command!<\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"flushing-memory-ram-cache-automatically\">Flushing Memory (RAM) Cache automatically<\/h3>\n\n\n<p>You can use a cronjob to automatically flush the Memory cache by following the instructions:<\/p>\n\n\n\n<p>The root crontab provides the capability to schedule the execution of a script at specific times. You can achieve this by configuring a task in the <a href=\"https:\/\/www.geeksforgeeks.org\/crontab-in-linux-with-examples\/\" target=\"_blank\" rel=\"noopener\">cron job<\/a> file, based on an analysis of how frequently you require cache clearance.<br>To accomplish this task, Open the root user&#8217;s crontab file by executing the command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">crontab -u root -e<\/pre>\n\n\n\n<p>Within the crontab editor, add the specific cron job entry that suits your requirements and then save.<br>For example:<br><em>0 0 * * * echo 3 &gt; \/proc\/sys\/vm\/drop_caches<\/em><\/p>\n\n\n\n<p>For more details regarding setting cron jobs refer to the article: <a href=\"https:\/\/www.geeksforgeeks.org\/crontab-in-linux-with-examples\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.geeksforgeeks.org\/crontab-in-linux-with-examples\/<\/a><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Note: The steps outlined for adjusting MySQL parameters are applicable to systems that possess control over their own kernel. However, in cases such as VPS environments utilizing <a href=\"https:\/\/www.veeble.org\/in\/vps-hosting\/\" target=\"_blank\" rel=\"noopener\">OpenVZ technology<\/a>, where each VPS instance does not have its own kernel, it may not be possible to apply these settings since the kernel settings are typically managed at the host level rather than at the individual VPS level.<\/p>\n<\/blockquote>\n\n\n<h3 class=\"wp-block-heading\" id=\"altering-caching-by-editing-mysql-configuration\">Altering caching by editing MySQL configuration<\/h3>\n\n\n<p>To alter caching in the MySQL configuration file, follow the steps below:<\/p>\n\n\n\n<p> 1. Ensure you have a safety net by backing up your database first.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysqldump -u [username] -p [database_name] &gt; backup.sql<\/pre>\n\n\n\n<p>2. Execute the below command to understand its current configuration:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql -u root -p -e \"SHOW VARIABLES;\"<\/pre>\n\n\n\n<p>3. Open the MySQL configuration file, typically found at \/etc\/my.cnf or \/etc\/mysql\/my.cnf<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">nano \/etc\/my.cnf<\/pre>\n\n\n\n<p> 3.1. Set the size of the InnoDB buffer pool (innodb_buffer_pool_size) based on available RAM:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">innodb_buffer_pool_size = [size]G<\/pre>\n\n\n\n<p> 3.2. Make sure to set the values to be following for all key values in the configuration file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">query_cache_type = 1\nquery_cache_size = [size]M\nkey_buffer_size = [size]M\nthread_cache_size = [size]\nthread_concurrency = [value]\nslow_query_log = 1\nslow_query_log_file = \/var\/log\/mysql\/mysql-slow.log\nlog_error = \/var\/log\/mysql\/error.log\n<\/pre>\n\n\n\n<p>4. Restart the mysqld by the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">systemctl restart mysql<\/pre>\n\n\n\n<div style=\"height:45px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-uagb-call-to-action uagb-block-44eabd2f wp-block-button\"><div class=\"uagb-cta__wrap\"><h3 class=\"uagb-cta__title\">Premium KVM VPS, Without the Premium Price<\/h3><p class=\"uagb-cta__desc\">Get the performance you need at a price you&#8217;ll love. Our KVM VPS plans are designed for value.<\/p><\/div><div class=\"uagb-cta__buttons\"><a href=\"https:\/\/www.veeble.com\/in\/kvm-vps\/\" class=\"uagb-cta__button-link-wrapper wp-block-button__link\" target=\"_blank\" rel=\"noopener noreferrer\">View Pricing<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Your server&#8217;s performance is sluggish, and your websites are crawling like a snail. The main issue? MySQL is consuming too much of your server&#8217;s [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":8077,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","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":"","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":"set","ast-page-background-enabled":"default","ast-page-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":"","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-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":"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":""},"mobile":{"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":""}},"footnotes":""},"categories":[11],"tags":[],"class_list":["post-1198","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql"],"uagb_featured_image_src":{"full":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/10\/Optimizing-MySQLMariaDB-RAM-Usage-Resource-Efficiency-Guide.jpg",1366,768,false],"thumbnail":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/10\/Optimizing-MySQLMariaDB-RAM-Usage-Resource-Efficiency-Guide-150x150.jpg",150,150,true],"medium":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/10\/Optimizing-MySQLMariaDB-RAM-Usage-Resource-Efficiency-Guide-300x169.jpg",300,169,true],"medium_large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/10\/Optimizing-MySQLMariaDB-RAM-Usage-Resource-Efficiency-Guide-768x432.jpg",768,432,true],"large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/10\/Optimizing-MySQLMariaDB-RAM-Usage-Resource-Efficiency-Guide-1024x576.jpg",1024,576,true],"1536x1536":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/10\/Optimizing-MySQLMariaDB-RAM-Usage-Resource-Efficiency-Guide.jpg",1366,768,false],"2048x2048":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2023\/10\/Optimizing-MySQLMariaDB-RAM-Usage-Resource-Efficiency-Guide.jpg",1366,768,false]},"uagb_author_info":{"display_name":"Adrian Antony","author_link":"https:\/\/www.veeble.com\/kb\/author\/adrian\/"},"uagb_comment_info":0,"uagb_excerpt":"Your server&#8217;s performance is sluggish, and your websites are crawling like a snail. The main issue? MySQL is consuming too much of your server&#8217;s [&hellip;]","_links":{"self":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/1198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/comments?post=1198"}],"version-history":[{"count":14,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/1198\/revisions"}],"predecessor-version":[{"id":7563,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/1198\/revisions\/7563"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media\/8077"}],"wp:attachment":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media?parent=1198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/categories?post=1198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/tags?post=1198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}