10

In MySQL I want to clone certain databases. Is there such a thing as a

CREATE DATABASE LIKE <template database here>

command?
I know, there is

CREATE TABLE LIKE 
shA.t
  • 16,580
  • 5
  • 54
  • 111
Anton
  • 105
  • 1
  • 3
  • 7

3 Answers3

4

If you want to clone a database, my_database for example, you'll want to run a mysql query and then run a command in the linux terminal:

mysql> CREATE DATABASE my_database_copy;

linux terminal (on the machine with the database): mysqldump -u username -p my_database | mysql -u username -p my_database_copy

From there you'll likely get 2 "Enter password:" prompts....just input your password, press enter, and wait :)

rgenito
  • 1,751
  • 13
  • 8
  • You'll want to make sure you grant privileges on ````my_database_copy```` before you pipe output from ````my_database```` – Eddie Rowe Aug 16 '23 at 14:52
2

There is no such command.

But you can create a backup (SQL file) of your database, and then restore all objects in the new database.

Also, you can use GUI tools in dbForge Studio for MySQL (free express edition) - Backup or restore a database. It will help you quickly recreate database and its contents.

Devart
  • 119,203
  • 23
  • 166
  • 186
1
  1. I will prefer taking backup and restoring
  2. If it's just for cloning, I will go for MySQLDump


This is just one another way of doing:

mysql> create database new_dbname;

mysql> select concat('create table
new_dbname.',TABLE_NAME,' like wings2020.',TABLE_NAME,'; insert into
new_dbname.',TABLE_NAME,' select * from ol_dbname.',TABLE_NAME,';')
from information_schema.tables where table_schema = 'old_dbname'  into
outfile '/tmp/a.txt'; 

mysql> source /tmp/a.txt;
shA.t
  • 16,580
  • 5
  • 54
  • 111
Angelin Nadar
  • 8,944
  • 10
  • 43
  • 53