Tips and tricks for LAMP (Linux Apache MySQL PHP) developers.

When I cant find a suitable solution to a problem and have to work it out for myself I'll post the result here so hopefully others will find it useful. (All code is offered in good faith. It may not be the best solution, just a solution).
Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

Saturday, 12 November 2011

Rename a MySQL database

Recently I needed to rename a MySQL database I was working with to make way for another with the same name. When I looked around for how to do this it seemed that the only option was to create the new database and then copy each table to it, via INSERT...SELECTs or a dump and import. It was quite a large database and this would have been very time consuming.


After discussing it with a colleague we came up with another way to do it by using bash to iterate over each table in the schema and then use the RENAME TABLE command:

for TABLE in `mysql -u[USERNAME] -p[PASSWORD] [OLD DB] -e "SHOW TABLES" -B -N -s`; do mysql -u[USERNAME] -p[PASSWORD] [OLD DB] -e "RENAME TABLE [OLD DB].$TABLE TO [NEW DB].$TABLE"; done;

This worked a treat (I've been using my new database reliably for a while now) and was fast too!

Saturday, 10 October 2009

No disk space causes random errors

I'm sure most of you are aware of this but it's something that always takes me a long time to remember.

Whenever you have a seemingly random error on your Linux box - strange MySQL or Apache errors, being unable to write to a file that you could write to a moment ago, etc - always do a quick df -h to check how much disk space you've got left.

This problem is especially evident for those of us developing on virtual machines that often have limited disk space and so can run out easily. A quick recce of your /var/log/ (or equivalent) directory is a good starting point for freeing up space but you may also want to try find / -type f -size +50000k