I have recently started on a PoC project wherein we are developing a small web app. The initial setup is done on a micro instance from AWS. We are on rails+mysql stack.

After installing/running MySQL, I see that about 500+ MB RAM has been consumed already; leaving quite less for rest of the systems (micro instances have barely 620 MB RAM).

Our app is fairly simple at this stage. Can I do something to reduce the memory consumed by MySQL server?

Appreciate the help.

share|improve this question
    
How are you defining memory consumed ? There are lots of MySQL settings that control memory size. – Frederick Cheung May 20 '12 at 20:04
    
I am no mysql admin, but I see that on the system, nothing else is running except the OS itself and mysql server, and more than 500M is consumed... – Gyan May 20 '12 at 20:16
    
Optimize your queries! Look at the slow query log to see the exact queries. Probably a lot of RAM is wasted in temporary tables – Maxim Krizhanovsky May 20 '12 at 20:25
up vote 32 down vote accepted

Change this setting in the MySQL configuration file (my.cnf)

key_buffer              = 8M 
max_connections         = 30 # Limit connections
query_cache_size        = 8M # try 4m if not enough 
query_cache_limit       = 512K
thread_stack            = 128K
share|improve this answer
    
helped a lot..thanks for the tips. – Gyan May 25 '12 at 5:49
3  
See also Mahn's answer, which is tremendously more radical and efficient. – Denis de Bernardy Oct 27 '13 at 18:28
    
I went for 1.5G to 0.5G. Thanks a lot – Wistar Dec 21 '15 at 19:15
    
This answer combined with Mahn's decreased the usage from 21% of RAM on t2.micro to 0.2%!!! It went from 200M to 2M – Ricardo BRGWeb Dec 30 '15 at 20:23

In your my.cnf file:

performance_schema = 0

And restart mysql. This should chop memory usage dramatically if you previously had it on.


2016 Edit: From MySQL 5.7.8 onwards, the above is not enough to free your memory of performance schema data:

As of MySQL 5.7.8, even when the Performance Schema is disabled, it continues to populate the global_variables, session_variables, global_status, and session_status tables.

(source)

To prevent this behaviour, set show_compatibility_56 to 1 in addition to performance_schema. That is to say, your my.cnf changes should look like:

performance_schema = 0
show_compatibility_56 = 1
share|improve this answer
    
cut mine in half – Alex R Oct 4 '13 at 5:00
2  
Cut mine from 400MB to 25MB. – Isaac Oct 18 '13 at 11:14
4  
I wish I could vote this up ten times... – Denis de Bernardy Oct 27 '13 at 18:12
    
Yes for slow traffic(no inspection need for performance). – schemacs Jan 15 '15 at 14:01

Just to add to the other answer. I recently had this problem myself with the Amazon micro instance (not Ubuntu). The my.cnf file is almost empty so what I did was this:

cp /etc/my.cnf /etc/my.cnf.orig
cp /usr/share/mysql/my-medium.cnf /etc/my.cnf

Edit my.cnf and enable the innodb lines if applicable. Restart mysqld.

Also the micro instance has no swap, that might be a problem..

SWAPFILE=/mnt/swapfile.swap
dd if=/dev/zero of=$SWAPFILE bs=1M count=512
mkswap $SWAPFILE
swapon $SWAPFILE

Then in /etc/rc.local add:

swapon /mnt/swapfile.swap

To save memory in ruby you might want to use ruby enterprise:

bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
gpasswd -a root rvm
source /etc/profile.d/rvm.sh
rvm get head
rvm reload
rvm install ree
rvm --default use ree
share|improve this answer
    
Thanks for tips on my.cfg. I am currently using a ruby built from source. REE page says that it would bring down memory usage by about 30%. However, another question that i couldnt find answer to, is rails 3.2.2 going to work fine with ruby 1.8.7 (REE is based/compatible with ruby 1.8.7)... – Gyan May 25 '12 at 5:42
    
Also, I couldnt get the swap mechanism to start.. – Gyan May 25 '12 at 5:58
    
When installing rails with gem after installing ree you get 3.2.3. – EivinGS May 26 '12 at 13:48
    
Is the swap working when doing swapon /mnt/swapfile.swap but not starting at boot? If your rc.local file has this at the top: #!/bin/sh -e, you can try to remove the -e. – EivinGS May 26 '12 at 13:52
2  
Enabling a swap file is the single most valuable response to my months long trouble with Amazon micro. This helped tremendously compared to fiddling with my.cnf which frankly makes no difference with only 600KB RAM. – Slawa Sep 10 '13 at 23:30

I have a server with only 500mb ram and found that mysql started using a lot of ram as my tables got larger. After playing with a bunch of the settings, what reduced memory usage for me was to convert all my tables to MyISAM. If you dont need the features of innodb converting tables to MyISAM helps quite a bit. You can convert tables like this :

ALTER TABLE test.mytable ENGINE=MyISAM;

After this change I found that memory usage decreased by 20%. To get a further reduction in memory usage you can convert ALL of your tables to MyISAM and then turn off innodb support in mysql altogether. This reduced my memory usage by 50%.

You can do this by adding :

[mysqld]
default_storage_engine=myisam
innodb=OFF

and then restarting mysql.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.