How To Import and Export Databases in MySQL or MariaDB

How To Import and Export Databases in MySQL or MariaDB
Riddhi Jindal
Riddhi Jindal

Created: 05/25/2020 12:14 PM - Updated: 06/03/2020 8:22 AM

How To Import and Export Databases in MySQL or MariaDB

Prerequisites

  • Access to a Linux server running MySQL or MariaDB
  • The database name and user credentials for it.


Exporting the Database

·         mysqldump -u username -p database_name > data-dump.sql
  • username the name to log in to the database.
  • database_name Database to export.
  • data-dump.sql  file in the current directory in which the output will be saved.
·         head -n 5 data-dump.sql
SQL dump fragment
-- MySQL dump 10.13  Distrib 5.7.16, for Linux (x86_64)
--
-- Host: localhost    Database: database_name
-- ------------------------------------------------------
-- Server version       5.7.16-0ubuntu0.16.04.1


Importing the Database

·         mysql -u root -p 

The new database is called new_database:

·         CREATE DATABASE new_database;
Output
Query OK, 1 row affected (0.00 sec)

Press  CTRL+D to exit.

mysql -u username -p new_database < data-dump.sql

  • username the name to log in to the database.
  • newdatabase  name of database created right now.
  • data-dump.sql  data dump file which has to be imported, located in the current directory.

If it is success then no output is produced. In the case of error, error occurred is shown on the terminal.

Was this article helpful?

0 Out of 0 Marked As Helpfull

Have more questions? Please Contact Us