今天又遇到一个新的问题
原来有一个数据库A,要将数据库A以space开头和tmp开头的表移动到本实例的B数据库。
A数据库的表
-
create table tmp_a (a int);
-
create table tmp_b (a int);
-
create table tmp_c (a int);
-
create table tmp_d (a int);
-
create table tmp_e (a int);
-
create table tmp_f (a int);
-
create table space_a (a int);
-
create table space_b (a int);
-
create table space_c (a int);
-
create table space_d (a int);
-
create table space_e (a int);
-
create table space_f (a int);
-
create table a (a int);
-
create table b (a int);
-
create table c (a int);
-
create table d (a int);
-
create table e (a int);

我想干脆用mysqldump导出,再导入B数据库。
但是mysqldump不支持通配符批量导出。
查看以tmp和space开头的表
-
mysql -uxx -pxx -e "show tables from a where tables_in_a like 'tmp_%' or tables_in_a like 'space_%'";

只是查出来还不行,还要去除分割符等
-
mysql -uxx -pxx -Da -Bse "show tables from a where tables_in_a like 'tmp_%' or tables_in_a like 'space_%'";

将上面的结果作为一个Shell的变量传给mysqldump进行批量导出
-
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 需要返回多个条件的记录,需要使用以下的方式
-
SHOW TABLES
-
FROM ``
-
WHERE
-
`Tables_in_` LIKE '%cms%'
-
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/
,如需转载,请注明出处,否则将追究法律责任。