如何有选择地将所有innodb表转储到mysql数据库中?

时间:2010-02-18 19:38:56

标签: mysql innodb mysqldump

我有一个名为av2web的数据库,其中包含130个MyISAM表和20个innodb表。我想把这20个innodb表的mysqldump,并作为MyISAM表导出到另一个数据库。

你能告诉我更快的方法吗?

由于 Pedro Alvarez Espinoza。

2 个答案:

答案 0 :(得分:3)

如果这是一次性操作,我会这样做:

use DB;
show table status name where engine='innodb';

并从名称列中执行矩形复制/粘贴:

+-----------+--------+---------+------------+-
| Name      | Engine | Version | Row_format |
+-----------+--------+---------+------------+-
| countries | InnoDB |      10 | Compact    |
| foo3      | InnoDB |      10 | Compact    |
| foo5      | InnoDB |      10 | Compact    |
| lol       | InnoDB |      10 | Compact    |
| people    | InnoDB |      10 | Compact    |
+-----------+--------+---------+------------+-

到文本编辑器并将其转换为命令

mysqldump -u USER DB countries foo3 foo5 lol people > DUMP.sql

然后在DUMP.sql中用ENGINE=InnoDB替换ENGINE=MyISAM的所有实例后导入

如果你想避免使用矩形复制/粘贴魔术,你可以这样做:

use information_schema;
select group_concat(table_name separator ' ') from tables 
    where table_schema='DB' and engine='innodb';

将返回countries foo3 foo5 lol people

答案 1 :(得分:1)

我知道这是一个老问题。我只想分享这个生成mysqldump命令的脚本,并展示如何恢复它

脚本的以下部分将生成一个用于创建mysql备份/转储的命令

SET SESSION group_concat_max_len = 100000000; -- this is very important when you have lots of table to make sure all the tables get included
SET @userName = 'root'; -- the username that you will login with to generate the dump
SET @databaseName = 'my_database_name'; -- the database name to look up the tables from
SET @extraOptions = '--compact --compress'; -- any additional mydqldump options https://dev.mysql.com/doc/refman/5.6/en/mysqldump.html
SET @engineName = 'innodb'; -- the engine name to filter down the table by
SET @filename = '"D:/MySQL Backups/my_database_name.sql"'; -- the full path of where to generate the backup too

-- This query will generate the mysqldump command to generate the backup
SELECT
 CASE WHEN tableNames IS NULL 
            THEN 'No tables found. Make sure you set the variables correctly.' 
      ELSE CONCAT_WS(' ','mysqldump -p -u', @userName, @databaseName, tableNames, @extraOptions, '>', @filename)
      END AS command  
FROM (
    SELECT GROUP_CONCAT(table_name SEPARATOR ' ') AS tableNames 
    FROM INFORMATION_SCHEMA.TABLES 
    WHERE table_schema= @databaseName AND ENGINE= @engineName
) AS s;

脚本的以下部分将生成一个命令,将mysql备份/转储恢复到相同或不同服务器上的特定数据库

SET @restoreIntoDatabasename = @databaseName; -- the name of the new database you wish to restore into
SET @restoreFromFile = @filename; -- the full path of the filename you want to restore from
-- This query will generate the command to use to restore the generated backup into mysql
SELECT CONCAT_WS(' ', 'mysql -p -u root', @restoreIntoDatabasename, '<', @restoreFromFile);