免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2187 | 回复: 4
打印 上一主题 下一主题

(转载)Using INFORMATION_SCHEMA instead of shell scripting [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-24 16:02 |只看该作者 |倒序浏览
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:

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:

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:

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.

论坛徽章:
8
综合交流区版块每周发帖之星
日期:2015-12-02 15:03:53数据库技术版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-09-14 06:20:00金牛座
日期:2014-10-10 11:23:34CU十二周年纪念徽章
日期:2013-10-24 15:41:34酉鸡
日期:2013-10-19 10:17:1315-16赛季CBA联赛之北京
日期:2017-03-06 15:12:44
2 [报告]
发表于 2008-12-24 16:28 |只看该作者
TKS

论坛徽章:
0
3 [报告]
发表于 2008-12-24 21:32 |只看该作者
好文,谢谢

论坛徽章:
5
未羊
日期:2014-08-07 15:42:10双子座
日期:2014-09-23 15:42:172015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:55:282022北京冬奥会纪念版徽章
日期:2015-08-10 16:30:32
4 [报告]
发表于 2008-12-24 21:49 |只看该作者
En太差,看不懂!

论坛徽章:
0
5 [报告]
发表于 2008-12-25 12:29 |只看该作者
Good.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP