INFORMATION_SCHEMA, in particular by favorite TABLES table is not only helpful to understand tables you have on the system, but I have also found it to be very helpful as a scripting language for variety of database administration tasks. It can be more straightforward compared to using shell or Perl when the operation is database specific.
For example if you would like to MySQLDump only Innodb table in one file per database you can do the following:
1 2 3 4 5 6 7 8 9 10 11 | mysql> select concat("mysqldump ",table_schema," ",table_name, " >> ",table_schema,".sql") from tables where engine='innodb' limit 5; +------------------------------------------------------------------------------+ | concat("mysqldump ",table_schema," ",table_name, " >> ",table_schema,".sql") | +------------------------------------------------------------------------------+ | mysqldump art73 article73 >> art73.sql | | mysqldump art73 author73 >> art73.sql | | mysqldump art73 forum73 >> art73.sql | | mysqldump art73 forum_stats73 >> art73.sql | | mysqldump art73 img_out73 >> art73.sql | +------------------------------------------------------------------------------+ 5 rows in set (48.69 sec) |
As you can see we’re just getting the set of commands to run. How to make it easily runable ? Well just use INTO OUTFILE to create very simple shell script:
1 2 | mysql> select concat("mysqldump ",table_schema," ",table_name, " >> ",table_schema,".sql") from tables where engine='innodb' into outfile '/tmp/dump.sh'; Query OK, 328 rows affected (46.88 sec) |
In other case I needed to restore Innodb tables from mysqldump because of corrupted Innodb tablespace – to do this I had to clean all .frm files which correspond to innodb tables (as well as ibdata1 and innodb log files). With shell you could so it by looking for .frm files which do not have corresponding .MYI files…. but this will also get all MEMORY tables which we want to leave in tact. Using similar approach we can do:
1 2 3 4 5 6 7 8 9 10 11 | mysql> select concat("rm -f /var/lib/mysql/",table_schema,"/",table_name, ".frm") from tables where engine='innodb' limit 5; +---------------------------------------------------------------------+ | concat("rm -f /var/lib/mysql/",table_schema,"/",table_name, ".frm") | +---------------------------------------------------------------------+ | rm -f /var/lib/mysql/art73/article73.frm | | rm -f /var/lib/mysql/art73/author73.frm | | rm -f /var/lib/mysql/art73/forum73.frm | | rm -f /var/lib/mysql/art73/forum_stats73.frm | | rm -f /var/lib/mysql/art73/img_out73.frm | +---------------------------------------------------------------------+ 5 rows in set (44.68 sec |
Do not get me wrong, this is far from replacement for shell in all cases but it is rather handy for some niche tasks, in particular which involve unix commands driven by MySQL meta data.
Another fun trick is to use INFORMATION_SCHEMA tables to generate SQL statements, and pipe them back into MySQL:
mysql -uroot -e”SELECT concat(‘ALTER TABLE ‘,table_schema,’.’,table_name, ‘ ENGINE = MyISAM;’)
#sql
FROM information_schema.tables WHERE engine=’innodb’ AND table_schema = ‘test’;” | mysql -urootTodd,
Yes good note. You can pipe it too which is better in many cases. I just used 2 different boxes in this case so outfile was easier.
ниодного индекÑа в таблице TABLES – ну ппц проÑто.
запроÑÑ‹ на ней проÑто ужоÑ.
а так, муÑкул в Ñтом отношении не только Ñ Ñтими таблицами иÑпользовать можно – Ð´Ð»Ñ Ñ€Ð°Ð·Ð½Ñ‹Ð¹ мелких задач такой путь тоже подходит. ну или Ñформировать csv например из какойто таблицы, причем можно Ñразу Ñделать некоторую обработку.
Metadata, baby, metadata 🙂 Powerful stuff…
Nmike,
Information Schema is not really the table – MySQL scans the databases finding all the tables and putting their stats in the temporaty table which is later used for queries. This is why it takes so long time.
This is a great post. It opened my eyes to a whole new way of controlling my DB. Funny how sometimes you just need a little ‘inspiration’ and your mind will open up to a plethora of new possibilities. THANK YOU FOR THIS POST!
Todd, the great script. Very useful.