How do I save the tables of one database in MySQL and move them to another?

There are two databases in MySQL. Their names are different, but the structure of the tables is the same. How do I make a backup of one database and upload data to another? Please, with a description of the commands, what and how. Thank you in advance.

Author: Nicolas Chabanovsky, 2011-01-24

2 answers

mysqldump -u <user> -p<password> <db1> | mysql -u <user> -p<password> <db2>

The first command is mysqldump. Actually, it outputs the contents of the specified database as text. The second command-banally reads a dump from the standard stream (in the same format in which mysqldump issues it, which is convenient!) and fills it into the specified database.

Accordingly, they were linked by a conveyor belt - and everything worked out. You can use an intermediate file, if you prefer.

 3
Author: kirelagin, 2011-01-24 17:37:18

Another idea just in case.

There is such a secret syntax (don't tell anyone about it!):

CREATE TABLE <new_table> AS SELECT * FROM <old_table>;

If you remember that table names can be specified in the form имя_БД.имя_таблицы, then it is easy to move one table to another database.

However, you will not be able to move all the tables in one move. I'm afraid that without stored procedures, on pure SQL, this problem has no solution.

 1
Author: kirelagin, 2015-10-10 11:02:48