lurscher
lurscher

Reputation: 26943

how to clone mysql db programmatically

i am interested in a programmatic approach (from c++ application) to mysql database cloning. I was thinking something along invoking mysqldump remotely like:

mysqldump -u root -p --all-databases > C:\MySQL_Backup.sql

and then going back with:

mysql --user=root --password=password < c:\ MySQL_Backup.sql

the problem with the last command would be that it assumes the restored database must be called the same as the original - so this only really works for backup and restore but not in general for cloning databases.

What approach would be available for database cloning with mysql? btw, i am not really using mysql but MariaDB, but that should not matter

Upvotes: 1

Views: 677

Answers (1)

Roadmaster
Roadmaster

Reputation: 5357

mysqldump will only work if you want to clone the entire DB server, including nicely clobbering the mysql database itself (all users, hosts, db permissions overwritten).

I'd use a combination of:

mysql -e "show databases"

to dump a list of databases. Then remove any you don't want to back up (such as information_schema and mysql), and for each remaining name do

mysqldump $database > $database.sql

then you can import the files to a specific database name:

mysql $new_database < $database.sql

Upvotes: 3

Related Questions