ITPub博客

首页 > 数据库 > MySQL > MysqlDump根据通配符批量导出

MysqlDump根据通配符批量导出

原创 MySQL 作者:壹頁書 时间:2014-08-28 22:12:19 1 删除 编辑
今天又遇到一个新的问题
原来有一个数据库A,要将数据库A以space开头和tmp开头的表移动到本实例的B数据库。
A数据库的表
  1. create table tmp_a (a int);
  2. create table tmp_b (a int);
  3. create table tmp_c (a int);
  4. create table tmp_d (a int);
  5. create table tmp_e (a int);
  6. create table tmp_f (a int);
  7. create table space_a (a int);
  8. create table space_b (a int);
  9. create table space_c (a int);
  10. create table space_d (a int);
  11. create table space_e (a int);
  12. create table space_f (a int);
  13. create table a (a int);
  14. create table b (a int);
  15. create table c (a int);
  16. create table d (a int);
  17. create table e (a int);


我想干脆用mysqldump导出,再导入B数据库。
但是mysqldump不支持通配符批量导出。

查看以tmp和space开头的表

  1. mysql -uxx -pxx -e "show tables from a where tables_in_a like 'tmp_%' or tables_in_a like 'space_%'";


只是查出来还不行,还要去除分割符等

  1. mysql -uxx -pxx -Da -Bse "show tables from a where tables_in_a like 'tmp_%' or tables_in_a like 'space_%'";


将上面的结果作为一个Shell的变量传给mysqldump进行批量导出

  1. mysqldump -uxx -pxx a $(mysql -uxx -pxx -Da -Bse "show tables from a where tables_in_a like 'tmp_%' or tables_in_a like 'space_%'") > a.sql
最后将a.sql导入B数据库即可。

其中show tables 需要返回多个条件的记录,需要使用以下的方式
  1. SHOW TABLES
  2. FROM ``
  3. WHERE
  4.     `Tables_in_` LIKE '%cms%'
  5.     OR `Tables_in_` LIKE '%role%';
参考:
http://stackoverflow.com/questions/5609620/show-tables-statement-with-multiple-like-values

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-1259796/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6339893