Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force an entire MySQL database to be in memory

For the purposes of running a large number of tests that interact with the database, I want to do two things:

  1. I would like to copy the schema of a database without copying its data. I can do this with a script that grabs the CREATE TABLE statements from each table in the database.

  2. Upon creating this database, I would like to force it to be 100% in memory.

I'm stuck on how to do part 2 - Is there an easier way to do this other than specifying each table's engine? Somehow that seems like a poor way of doing it.

like image 952
Fragsworth Avatar asked Feb 04 '11 05:02

Fragsworth


People also ask

How do I run MySQL in memory?

Create the MEMORY database and recreate the tables you'll be using with this syntax: CREATE TABLE tablename (...) ENGINE = MEMORY; . You can then import your data using LOAD DATA INFILE 'table_filename' INTO TABLE tablename for each table.

Can MySQL run in memory?

MySQL allocates buffers and caches to improve performance of database operations. The default configuration is designed to permit a MySQL server to start on a virtual machine that has approximately 512MB of RAM.

Which MySQL case eats all memory?

First of all, there are 3 major cases when MySQL will crash due to running out of memory: MySQL tries to allocate more memory than available because we specifically told it to do so. For example: you did not set innodb_buffer_pool_size correctly. This is very easy to fix.

What is buffering in MySQL?

MySQL allocates buffers and caches to improve performance of database operations. You can improve MySQL performance by increasing the values of certain cache and buffer-related system variables. You can also modify these variables to run MySQL on systems with limited memory.


2 Answers

Create the database in /dev/shm (ubuntu|debian) and it will be in RAM. It can grow up to 0.5 of available memory.

like image 161
Diego Torres Milano Avatar answered Oct 19 '22 11:10

Diego Torres Milano


As dtmilano said, you can put it on a tmpfs mounted filesystem. It doesn't have to be /dev/shm, but that is one place where tmpfs is usually mounted.

You can create a new one anywhere, though:

mount none -t tmpfs /path/to/dir

If it fills all your available RAM, it will use swap as a backup.

Put it in /etc/fstab to re-mount on boot. Just remember, it's a ram disk, so it starts out empty every time you reboot. See: http://www.howtoforge.com/storing-files-directories-in-memory-with-tmpfs

Alternately, as suggested by yuxhuang you can create a table of type MEMORY. It also empties on restart, though the table definition remains. The MEMORY table type has a few restrictions, though. It uses fixed-size rows, for example, so text and blob columns are not allowed, and varchar isn't variable length. See: http://dev.mysql.com/doc/refman/5.0/en/memory-storage-engine.html

like image 27
tylerl Avatar answered Oct 19 '22 10:10

tylerl