Wednesday, May 28, 2008

Create an alias for your mysql database

I administer a mysql database that is updated with new snapshots (i.e. completely overwritten) every week and have users that generally need to access the latest snapshot. However, occasionally they will need to access an older version of the database, so all versions must be kept online. In the past, when I received a snapshot, I would create a database with the date of the snapshot in the title and send out an email to the users with the new connection information. However, since mysql database names are simply the name of the directory that they are stored in, it is easy to create an alias for the latest snapshot of the database simply by creating a symbolic link. For example, on a linux system the databases are generally stored under /var/lib/mysql. So, if you have a database named db_20080304 you can create a symbolic link to this database like:

ln -s /var/lib/mysql/db_20080304 /var/lib/mysql/db_latest

Now you should have a database called db_latest. You can repeat this process each time you receive a new snapshot of the database. Note that mysql views this as another database so you will need to set the permissions on db_latest separately from db_20080304. The nice thing about this is that you will only need to set permission once. The users do not need to have permissions on the actual databases; only on db_latest. I'm not sure how this will work on other operating systems, but would be glad to hear comments.

6 comments:

Unknown said...

Symlink doesn't seem to work for InnoDB though

adaniels said...

Be careful with this. Since MySQL sees it as different databases, internal table locking won't apply to both databases. If you perform write actions to both dbs, make sure you enable external locking.

http://dev.mysql.com/doc/refman/5.0/en/external-locking.html

adaniels said...

This doesn't work for me actually. It results in an error.

ERROR 1034 (HY000): Incorrect key file for table 'attachment_type'; try to repair it

Salsan Jose said...
This comment has been removed by the author.
Salsan Jose said...
This comment has been removed by the author.
Salsan Jose said...

Creating view is the easiest way for alias. See the following post: http://mildcoder.com/about-creating-alias-for-table-in-mysql/