Thursday, July 23, 2009

Attaching and detaching a MYISAM datbase

Let's say you have a huge MYSQL database in one MYSQL instance. You want to make a copy of this database in another MYSQL instance. There are several ways you can do a copy
1) Export the database:- Use Mysqldump. Basically, this creates a huge SQL file that contains all the SQL statements for creating and populating data into the database. This way is good for creating empty starter databases, because you can easily change the schema by changing the SQL. A good tutorial is here
2) Just copy the database:- This works only for ISAM tables.
First a short background on how MYSQL stores data:- If you look into your MYSQL data folder, you will see that there is a folder for every database in the MYSQL instance. This folder has 3 files for every table in the database
  • frm table - I think this file contains the schema of the table
  • myd table - for an ISAM table, this file holds the data. A InnodDB table doesn;t have any data in this file. The data is stored in a seperate file outside of the folder
  • myi table - this file holds the indexes
Since, a database is just a collection of tables, you see these files for every table in the database. As long as all your tables are ISAM, everything that MYSQL needs to retreive data from the database is in this folder

So, this is how you detach and attach the database from one instance to another:- You just copy the folder that holds the database to the data folder of the instance you are attaching the database to. That's it! You don't even have to restart MYSQL.

No comments:

Post a Comment