10

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.

4 Answers 4

11

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

Sign up to request clarification or add additional context in comments.

4 Comments

This sounds good, but is there a similar solution for CentOS?
Check if you have it: $ mount | grep shm. Otherwise, you can create a filesystem on one of /dev/ram* and mount it somewhere.
Centos 5.0+ always has /dev/shm mounted. However, you can mount a tmpfs anywhere you like. tmpfs has been available for some time.
guys, dont know if its OK, just tried : mv /usr/local/mysql/data/blah /dev/shm.... ln -s /dev/shm/blah -> data/blah.... restarted mysql... its flying
2

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

Comments

1
SET storage_engine=MEMORY;

This is going to set the default storage engine for the current session.

3 Comments

There is a limit how large a MEMORY table can be. it is doubtful (but possible) that all the tables in a db will be able to be MEMORY tables
Memory tables still have their metadata stored on disc, and they don't behave the same as the other engines.
Further expanding @MarkR comment: MyISAM and Innodb are already very different and can lead to different behavior, leading to bugs that only happen to one engine. Thus, I recommend NOT changing your database engine to MEMORY, as it will very likely introduce engine-specific bugs.
1

If you are using Windows, in your database creation script you can create the tables adding the MEMORY parameter like this:

CREATE TABLE IF NOT EXISTS `user` (
  `id` varchar(23) NOT NULL,
  `username` varchar(250) NOT NULL,
  ...
) ENGINE=MEMORY DEFAULT CHARSET=utf8;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.