How to duplicate a MYSQL database?

4

Is there any way to duplicate a database in MYSQL by running some query ?

I can copy a table with this code:

CREATE TABLE table_2 AS SELECT * FROM table_1

But how do you copy to replicate a given database (on the same host)?

    
asked by anonymous 11.08.2015 / 22:00

2 answers

5

It is possible (and easier) using mysqldump per command line:

mysqldump -h [servidor] -u root -e "create database banco_novo"
mysqldump -h [servidor] -u root banco_antigo|mysql -h [servidor] -u root banco_novo

However there are alternatives with php and bash:

Note that in PHP you can run command lines too, using exec() for example.

    
11.08.2015 / 22:09
5

I usually do this outside phpMyAdmin, using mysqldump in the shell. First you make a dump of the current base:

mysqldump -R --user=usuario --password=senha nomedabase > arquivo.sql

The -R is to include procedures and functions in the dump. Then you create an empty base (can be from MyAdmin or any client), and import the dump back:

mysql --user=usuario --password=senha basedestino < arquivo.sql
    
11.08.2015 / 22:09