How to Copy Database in MySQL
- Copy the Database On the Same Server Instance in MySQL
-
Use the
|
or Pipe Operator to Copy Command in MySQL
To create a copy of the existing database is known as the MySQL Clone
method. The clone includes creating a copy of table structure, constraints, functions, procedures, triggers, and all the features related to a table in one go.
The features allow users more reliability as even one node goes down, the replica or the clone can serve. Making a database copy and clone can help when an accidental loss or failure happens.
The backup process can happen daily or over a particular interval of time. The ways to copy the database over the same server instance are below.
Copy the Database On the Same Server Instance in MySQL
Copy the source database in a SQL script file. Create a new target database and then import the SQL script file into a new database.
It is a three-step process.
> mysql -u root -p
****
> mysqldump -u root -p firstDb > firstDbDump.sql
> create database newDb;
> mysql -u root -p newDb < firstdump.sql
> show databases;
> use newDb;
> show tables;
The detailed description of the first approach above and the commands are below.
-
To proceed with the first approach, install MySQL server 8.0 in the local system.
-
Proceed with the installation process, and after successful installing, reach the
bin
path. -
Open environment variables and add the path of the MySQL system in the system PATH variable. It allows the accessibility of MySQL from any place in the file system.
-
Open the command prompt and enter the command shown above. This command enables the use of the MySQL prompt.
The
-u
attribute specifies the username that proceeds the value; theroot
is the username. And the-p
attribute specifies the password, which comes in another line.The password is never added to the command. It comes in another line in an encrypted format.
-
Enter the command to copy files to a temporary intermediate file from the location.
-
mysqldump -u root -p database_name_to_be_cloned > filename_for_clone
is the command that specifies themysqldump
utility with username and password as options.It takes the database name as a parameter and puts the dump in a file. The
>
argument directs the output to a particular file. -
If the absolute file path of the file name is not provided, the SQL file will be stored in the
bin
directory where the MySQL server gets installed. -
Create a new database where the SQL is supposed to get imported to make a clone of the existing DB.
-
Once a temporary or intermediate file gets formed, it can again read into the newly formed DB using the
<
operator. The operator redirects the SQL result in thenewDb
database. -
The database can get checked using the command
show databases;
. This command will list out all the databases in the current SQL connection. -
The
use
command allows the user to use the specified database. The use command takes a parameter as a database name. -
The
show tables
allow the user to list all the tables present in a database. Hence the copied contents can get checked with the last three commands.
Use the |
or Pipe Operator to Copy Command in MySQL
One liner command takes a dump of the source database and redirects to another target database.
> mysqldump -u root -p sourceDb | mysql -u root -p targetDb
-
Reach to the
bin
folder where MySQL gets installed. -
Open the command prompt from the
bin
folder. -
Use the above command as it is, beforehand creating source and target database respectively.
-
The command creates the SQL dump using the
mysqldump
utility, taking username and password as mandatory options. And the output is redirected to another target database. -
The
|
or pipe symbol is mostly a LINUX/UNIX command operator that allows users to use two or more commands in one line.It acts as a pipeline to provide the output of the first command to another one. The second command is generally separated using the pipe symbol.
-
The command is the same described in approach one and uses the pipe symbol in one line.
Attaching a screenshot of the above command executed successfully.
As we enter the above command, it prompts the user for the password twice. As per the command here, the -p
option is mentioned twice, asking the user for the password for both databases.
Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.
LinkedIn