简体   繁体   中英

How to selectively dump all innodb tables in a mysql database?

I have a database called av2web, which contains 130 MyISAM tables and 20 innodb tables. I wanna take mysqldump of these 20 innodb tables, and export it to another database as MyISAM tables.

Can you tell me a quicker way to achieve this?

Thanks Pedro Alvarez Espinoza.

If this was an one-off operation I'd do:

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

and do a rectangular copy/paste from the Name column:

+-----------+--------+---------+------------+-
| 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    |
+-----------+--------+---------+------------+-

to a text editor and convert it to a command

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

and then import after replacing all instances of ENGINE=InnoDB with ENGINE=MyISAM in DUMP.sql

If you want to avoid the rectangular copy/paste magic you can do something like:

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

which will return countries foo3 foo5 lol people

I know this is an old question. I just want to share this script that genrate the mysqldump command and also shows how to restore it

This following portion of the script will generate a command to create a mysql backup/dump

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;

This following portion of the script will generate a command to restore mysql backup/dump into a specific database on the same or a different server

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM