Robby on Rails: Reducing MySQL's memory usage on OS X Mavericksthoughts.sort_by{|t| t[:topic]}.collect tag:www.robbyonrails.com,2005:TypoTypo2013-11-24T13:03:21-05:00Robby Russellurn:uuid:d13a3b62-4411-4417-a25d-d3cf30813a452013-11-24T10:22:00-05:002013-11-24T13:03:21-05:00Reducing MySQL's memory usage on OS X Mavericks<p>Recently, I found myself re-installing everything from Homebrew and began to notice that MySQL was consuming nearly half a gig of memory. Given that I don’t do too much with MySQL on a regular basis, I opted to override a handful of default configuration options to reduce the memory footprint.</p>
<p>As you can see, a fresh MySQL install via homebrew was consuming over 400mb of memory.</p>
<p><img src="http://robbyonrails.com/files/mysqld-before.jpg" alt="" /></p>
<p>Here is how I reduced my memory footprint:</p>
<p><code>$ mkdir -p /usr/local/etc</code></p>
<p>Unless you already have a custom MySQL config file, you will want to add one into this directory.</p>
<p><code>$ vim /usr/local/etc/my.cnf</code></p>
<p>We’ll then paste in the following options into our file… and save it.</p>
<div class="typocode"><pre><code class="typocode_shell "> # Robby's MySQL overrides
[mysqld]
max_connections = 10
key_buffer_size = 16K
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 128K</code></pre></div>
<p>Finally, we’ll restart MySQL.</p>
<p><code>$ mysql.server stop</code></p>
<p>If you have MySQL setup in <code>launchctl</code>, it should restart automatically. After I did this, my MySQL instance was now closer to 80mb.</p>
<p><img src="http://robbyonrails.com/files/mysqld-after.jpg" alt="" /></p>
<p>So far, this has worked out quite well for my local Ruby on Rails development. Mileage may vary…</p>
<p>Having said that, how much memory are you now saving?</p><p>Recently, I found myself re-installing everything from Homebrew and began to notice that MySQL was consuming nearly half a gig of memory. Given that I don’t do too much with MySQL on a regular basis, I opted to override a handful of default configuration options to reduce the memory footprint.</p>
<p>As you can see, a fresh MySQL install via homebrew was consuming over 400mb of memory.</p>
<p><img src="http://robbyonrails.com/files/mysqld-before.jpg" alt="" /></p>
<p>Here is how I reduced my memory footprint:</p>
<p><code>$ mkdir -p /usr/local/etc</code></p>
<p>Unless you already have a custom MySQL config file, you will want to add one into this directory.</p>
<p><code>$ vim /usr/local/etc/my.cnf</code></p>
<p>We’ll then paste in the following options into our file… and save it.</p>
<div class="typocode"><pre><code class="typocode_shell "> # Robby's MySQL overrides
[mysqld]
max_connections = 10
key_buffer_size = 16K
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 128K</code></pre></div>
<p>Finally, we’ll restart MySQL.</p>
<p><code>$ mysql.server stop</code></p>
<p>If you have MySQL setup in <code>launchctl</code>, it should restart automatically. After I did this, my MySQL instance was now closer to 80mb.</p>
<p><img src="http://robbyonrails.com/files/mysqld-after.jpg" alt="" /></p>
<p>So far, this has worked out quite well for my local Ruby on Rails development. Mileage may vary…</p>
<p>Having said that, how much memory are you now saving?</p>