How To Import and Export Databases in MySQL using Command Line on Ubuntu Server 20.04

You can always use tools such as MySQL workbench and phpMyAdmin, but MySQL provides command-line tools for the bulk processing of data that are more efficient and flexible.

Follow these steps to import and export Databases in MySQL.

Exporting MySQL database

Step 1 : To export a database from the MySQL server, use the following command

Step 2 : To export specific tables from a database, use the following command

Step 3 : To compress exported data, use gzip

Importing MySQL database

Step 4 : To import an SQL file to a MySQL database, we need to first create a database

Step 5 : Once the database is created, import data with the following command

Importing MySQL database with foreign key constraints

Step 6 : Login to MySQL using your credentials with the following command

Step 8 : we need to create a database

Step 9 : Select a database to work with

Step 10 : Set your MySQL foreign key constraint checks to 0 using the next command

Step 8 : Import your database using a source command like the one below

Step 9 : And set your MySQL foreign key constraint checks back to 1 using the command that follows